Skip to content

Commit da3e114

Browse files
committed
fix: trim after persist
1 parent 4495e1c commit da3e114

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"test-db": "docker-compose -f ./docker-compose.test.yaml up",
1616
"dist": "tsup",
1717
"lint": "standard && tsc",
18+
"lint:fix": "standard --fix && tsc",
1819
"test": "dotenvx run --env-file=.env -- node tests/index.js",
1920
"test-inspect": "dotenvx run --env-file=.env -- node --inspect-brk tests/index.js",
2021
"preversion": "npm run lint && npm run dist",

src/y-socket-io/y-socket-io.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const logSocketIO = createModuleLogger('@y/socket-io/server')
1818
const PERSIST_INTERVAL = number.parseInt(env.getConf('y-socket-io-server-persist-interval') || '3000')
1919
const REVALIDATE_TIMEOUT = number.parseInt(env.getConf('y-socket-io-server-revalidate-timeout') || '60000')
2020

21-
process.on('SIGINT', function() {
21+
process.on('SIGINT', function () {
2222
// calling .shutdown allows your process to exit normally
23-
toobusy.shutdown();
24-
process.exit();
25-
});
23+
toobusy.shutdown()
24+
process.exit()
25+
})
2626

2727
/**
2828
* @typedef {import('socket.io').Namespace} Namespace
@@ -411,8 +411,12 @@ export class YSocketIO {
411411
changed = tr.changed.size > 0
412412
})
413413
Y.transact(existDoc.ydoc, () => {
414-
for (const msg of updates) Y.applyUpdate(existDoc.ydoc, msg)
414+
for (const msg of updates) {
415+
if (msg.length === 0) continue
416+
Y.applyUpdate(existDoc.ydoc, msg)
417+
}
415418
for (const msg of awareness) {
419+
if (msg.length === 0) continue
416420
AwarenessProtocol.applyAwarenessUpdate(existDoc.awareness, msg, null)
417421
}
418422
})
@@ -426,10 +430,9 @@ export class YSocketIO {
426430
changed = getDoc.changed
427431
}
428432
assert(doc)
429-
this.debouncedPersist(namespace, doc.ydoc)
433+
if (changed) this.debouncedPersist(namespace, doc.ydoc)
430434
this.namespaceDocMap.get(namespace)?.ydoc.destroy()
431435
this.namespaceDocMap.set(namespace, doc)
432-
await this.client.trimRoomStream(namespace, 'index', nsp.sockets.size === 0)
433436
}
434437

435438
/**
@@ -456,6 +459,7 @@ export class YSocketIO {
456459
const doc = this.debouncedPersistDocMap.get(namespace)
457460
if (!doc) return
458461
await this.client.store.persistDoc(namespace, 'index', doc)
462+
await this.client.trimRoomStream(namespace, 'index', true)
459463
this.debouncedPersistDocMap.delete(namespace)
460464
this.debouncedPersistMap.delete(namespace)
461465
},

0 commit comments

Comments
 (0)