From 058e93d191deb59dd5ea9d11184bbb74ddef7f1f Mon Sep 17 00:00:00 2001 From: Siddhartha Date: Sat, 25 Nov 2023 13:23:10 -0800 Subject: [PATCH] (bug): Check if structs are empty before findIndexSS and writing encoding. --- src/utils/Snapshot.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/utils/Snapshot.js b/src/utils/Snapshot.js index dfd82c86..5624bf16 100644 --- a/src/utils/Snapshot.js +++ b/src/utils/Snapshot.js @@ -194,14 +194,16 @@ export const createDocFromSnapshot = (originDoc, snapshot, newDoc = new Doc()) = getItemCleanStart(transaction, createID(client, clock)) } const structs = originDoc.store.clients.get(client) || [] - const lastStructIndex = findIndexSS(structs, clock - 1) - // write # encoded structs - encoding.writeVarUint(encoder.restEncoder, lastStructIndex + 1) - encoder.writeClient(client) - // first clock written is 0 - encoding.writeVarUint(encoder.restEncoder, 0) - for (let i = 0; i <= lastStructIndex; i++) { - structs[i].write(encoder, 0) + if(structs.length != 0) { + const lastStructIndex = findIndexSS(structs, clock - 1) + // write # encoded structs + encoding.writeVarUint(encoder.restEncoder, lastStructIndex + 1) + encoder.writeClient(client) + // first clock written is 0 + encoding.writeVarUint(encoder.restEncoder, 0) + for (let i = 0; i <= lastStructIndex; i++) { + structs[i].write(encoder, 0) + } } } writeDeleteSet(encoder, ds)