From 004a781a5635ce370f1cbc157512df3a72c76d93 Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Sun, 13 Dec 2020 16:24:43 +0100 Subject: [PATCH] basic merge works. fixes first test #263 --- src/structs/Item.js | 21 +++++++++++++-------- src/utils/updates.js | 15 ++++++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/structs/Item.js b/src/structs/Item.js index 4e171224..b4c10074 100644 --- a/src/structs/Item.js +++ b/src/structs/Item.js @@ -639,16 +639,21 @@ export class Item extends AbstractStruct { } if (origin === null && rightOrigin === null) { const parent = /** @type {AbstractType} */ (this.parent) - const parentItem = parent._item - if (parentItem === null) { - // parent type on y._map - // find the correct key - const ykey = findRootTypeKey(parent) + if (parent.constructor === String) { // this edge case was added by differential updates encoder.writeParentInfo(true) // write parentYKey - encoder.writeString(ykey) + encoder.writeString(parent) } else { - encoder.writeParentInfo(false) // write parent id - encoder.writeLeftID(parentItem.id) + const parentItem = parent._item + if (parentItem === null) { + // parent type on y._map + // find the correct key + const ykey = findRootTypeKey(parent) + encoder.writeParentInfo(true) // write parentYKey + encoder.writeString(ykey) + } else { + encoder.writeParentInfo(false) // write parent id + encoder.writeLeftID(parentItem.id) + } } if (parentSub !== null) { encoder.writeString(parentSub) diff --git a/src/utils/updates.js b/src/utils/updates.js index 827eb9c2..c2285cce 100644 --- a/src/utils/updates.js +++ b/src/utils/updates.js @@ -77,7 +77,6 @@ export class LazyStructWriter { * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder */ constructor (encoder) { - this.fresh = true /** * We keep the last written struct around in case we want to * merge it with the next written struct. @@ -107,7 +106,7 @@ export class LazyStructWriter { * @param {Array} updates * @return {Uint8Array} */ -export const mergeUpdates = updates => mergeUpdatesV2(updates, UpdateDecoderV1, UpdateEncoderV2) +export const mergeUpdates = updates => mergeUpdatesV2(updates, UpdateDecoderV1, UpdateEncoderV1) /** * This method is intended to slice any kind of struct and retrieve the right part. @@ -213,6 +212,10 @@ export const mergeUpdatesV2 = (updates, YDecoder = UpdateDecoderV2, YEncoder = U currWrite = { struct: next, offset: 0 } } } + if (currWrite !== null) { + writeStructToLazyStructWriter(lazyStructEncoder, currWrite.struct, currWrite.offset) + currWrite = null + } finishLazyStructWriting(lazyStructEncoder) const dss = updateDecoders.map(decoder => readDeleteSet(decoder)) @@ -233,10 +236,10 @@ export const diffUpdate = (update, sv) => { * @param {LazyStructWriter} lazyWriter */ const flushLazyStructWriter = lazyWriter => { - if (!lazyWriter.fresh) { + if (lazyWriter.written > 0) { lazyWriter.clientStructs.push({ written: lazyWriter.written, restEncoder: encoding.toUint8Array(lazyWriter.encoder.restEncoder) }) lazyWriter.encoder.restEncoder = encoding.createEncoder() - lazyWriter.fresh = true + lazyWriter.written = 0 } } @@ -247,8 +250,10 @@ const flushLazyStructWriter = lazyWriter => { */ const writeStructToLazyStructWriter = (lazyWriter, struct, offset) => { // flush curr if we start another client - if (!lazyWriter.fresh && lazyWriter.currClient !== struct.id.client) { + if (lazyWriter.written > 0 && lazyWriter.currClient !== struct.id.client) { flushLazyStructWriter(lazyWriter) + } + if (lazyWriter.written === 0) { lazyWriter.currClient = struct.id.client // write next client lazyWriter.encoder.writeClient(struct.id.client)