From 4bd75e54380f28ae7b2d2c30e3612ecd5774c1a1 Mon Sep 17 00:00:00 2001 From: frontend-sensei Date: Mon, 2 Oct 2023 21:56:03 +0300 Subject: [PATCH 1/2] Make PREFERRED_TRIM_SIZE configurable from IndexeddbPersistence instance via passing options --- src/y-indexeddb.js | 11 +++++++---- tests/y-indexeddb.tests.js | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/y-indexeddb.js b/src/y-indexeddb.js index 07f4fbb..5c19cbc 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.PREFERRED_TRIM_SIZE) { 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,17 @@ export class IndexeddbPersistence extends Observable { /** * @param {string} name * @param {Y.Doc} doc + * @param {Object} [options={}] + * @param {number} [options.PREFERRED_TRIM_SIZE=500] */ - constructor (name, doc) { + constructor (name, doc, options = {}) { super() this.doc = doc this.name = name this._dbref = 0 this._dbsize = 0 this._destroyed = false + this.PREFERRED_TRIM_SIZE = options.PREFERRED_TRIM_SIZE || DEFAULT_PREFERRED_TRIM_SIZE /** * @type {IDBDatabase|null} */ @@ -108,7 +111,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.PREFERRED_TRIM_SIZE) { // 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) From 447a94737e3211c88451d297f77000116259ee47 Mon Sep 17 00:00:00 2001 From: frontend-sensei Date: Tue, 3 Oct 2023 21:51:09 +0300 Subject: [PATCH 2/2] Improve namings. Add documentation --- src/y-indexeddb.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/y-indexeddb.js b/src/y-indexeddb.js index 5c19cbc..ca06a47 100644 --- a/src/y-indexeddb.js +++ b/src/y-indexeddb.js @@ -36,7 +36,7 @@ export const fetchUpdates = (idbPersistence, beforeApplyUpdatesCallback = () => export const storeState = (idbPersistence, forceStore = true) => fetchUpdates(idbPersistence) .then(updatesStore => { - if (forceStore || idbPersistence._dbsize >= idbPersistence.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 })) @@ -56,7 +56,7 @@ export class IndexeddbPersistence extends Observable { * @param {string} name * @param {Y.Doc} doc * @param {Object} [options={}] - * @param {number} [options.PREFERRED_TRIM_SIZE=500] + * @param {number} [options.preferredTrimSize=500] */ constructor (name, doc, options = {}) { super() @@ -65,7 +65,11 @@ export class IndexeddbPersistence extends Observable { this._dbref = 0 this._dbsize = 0 this._destroyed = false - this.PREFERRED_TRIM_SIZE = options.PREFERRED_TRIM_SIZE || DEFAULT_PREFERRED_TRIM_SIZE + /** + * 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} */ @@ -111,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 >= this.PREFERRED_TRIM_SIZE) { + if (++this._dbsize >= this._trim) { // debounce store call if (this._storeTimeoutId !== null) { clearTimeout(this._storeTimeoutId)