Skip to content

fix/use latest doc on persist & correct queueing #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
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
32 changes: 15 additions & 17 deletions src/y-socket-io/y-socket-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ export class YSocketIO {
* @readonly
*/
debouncedPersistMap = new Map()
/**
* @type {Map<string, Y.Doc>}
* @private
* @readonly
*/
debouncedPersistDocMap = new Map()
/**
* @type {Map<string, number>}
* @private
Expand Down Expand Up @@ -373,8 +367,7 @@ export class YSocketIO {
const nsp = this.namespaceMap.get(ns)
if (nsp?.sockets.size === 0 && stream) {
this.cleanupNamespace(ns, stream, DEFAULT_CLEAR_TIMEOUT)
const doc = this.namespaceDocMap.get(ns)
if (doc) this.debouncedPersist(ns, doc.ydoc, true)
if (this.namespaceDocMap.has(ns)) this.debouncedPersist(ns, true)
}
}
})
Expand Down Expand Up @@ -492,19 +485,17 @@ export class YSocketIO {
const shouldPersist = now - lastPersistCalledAt > MAX_PERSIST_INTERVAL
if (changed || shouldPersist || nsp.sockets.size === 0) {
this.namespacePersistentMap.set(namespace, now)
this.debouncedPersist(namespace, doc.ydoc, nsp.sockets.size === 0)
this.debouncedPersist(namespace, nsp.sockets.size === 0)
}
this.namespaceDocMap.get(namespace)?.ydoc.destroy()
this.namespaceDocMap.set(namespace, doc)
}

/**
* @param {string} namespace
* @param {Y.Doc} doc
* @param {boolean=} immediate
*/
debouncedPersist (namespace, doc, immediate = false) {
this.debouncedPersistDocMap.set(namespace, doc)
debouncedPersist (namespace, immediate = false) {
if (this.debouncedPersistMap.has(namespace)) {
if (!immediate) return
clearTimeout(this.debouncedPersistMap.get(namespace) || undefined)
Expand All @@ -514,9 +505,17 @@ export class YSocketIO {
: PERSIST_INTERVAL + (Math.random() - 0.5) * PERSIST_INTERVAL
const timeout = setTimeout(
async () => {
// wait for previous persisting operation if exists
const prev = this.awaitingPersistMap.get(namespace)
if (prev?.promise) await prev.promise
// delete persist entry to allow queueing next persisting operation
// we can delete it here because the following until awaiting persist
// are all synchronize operations
this.debouncedPersistMap.delete(namespace)

try {
assert(this.client)
const doc = this.debouncedPersistDocMap.get(namespace)
const doc = this.namespaceDocMap.get(namespace)?.ydoc
logSocketIO(`trying to persist ${namespace}`)
if (!doc) return
if (this.client.persistWorker) {
Expand All @@ -534,15 +533,14 @@ export class YSocketIO {
})
await promise
} else {
await this.client.store.persistDoc(namespace, 'index', doc)
const promise = this.client.store.persistDoc(namespace, 'index', doc)
this.awaitingPersistMap.set(namespace, { promise, resolve: () => {} })
await promise
}

await this.client.trimRoomStream(namespace, 'index')
} catch (e) {
console.error(e)
} finally {
this.debouncedPersistDocMap.delete(namespace)
this.debouncedPersistMap.delete(namespace)
}
},
timeoutInterval
Expand Down