Skip to content
Open
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
21 changes: 13 additions & 8 deletions src/y-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Y from 'yjs'
import * as idb from 'lib0/indexeddb.js'
import * as mutex from 'lib0/mutex.js'
import { Observable } from 'lib0/observable.js'
import * as promise from 'lib0/promise.js'

const customStoreName = 'custom'
const updatesStoreName = 'updates'
Expand Down Expand Up @@ -73,15 +74,19 @@ export class IndexeddbPersistence extends Observable {
/**
* @type {Promise<IndexeddbPersistence>}
*/
this.whenSynced = this._db.then(db => {
this.db = db
const currState = Y.encodeStateAsUpdate(doc)
return fetchUpdates(this).then(updatesStore => idb.addAutoKey(updatesStore, currState)).then(() => {
this.emit('synced', [this])
this.synced = true
return this
})
this.whenSynced = promise.create((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this change about?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I guess I misunderstood your ask. Above, you requested this change:

this.whenSynced = new Promise(..)
this.whenSynced.catch(err => { this.emit('error', ..) })

Which I interpreted to mean that you wanted this.whenSynced to be a new Promise instead of the Promise returned from this._db.

What would you like me to do instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. Don't take my above comment too literally. The second part this.whenSynced.catch(err => { this.emit('error', ..) }) is fine. The first part adds five extra lines of code with no apparent benefit. Please rework the PR to include a minimal amount of changes.

this._db.then(db => {
this.db = db
resolve(this)
const currState = Y.encodeStateAsUpdate(doc)
return fetchUpdates(this).then(updatesStore => idb.addAutoKey(updatesStore, currState)).then(() => {
this.emit('synced', [this])
this.synced = true
return this
})
}).catch(reject)
})
this.whenSynced.catch(err => { this.emit('error', [err]) })
/**
* Timeout in ms untill data is merged and persisted in idb.
*/
Expand Down