From 443335fa73c57e9ca4b0cd13931799e030872892 Mon Sep 17 00:00:00 2001 From: Bartosz Sypytkowski Date: Thu, 2 May 2024 08:10:04 +0200 Subject: [PATCH 1/2] missing state vector should be usable for update encoding --- tests/doc.tests.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/doc.tests.js b/tests/doc.tests.js index bb94819d..c5df310b 100644 --- a/tests/doc.tests.js +++ b/tests/doc.tests.js @@ -327,3 +327,45 @@ export const testSyncDocsEvent = async _tc => { t.assert(!ydoc.isSynced) t.assert(ydoc.whenSynced !== oldWhenSynced) } + +/** + * @param {t.TestCase} tc + */ +export const testMissingStateVector = tc => { + const d1 = new Y.Doc() + d1.clientID = 1 + const m1 = d1.getMap('test') + /** @type {any[]} */ + const updates = [] + d1.on('update', (update) => { + updates.push(update) + }) + m1.set('a', '1') + m1.set('b', '2') + m1.set('c', '3') + m1.set('d', '4') + m1.set('e', '5') + + // we only apply second half of the updates + const second = updates.slice(3) + + const d2 = new Y.Doc() + d2.clientID = 2 + const m2 = d2.getMap('test') + + for (let u of second) { + Y.applyUpdate(d2, u) + } + let json = m2.toJSON() + t.compare(json, {}) + + // a missing state vector should mark the beginning of missing updates + const missingSV = /** @type {Map} */ (d2.store.pendingStructs?.missing) + //t.compare(missingSV, new Map([[1, 0]])) + + // exchange all updates from missing state vector onwards + const update = Y.encodeStateAsUpdate(d1, Y.encodeStateVector(missingSV)) + Y.applyUpdate(d2, update) + json = m2.toJSON() + t.compare(json, {'a':'1','b':'2','c':'3','d':'4','e':'5'}) +} \ No newline at end of file From 27299f7746a69dbc59354cd027069c502cdaddf2 Mon Sep 17 00:00:00 2001 From: Bartosz Sypytkowski Date: Thu, 2 May 2024 08:17:06 +0200 Subject: [PATCH 2/2] fix: initialize missing state vector with 0 for new client --- src/utils/encoding.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/encoding.js b/src/utils/encoding.js index 08a9602d..1d84d495 100644 --- a/src/utils/encoding.js +++ b/src/utils/encoding.js @@ -265,7 +265,10 @@ const integrateStructs = (transaction, store, clientsStructRefs) => { */ const updateMissingSv = (client, clock) => { const mclock = missingSV.get(client) - if (mclock == null || mclock > clock) { + if (mclock === undefined) { + // we never seen that client before + missingSV.set(client, 0) + } else if (mclock > clock) { missingSV.set(client, clock) } }