Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/y-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }))
Expand All @@ -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}
*/
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions tests/y-indexeddb.tests.js
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down