From 9ff1f1c5af0f9ce81492f350933dcd48fe060c9a Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Fri, 23 Sep 2022 09:54:36 +0200 Subject: [PATCH] refactor: extract out getStructsToWrite --- src/utils/encoding.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/utils/encoding.js b/src/utils/encoding.js index 29af8bc6..354a519c 100644 --- a/src/utils/encoding.js +++ b/src/utils/encoding.js @@ -46,6 +46,18 @@ import * as binary from 'lib0/binary' import * as map from 'lib0/map' import * as math from 'lib0/math' +/** + * @param {Array} structs All structs by `client` + * @param {number} minClock write structs starting with `ID(client,minClock)` + * + * @function + */ +const getStructsToWrite = (structs, minClock) => { + minClock = math.max(minClock, structs[0].id.clock) // make sure the first id exists + const startNewStructs = findIndexSS(structs, minClock) + return structs.slice(startNewStructs) +} + /** * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder * @param {Array} structs All structs by `client` @@ -57,16 +69,16 @@ import * as math from 'lib0/math' const writeStructs = (encoder, structs, client, clock) => { // write first id clock = math.max(clock, structs[0].id.clock) // make sure the first id exists - const startNewStructs = findIndexSS(structs, clock) + const newStructs = getStructsToWrite(structs, clock, maxClock) // write # encoded structs - encoding.writeVarUint(encoder.restEncoder, structs.length - startNewStructs) + encoding.writeVarUint(encoder.restEncoder, newStructs.length) encoder.writeClient(client) encoding.writeVarUint(encoder.restEncoder, clock) - const firstStruct = structs[startNewStructs] + const firstStruct = newStructs[0] // write first struct with an offset firstStruct.write(encoder, clock - firstStruct.id.clock) - for (let i = startNewStructs + 1; i < structs.length; i++) { - structs[i].write(encoder, 0) + for (let i = 1; i < newStructs.length; i++) { + newStructs[i].write(encoder, 0) } }