From 0a5753c19142aaae0a0779375e837743df53fefa Mon Sep 17 00:00:00 2001 From: Kevin Jahns Date: Thu, 7 Mar 2019 18:57:39 +0100 Subject: [PATCH] decode items before they are decoded. fixes lots of y-array tests --- package.json | 2 +- src/utils/integrateRemoteStructs.js | 46 ++++++++++++++++++----------- tests/testHelper.js | 2 +- tests/y-array.tests.js | 4 +++ 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 1e89bdbf..90d421b4 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "module": "./dist/yjs.mjs'", "sideEffects": false, "scripts": { - "test": "npm run lint && npm run dist && node ./dist/tests.js", + "test": "npm run lint && npm run dist && node ./dist/tests.js --repitition-time 10000", "dist": "rm -rf dist examples/build && PRODUCTION=1 rollup -c", "watch": "rollup -wc", "lint": "standard", diff --git a/src/utils/integrateRemoteStructs.js b/src/utils/integrateRemoteStructs.js index 1de75a01..4b62a6d6 100644 --- a/src/utils/integrateRemoteStructs.js +++ b/src/utils/integrateRemoteStructs.js @@ -5,7 +5,6 @@ import { getStruct } from 'y-protocols/sync.js' import * as decoding from 'funlib/decoding.js' import { GC } from '../structs/GC.js' -import { Delete } from '../structs/Delete.js' import { Y } from '../utils/Y.js' // eslint-disable-line import { Item } from '../structs/Item.js' // eslint-disable-line @@ -51,18 +50,7 @@ function _integrateRemoteStructHelper (y, struct) { missingStructs.forEach(missingDef => { missingDef.missing-- if (missingDef.missing === 0) { - const decoder = missingDef.decoder - let oldPos = decoder.pos - let missing = missingDef.struct._fromBinary(y, decoder) - decoder.pos = oldPos - if (missing.length === 0) { - y._readyToIntegrate.push(missingDef.struct) - // TODO: sorting should be optimized - // Sorting because deletes may change the origin of ops - y._readyToIntegrate.sort((a, b) => a.constructor === b.constructor ? 0 : (a.constructor === Delete ? 1 : -1)) - } else { - // TODO: throw error here - } + y._readyToIntegrate.push(missingDef) } }) msu.delete(clock) @@ -88,10 +76,21 @@ export const integrateRemoteStructs = (decoder, y) => { let decoderPos = decoder.pos let missing = struct._fromBinary(y, decoder) if (missing.length === 0) { - y._readyToIntegrate.sort((a, b) => a.constructor === b.constructor ? 0 : (a.constructor === Delete ? 1 : -1)) - while (struct != null) { + while (struct !== null) { _integrateRemoteStructHelper(y, struct) - struct = y._readyToIntegrate.shift() + struct = null + if (y._readyToIntegrate.length > 0) { + const missingDef = y._readyToIntegrate.shift() + const decoder = missingDef.decoder + let oldPos = decoder.pos + let missing = missingDef.struct._fromBinary(y, decoder) + decoder.pos = oldPos + if (missing.length === 0) { + struct = missingDef.struct + } else { + throw new Error('Missing should be empty') + } + } } } else { let _decoder = decoding.createDecoder(decoder.arr.buffer) @@ -126,10 +125,21 @@ export const integrateRemoteStruct = (decoder, y) => { let decoderPos = decoder.pos let missing = struct._fromBinary(y, decoder) if (missing.length === 0) { - y._readyToIntegrate.sort((a, b) => a.constructor === b.constructor ? 0 : (a.constructor === Delete ? 1 : -1)) while (struct != null) { _integrateRemoteStructHelper(y, struct) - struct = y._readyToIntegrate.shift() + struct = null + if (y._readyToIntegrate.length > 0) { + const missingDef = y._readyToIntegrate.shift() + const decoder = missingDef.decoder + let oldPos = decoder.pos + let missing = missingDef.struct._fromBinary(y, decoder) + decoder.pos = oldPos + if (missing.length === 0) { + struct = missingDef.struct + } else { + throw new Error('Missing should be empty') + } + } } } else { let _decoder = decoding.createDecoder(decoder.arr.buffer) diff --git a/tests/testHelper.js b/tests/testHelper.js index ef439520..ff81e159 100644 --- a/tests/testHelper.js +++ b/tests/testHelper.js @@ -154,7 +154,7 @@ export class TestConnector { } const encoder = encoding.createEncoder() receiver.mMux(() => { - console.log('receive (' + sender.userID + '->' + receiver.userID + '):\n', syncProtocol.stringifySyncMessage(decoding.createDecoder(m), receiver)) + // console.log('receive (' + sender.userID + '->' + receiver.userID + '):\n', syncProtocol.stringifySyncMessage(decoding.createDecoder(m), receiver)) // do not publish data created when this function is executed (could be ss2 or update message) syncProtocol.readSyncMessage(decoding.createDecoder(m), encoder, receiver) }) diff --git a/tests/y-array.tests.js b/tests/y-array.tests.js index db3aa93f..fca47d4e 100644 --- a/tests/y-array.tests.js +++ b/tests/y-array.tests.js @@ -327,3 +327,7 @@ export const testRepeatGeneratingYarrayTests1000 = tc => { export const testRepeatGeneratingYarrayTests1800 = tc => { applyRandomTests(tc, arrayTransactions, 1800) } + +export const testRepeatGeneratingYarrayTests10000 = tc => { + applyRandomTests(tc, arrayTransactions, 10000) +}