From fbb9a81db935e1c7730aa26966fe3fa3e70d2a9e Mon Sep 17 00:00:00 2001 From: streamich Date: Tue, 26 Mar 2024 11:13:27 +0100 Subject: [PATCH] =?UTF-8?q?refactor(json-crdt):=20=F0=9F=92=A1=20fix=20Fil?= =?UTF-8?q?e=20class=20after=20refacator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/file/File.ts | 19 +++++++++---------- src/json-crdt/file/__tests__/File.spec.ts | 10 +++++----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/json-crdt/file/File.ts b/src/json-crdt/file/File.ts index decd989802..15dd480931 100644 --- a/src/json-crdt/file/File.ts +++ b/src/json-crdt/file/File.ts @@ -46,9 +46,8 @@ export class File implements Printable { if (history) { const [start, patches] = history; if (start) { - const startModel = decodeModel(start); - log = new PatchLog(startModel); - for (const patch of patches) log.push(decodePatch(patch)); + log = new PatchLog(() => decodeModel(start)); + for (const patch of patches) log.end.applyPatch(decodePatch(patch)); } } if (!log) throw new Error('NO_HISTORY'); @@ -57,7 +56,7 @@ export class File implements Printable { for (const patch of frontier) { const patchDecoded = decodePatch(patch); decodedModel.applyPatch(patchDecoded); - log.push(patchDecoded); + log.end.applyPatch(patchDecoded); } } const file = new File(decodedModel, log); @@ -88,7 +87,7 @@ export class File implements Printable { const id = patch.getId(); if (!id) return; this.model.applyPatch(patch); - this.log.push(patch); + this.log.end.applyPatch(patch); } /** @@ -100,10 +99,10 @@ export class File implements Printable { const api = model.api; const autoflushUnsubscribe = api.autoFlush(); const onPatchUnsubscribe = api.onPatch.listen((patch) => { - log.push(patch); + log.end.applyPatch(patch); }); const onFlushUnsubscribe = api.onFlush.listen((patch) => { - log.push(patch); + log.end.applyPatch(patch); }); return () => { autoflushUnsubscribe(); @@ -153,7 +152,7 @@ export class File implements Printable { const patchFormat = params.history ?? 'binary'; switch (patchFormat) { case 'binary': { - history[0] = this.log.start.toBinary(); + history[0] = this.log.start().toBinary(); this.log.patches.forEach(({v}) => { history[1].push(v.toBinary()); }); @@ -162,7 +161,7 @@ export class File implements Printable { case 'compact': { const encoder = this.options.structuralCompactEncoder; if (!encoder) throw new Error('NO_COMPACT_ENCODER'); - history[0] = encoder.encode(this.log.start); + history[0] = encoder.encode(this.log.start()); const encodeCompact = this.options.patchCompactEncoder; if (!encodeCompact) throw new Error('NO_COMPACT_PATCH_ENCODER'); const list = history[1]; @@ -174,7 +173,7 @@ export class File implements Printable { case 'verbose': { const encoder = this.options.structuralVerboseEncoder; if (!encoder) throw new Error('NO_VERBOSE_ENCODER'); - history[0] = encoder.encode(this.log.start); + history[0] = encoder.encode(this.log.start()); const encodeVerbose = this.options.patchVerboseEncoder; if (!encodeVerbose) throw new Error('NO_VERBOSE_PATCH_ENCODER'); const list = history[1]; diff --git a/src/json-crdt/file/__tests__/File.spec.ts b/src/json-crdt/file/__tests__/File.spec.ts index c97bcc7a51..7dd2015809 100644 --- a/src/json-crdt/file/__tests__/File.spec.ts +++ b/src/json-crdt/file/__tests__/File.spec.ts @@ -20,11 +20,11 @@ test('can create File from new model', () => { }), ); const file = File.fromModel(model); - expect(file.log.start.view()).toBe(undefined); + expect(file.log.start().view()).toBe(undefined); expect(file.model.view()).toEqual({ foo: 'bar', }); - expect(file.log.start.clock.sid).toBe(file.model.clock.sid); + expect(file.log.start().clock.sid).toBe(file.model.clock.sid); }); test.todo('patches are flushed and stored in memory'); @@ -56,7 +56,7 @@ describe('.toBinary()', () => { const file2 = File.fromNdjson(blob); expect(file2.model.view()).toEqual({foo: 'bar'}); expect(file2.model !== file.model).toBe(true); - expect(file.log.start.view()).toEqual(undefined); + expect(file.log.start().view()).toEqual(undefined); expect(file.log.replayToEnd().view()).toEqual({foo: 'bar'}); }); @@ -66,7 +66,7 @@ describe('.toBinary()', () => { const file2 = File.fromSeqCbor(blob); expect(file2.model.view()).toEqual({foo: 'bar'}); expect(file2.model !== file.model).toBe(true); - expect(file.log.start.view()).toEqual(undefined); + expect(file.log.start().view()).toEqual(undefined); expect(file.log.replayToEnd().view()).toEqual({foo: 'bar'}); }); }); @@ -78,7 +78,7 @@ describe('.toBinary()', () => { params.format === 'seq.cbor' ? File.fromSeqCbor(blob, fileEncoders) : File.fromNdjson(blob, fileEncoders); expect(file2.model.view()).toEqual(file.model.view()); expect(file2.model !== file.model).toBe(true); - expect(file2.log.start.view()).toEqual(undefined); + expect(file2.log.start().view()).toEqual(undefined); expect(file2.log.replayToEnd().view()).toEqual(file.model.view()); expect(file2.log.patches.size()).toBe(file.log.patches.size()); };