diff --git a/src/index.js b/src/index.js index af555a23..831461bf 100644 --- a/src/index.js +++ b/src/index.js @@ -65,6 +65,8 @@ export { encodeSnapshotV2, decodeStateVector, decodeStateVectorV2, + logUpdate, + logUpdateV2, isDeleted, isParentOf, equalSnapshots, diff --git a/src/utils/updates.js b/src/utils/updates.js index e0b6733c..9fa49f8a 100644 --- a/src/utils/updates.js +++ b/src/utils/updates.js @@ -2,6 +2,7 @@ import * as binary from 'lib0/binary.js' import * as decoding from 'lib0/decoding.js' import * as encoding from 'lib0/encoding.js' +import * as logging from 'lib0/logging.js' import { createID, readItemContent, @@ -82,6 +83,26 @@ export class LazyStructReader { } } +/** + * @param {Uint8Array} update + * + */ +export const logUpdate = update => logUpdateV2(update, UpdateDecoderV1) + +/** + * @param {Uint8Array} update + * @param {typeof UpdateDecoderV2 | typeof UpdateDecoderV1} [YDecoder] + * + */ +export const logUpdateV2 = (update, YDecoder = UpdateDecoderV2) => { + const structs = [] + const lazyDecoder = new LazyStructReader(new YDecoder(decoding.createDecoder(update))) + for (let curr = lazyDecoder.curr; curr !== null; curr = lazyDecoder.next()) { + structs.push(curr) + } + logging.print(structs) +} + export class LazyStructWriter { /** * @param {UpdateEncoderV1 | UpdateEncoderV2} encoder diff --git a/tests/updates.tests.js b/tests/updates.tests.js index 108c5535..25fec654 100644 --- a/tests/updates.tests.js +++ b/tests/updates.tests.js @@ -57,7 +57,11 @@ export const testMergeUpdatesWrongOrder = tc => { ]) const targetState = Y.encodeStateAsUpdate(ydoc) + t.info('Target State: ') + Y.logUpdate(targetState) ;[wrongOrder, overlapping, separated].forEach((updates, i) => { + t.info('State $' + i + ':') + Y.logUpdate(updates) const merged = new Y.Doc() Y.applyUpdate(merged, updates) t.compareArrays(merged.getArray().toArray(), array.toArray())