diff --git a/src/y-indexeddb.js b/src/y-indexeddb.js index 07f4fbb..ca06a47 100644 --- a/src/y-indexeddb.js +++ b/src/y-indexeddb.js @@ -6,7 +6,7 @@ import { Observable } from 'lib0/observable' const customStoreName = 'custom' const updatesStoreName = 'updates' -export const PREFERRED_TRIM_SIZE = 500 +export const DEFAULT_PREFERRED_TRIM_SIZE = 500 /** * @param {IndexeddbPersistence} idbPersistence @@ -36,7 +36,7 @@ export const fetchUpdates = (idbPersistence, beforeApplyUpdatesCallback = () => export const storeState = (idbPersistence, forceStore = true) => fetchUpdates(idbPersistence) .then(updatesStore => { - if (forceStore || idbPersistence._dbsize >= PREFERRED_TRIM_SIZE) { + if (forceStore || idbPersistence._dbsize >= idbPersistence._trim) { idb.addAutoKey(updatesStore, Y.encodeStateAsUpdate(idbPersistence.doc)) .then(() => idb.del(updatesStore, idb.createIDBKeyRangeUpperBound(idbPersistence._dbref, true))) .then(() => idb.count(updatesStore).then(cnt => { idbPersistence._dbsize = cnt })) @@ -55,14 +55,21 @@ export class IndexeddbPersistence extends Observable { /** * @param {string} name * @param {Y.Doc} doc + * @param {Object} [options={}] + * @param {number} [options.preferredTrimSize=500] */ - constructor (name, doc) { + constructor (name, doc, options = {}) { super() this.doc = doc this.name = name this._dbref = 0 this._dbsize = 0 this._destroyed = false + /** + * The preferred size of updates after which IndexedDB data is trimmed. + * @type {number|number} + */ + this._trim = options.preferredTrimSize || DEFAULT_PREFERRED_TRIM_SIZE /** * @type {IDBDatabase|null} */ @@ -108,7 +115,7 @@ export class IndexeddbPersistence extends Observable { if (this.db && origin !== this) { const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ (this.db), [updatesStoreName]) idb.addAutoKey(updatesStore, update) - if (++this._dbsize >= PREFERRED_TRIM_SIZE) { + if (++this._dbsize >= this._trim) { // debounce store call if (this._storeTimeoutId !== null) { clearTimeout(this._storeTimeoutId) diff --git a/tests/y-indexeddb.tests.js b/tests/y-indexeddb.tests.js index fcc7456..fa09913 100644 --- a/tests/y-indexeddb.tests.js +++ b/tests/y-indexeddb.tests.js @@ -1,6 +1,6 @@ import * as Y from 'yjs' -import { IndexeddbPersistence, clearDocument, PREFERRED_TRIM_SIZE, fetchUpdates } from '../src/y-indexeddb.js' +import { IndexeddbPersistence, clearDocument, DEFAULT_PREFERRED_TRIM_SIZE, fetchUpdates } from '../src/y-indexeddb.js' import * as t from 'lib0/testing.js' import * as promise from 'lib0/promise.js' @@ -42,12 +42,12 @@ export const testIdbUpdateAndMerge = async tc => { await persistence2.whenSynced t.assert(calledObserver) t.assert(arr2.length === 2) - for (let i = 2; i < PREFERRED_TRIM_SIZE + 1; i++) { + for (let i = 2; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) { arr1.insert(i, [i]) } await promise.wait(100) await fetchUpdates(persistence2) - t.assert(arr2.length === PREFERRED_TRIM_SIZE + 1) + t.assert(arr2.length === DEFAULT_PREFERRED_TRIM_SIZE + 1) t.assert(persistence1._dbsize === 1) // wait for dbsize === 0. db should be concatenated } @@ -70,11 +70,11 @@ export const testIdbConcurrentMerge = async tc => { await persistence2.whenSynced t.assert(arr2.length === 2) arr1.insert(0, ['left']) - for (let i = 0; i < PREFERRED_TRIM_SIZE + 1; i++) { + for (let i = 0; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) { arr1.insert(i, [i]) } arr2.insert(0, ['right']) - for (let i = 0; i < PREFERRED_TRIM_SIZE + 1; i++) { + for (let i = 0; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) { arr2.insert(i, [i]) } await promise.wait(100)