Skip to content

Commit 8521ccb

Browse files
authored
Merge pull request #20 from hackmdio/fix/only-call-persist-if-doc-has-changed
fix/only call persistDoc if ydoc has changes
2 parents cbd2657 + 9509041 commit 8521ccb

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/api.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ export class Api {
235235
const awareness = new awarenessProtocol.Awareness(ydoc)
236236
awareness.setLocalState(null) // we don't want to propagate awareness state
237237
const now = performance.now()
238+
if (docstate) { Y.applyUpdateV2(ydoc, docstate.doc) }
239+
let changed = false
240+
ydoc.once('afterTransaction', (tr) => { changed = tr.changed.size > 0 })
238241
ydoc.transact(() => {
239-
if (docstate) { Y.applyUpdateV2(ydoc, docstate.doc) }
240242
docMessages?.messages.forEach(m => {
241243
const decoder = decoding.createDecoder(m)
242244
switch (decoding.readVarUint(decoder)) {
@@ -254,7 +256,13 @@ export class Api {
254256
})
255257
})
256258
logApi(`took ${performance.now() - now}ms to process messages for room: ${room}`)
257-
return { ydoc, awareness, redisLastId: docMessages?.lastId.toString() || '0', storeReferences: docstate?.references || null }
259+
return {
260+
ydoc,
261+
awareness,
262+
redisLastId: docMessages?.lastId.toString() || '0',
263+
storeReferences: docstate?.references || null,
264+
changed
265+
}
258266
}
259267

260268
/**
@@ -299,11 +307,14 @@ export class Api {
299307
} else {
300308
reclaimCounts++
301309
const { room, docid } = decodeRedisRoomStreamName(task.stream, this.prefix)
302-
const { ydoc, storeReferences, redisLastId } = await this.getDoc(room, docid)
310+
const { ydoc, storeReferences, redisLastId, changed } = await this.getDoc(room, docid)
303311
const lastId = math.max(number.parseInt(redisLastId.split('-')[0]), number.parseInt(task.id.split('-')[0]))
304-
await this.store.persistDoc(room, docid, ydoc)
312+
if (changed) {
313+
logWorker(`persisting changes in room: ${room}`)
314+
await this.store.persistDoc(room, docid, ydoc)
315+
} else logWorker(`skip persisting room: ${room} due to no changes`)
305316
await promise.all([
306-
storeReferences ? this.store.deleteReferences(room, docid, storeReferences) : promise.resolve(),
317+
storeReferences && changed ? this.store.deleteReferences(room, docid, storeReferences) : promise.resolve(),
307318
this.redis.multi()
308319
.xTrim(task.stream, 'MINID', lastId - this.redisMinMessageLifetime)
309320
.xAdd(this.redisWorkerStreamName, '*', { compact: task.stream })

tests/api.tests.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const testWorker = async tc => {
7575
let streamexists = true
7676
while (streamexists) {
7777
streamexists = (await client.redis.exists(stream)) === 1
78+
await promise.wait(10)
7879
}
7980
const { ydoc: loadedDoc } = await client.getDoc(room, docid)
8081
t.assert(loadedDoc.getMap().get('key1') === 'val1')

0 commit comments

Comments
 (0)