basic merge works. fixes first test #263

This commit is contained in:
Kevin Jahns 2020-12-13 16:24:43 +01:00
parent c8534ea6bc
commit 004a781a56
2 changed files with 23 additions and 13 deletions

View File

@ -639,6 +639,10 @@ export class Item extends AbstractStruct {
}
if (origin === null && rightOrigin === null) {
const parent = /** @type {AbstractType<any>} */ (this.parent)
if (parent.constructor === String) { // this edge case was added by differential updates
encoder.writeParentInfo(true) // write parentYKey
encoder.writeString(parent)
} else {
const parentItem = parent._item
if (parentItem === null) {
// parent type on y._map
@ -650,6 +654,7 @@ export class Item extends AbstractStruct {
encoder.writeParentInfo(false) // write parent id
encoder.writeLeftID(parentItem.id)
}
}
if (parentSub !== null) {
encoder.writeString(parentSub)
}

View File

@ -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<Uint8Array>} 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)