From 69a33d728b410c349c18267c9677fad3a776478b Mon Sep 17 00:00:00 2001 From: streamich Date: Tue, 27 Feb 2024 09:28:14 +0100 Subject: [PATCH 1/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20add?= =?UTF-8?q?=20level=200=20events=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level0.ts | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/json-crdt/__demos__/events-level0.ts diff --git a/src/json-crdt/__demos__/events-level0.ts b/src/json-crdt/__demos__/events-level0.ts new file mode 100644 index 0000000000..85441d5da3 --- /dev/null +++ b/src/json-crdt/__demos__/events-level0.ts @@ -0,0 +1,49 @@ +/* tslint:disable no-console */ + +/** + * Run this demo with: + * + * npx nodemon -q -x ts-node src/json-crdt/__demos__/events-level0.ts + */ + +console.clear(); + +import {Model, PatchBuilder} from '..'; + +const model = Model.withLogicalClock(1234); // 1234 is the session ID + +model.onbeforepatch = (patch) => { + console.log('Called: onbeforepatch ' + patch); +}; + +model.onpatch = (patch) => { + console.log('Called: onpatch' + patch); +}; + +model.onbeforereset = () => { + console.log('Called: onbeforereset'); +}; + +model.onreset = () => { + console.log('Called: onreset'); +}; + +const model0 = model.clone(); + +const builder = new PatchBuilder(model.clock); +builder.root(builder.const(123)); +const patch = builder.flush(); + +console.log(model + ''); + +console.log(''); +model.applyPatch(patch); +console.log(''); + +console.log(model + ''); + +console.log(''); +model.reset(model0); +console.log(''); + +console.log(model + ''); From ca58a7c51631227422cde6cf69815abbcc73a356 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Tue, 27 Feb 2024 11:56:10 +0100 Subject: [PATCH 2/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20upd?= =?UTF-8?q?ate=20level0=20events=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level0.ts | 29 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/json-crdt/__demos__/events-level0.ts b/src/json-crdt/__demos__/events-level0.ts index 85441d5da3..9dd6fb4aff 100644 --- a/src/json-crdt/__demos__/events-level0.ts +++ b/src/json-crdt/__demos__/events-level0.ts @@ -6,44 +6,55 @@ * npx nodemon -q -x ts-node src/json-crdt/__demos__/events-level0.ts */ +// Clear terminal. console.clear(); +// Import all necessary dependencies. import {Model, PatchBuilder} from '..'; +// Create a new JSON CRDT document. const model = Model.withLogicalClock(1234); // 1234 is the session ID +// Clone the model now for future reset. +const model0 = model.clone(); + +// Attach event listeners. model.onbeforepatch = (patch) => { - console.log('Called: onbeforepatch ' + patch); + console.log('Called: "onbeforepatch" ' + patch); }; - model.onpatch = (patch) => { - console.log('Called: onpatch' + patch); + console.log('Called: "onpatch" ' + patch); }; - model.onbeforereset = () => { - console.log('Called: onbeforereset'); + console.log('Called: "onbeforereset"'); }; - model.onreset = () => { - console.log('Called: onreset'); + console.log('Called: "onreset"'); }; -const model0 = model.clone(); - +// Construct a JSON CRDT Patch which sets the document value to `123`. const builder = new PatchBuilder(model.clock); builder.root(builder.const(123)); const patch = builder.flush(); +// Print out the document state. +console.log('Document state #1:'); console.log(model + ''); +// Apply the patch to the model. console.log(''); model.applyPatch(patch); console.log(''); +// Print out the document state. +console.log('Document state #2:'); console.log(model + ''); +// Reset the model to the initial state. console.log(''); model.reset(model0); console.log(''); +// Print out the document state. +console.log('Document state #3:'); console.log(model + ''); From 0a6bf46e64da647f227fe2e4fe3db36d791e8444 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Tue, 27 Feb 2024 13:22:30 +0100 Subject: [PATCH 3/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20add?= =?UTF-8?q?=20Level=201=20events=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level1.ts | 144 +++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/json-crdt/__demos__/events-level1.ts diff --git a/src/json-crdt/__demos__/events-level1.ts b/src/json-crdt/__demos__/events-level1.ts new file mode 100644 index 0000000000..ed70b78bb6 --- /dev/null +++ b/src/json-crdt/__demos__/events-level1.ts @@ -0,0 +1,144 @@ +/* tslint:disable no-console */ + +/** + * Run this demo with: + * + * npx nodemon -q -x ts-node src/json-crdt/__demos__/events-level1.ts + */ + +// Clear terminal. +console.clear(); + +// Import all necessary dependencies. +import {Model} from '..'; +import {Patch, PatchBuilder} from '../../json-crdt-patch'; + +const subscribe = (model: Model) => { + /** + * Below we attach event listeners to all events that `model.api` supports. The + * `mode.api` object attaches to `model.on*` events as well as fires its own + * events as a result of `model.api.*` methods being called. All of that + * together can be subscribed to through `model.api.on*` event dispatchers. + */ + + // Here we subscribe to "local" events, those are events initiated by calling + // some `model.api.*` method. + model.api.onBeforeLocalChange.listen((tick: number) => { + console.log(`Called: "onBeforeLocalChange", ${tick}`); + }); + model.api.onLocalChange.listen((tick: number) => { + console.log(`Called: "onLocalChange", ${tick}`); + }); + model.api.onLocalChanges.listen((ticks: number[]) => { + console.log(`Called: "onLocalChanges", ${ticks}`); + }); + model.api.onFlush.listen((patch: Patch) => { + console.log(`Called: "onChanges"`); + }); + + // Here we subscribe to "patch" events, those are events triggered by applying + // a patch to the `model` through `model.applyPatch()` method. + model.api.onBeforePatch.listen((patch: Patch) => { + console.log(`Called: "onBeforePatch", ${patch}`); + }); + model.api.onPatch.listen((patch: Patch) => { + console.log(`Called: "onPatch", ${patch}`); + }); + + // The "change" events combine all "local" changes with the "patch" changes. + model.api.onChange.listen((change: number | void | Patch) => { + console.log(`Called: "onChange", ${change}`); + }); + model.api.onChanges.listen((changes: (number | void | Patch)[]) => { + console.log(`Called: "onChanges", ${changes}`); + }); + + // Transactions is a mechanism which allows a developer to group multiple + // operations together. + model.api.onBeforeTransaction.listen(() => { + console.log(`Called: "onBeforeTransaction"`); + }); + model.api.onTransaction.listen(() => { + console.log(`Called: "onBeforeTransaction"`); + }); + + // Here we subscribe to "reset" events, which are triggered by + // calling the `model.reset()` method. + model.api.onBeforeReset.listen(() => { + console.log(`Called: "onBeforeReset"`); + }); + model.api.onReset.listen(() => { + console.log(`Called: "onReset"`); + }); +}; + +const main = async () => { + // Create a new JSON CRDT document. + const model = Model.withLogicalClock(1234); // 1234 is the session ID + + // Clone the model now for future reset. + const model0 = model.clone(); + + // Subscribe to all Level 1 events. + subscribe(model); + + // Construct a JSON CRDT Patch which sets the document value to `123`. + const builder = new PatchBuilder(model.clock); + builder.root(builder.const(123)); + const patch = builder.flush(); + + // Print out the document state. + console.log('Document state #1:'); + console.log(model + ''); + + // Apply the patch to the model. + console.log('Executing: model.applyPatch(patch)'); + console.log(''); + model.applyPatch(patch); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document state #2:'); + console.log(model + ''); + + // Reset the model to the initial state. + console.log('Executing: model.reset(model0)'); + console.log(''); + model.reset(model0); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document state #3:'); + console.log(model + ''); + + // Execute a local change. + console.log('Executing: model.api.root(456)'); + console.log(''); + model.api.root(456); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document state #4:'); + console.log(model + ''); + + // Batch multiple operations together using a transaction. + // console.log('Executing: model.api.root(456)'); + console.log(''); + model.api.transaction(() => { + model.api.root({}); + model.api.obj([]).set({ + a: 'b', + }); + }); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document state #5:'); + console.log(model + ''); +}; + +main(); From 1d9b34d536604072c8413a3d9d4d5826b0f4b12f Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Tue, 27 Feb 2024 13:48:09 +0100 Subject: [PATCH 4/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20add?= =?UTF-8?q?=20events=20Level=202=20"obj"=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level1.ts | 2 +- src/json-crdt/__demos__/events-level2-obj.ts | 61 ++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/json-crdt/__demos__/events-level2-obj.ts diff --git a/src/json-crdt/__demos__/events-level1.ts b/src/json-crdt/__demos__/events-level1.ts index ed70b78bb6..537b063435 100644 --- a/src/json-crdt/__demos__/events-level1.ts +++ b/src/json-crdt/__demos__/events-level1.ts @@ -125,7 +125,7 @@ const main = async () => { console.log(model + ''); // Batch multiple operations together using a transaction. - // console.log('Executing: model.api.root(456)'); + console.log('Executing: model.api.transaction(() => { ... })'); console.log(''); model.api.transaction(() => { model.api.root({}); diff --git a/src/json-crdt/__demos__/events-level2-obj.ts b/src/json-crdt/__demos__/events-level2-obj.ts new file mode 100644 index 0000000000..707b98a92c --- /dev/null +++ b/src/json-crdt/__demos__/events-level2-obj.ts @@ -0,0 +1,61 @@ +/* tslint:disable no-console */ + +/** + * Run this demo with: + * + * npx nodemon -q -x ts-node src/json-crdt/__demos__/events-level2-obj.ts + */ + +// Clear terminal. +console.clear(); + +// Import all necessary dependencies. +import {Model, konst} from '..'; + +const main = async () => { + // Create a new JSON CRDT document. + const model = Model.withLogicalClock(1234); // 1234 is the session ID + + model.api.root({ + my: { + deep: { + obj: {}, + } + } + }); + + // Print out the document state. + console.log('Document state #1:'); + console.log(model.root + ''); + + model.api.obj(['my', 'deep', 'obj']).events.onChanges.listen(() => { + console.log(`Called: "onChanges"`); + }); + model.api.obj(['my', 'deep', 'obj']).events.onViewChanges.listen((view: unknown) => { + console.log(`Called: "onViewChanges"`); + }); + + // Apply changes to the deep object. + console.log('Changes which result in view change:'); + console.log(''); + model.api.obj(['my', 'deep', 'obj']).set({foo: 'bar'}); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document view #2:'); + console.log(model.root + ''); + + // Apply changes, which don't result in view change. + console.log('Changes which result in only model change:'); + console.log(''); + model.api.obj(['my', 'deep', 'obj']).set({foo: konst('bar')}); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document view #3:'); + console.log(model.root + ''); +}; + +main(); From 7e992984edb31ade0711c40ffc7b9b9b8ab21b3a Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Tue, 27 Feb 2024 15:17:56 +0100 Subject: [PATCH 5/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20add?= =?UTF-8?q?=20events=20Level=202=20arr=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level2-arr.ts | 81 ++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/json-crdt/__demos__/events-level2-arr.ts diff --git a/src/json-crdt/__demos__/events-level2-arr.ts b/src/json-crdt/__demos__/events-level2-arr.ts new file mode 100644 index 0000000000..61882237c8 --- /dev/null +++ b/src/json-crdt/__demos__/events-level2-arr.ts @@ -0,0 +1,81 @@ +/* tslint:disable no-console */ + +/** + * Run this demo with: + * + * npx nodemon -q -x ts-node src/json-crdt/__demos__/events-level2-arr.ts + */ + +// Clear terminal. +console.clear(); + +// Import all necessary dependencies. +import {Model, konst} from '..'; + +const main = async () => { + // Create a new JSON CRDT document. + const model = Model.withLogicalClock(1234); // 1234 is the session ID + + model.api.root({ + my: { + deep: { + arr: [], + } + } + }); + + // Print out the document state. + console.log('Document state #1:'); + console.log(model.root + ''); + + model.api.arr(['my', 'deep', 'arr']).events.onChanges.listen(() => { + console.log(`Called: "onChanges"`); + }); + model.api.arr(['my', 'deep', 'arr']).events.onViewChanges.listen((view: unknown) => { + console.log(`Called: "onViewChanges"`); + }); + + // Apply changes to the deep object. + console.log('Changes which result in view change:'); + console.log(''); + model.api.arr(['my', 'deep', 'arr']).ins(0, [1, 2, 3]); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document view #2:'); + console.log(model.root + ''); + + // Apply changes, which don't result in view change. + console.log('Changes which result in only model change:'); + console.log(''); + model.api.val(['my', 'deep', 'arr', 1]).set(2); + // model.api.transaction(() => { + // model.api.arr(['my', 'deep', 'arr']) + // .del(1, 1) + // .ins(1, [konst(2)]); + // }); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document view #3:'); + console.log(model.root + ''); + + // Apply more changes, which don't result in view change. + console.log('More changes which result in only model change:'); + console.log(''); + // model.api.transaction(() => { + model.api.arr(['my', 'deep', 'arr']) + .del(1, 1) + .ins(1, [konst(2)]); + // }); + await new Promise(r => setTimeout(r, 1)); + console.log(''); + + // Print out the document state. + console.log('Document view #4:'); + console.log(model.root + ''); +}; + +main(); From 312285f7b2159b082f6bc138ec7a5a48f923519c Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Tue, 27 Feb 2024 15:42:57 +0100 Subject: [PATCH 6/8] =?UTF-8?q?fix(json-crdt):=20=F0=9F=90=9B=20do=20not?= =?UTF-8?q?=20fire=20view=20events=20when=20arr=20node=20view=20does=20not?= =?UTF-8?q?=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/model/api/NodeEvents.ts | 2 +- .../model/api/__tests__/ArrayApi.spec.ts | 39 +++++++++++++++++++ src/json-crdt/model/api/fanout.ts | 3 +- src/json-crdt/nodes/arr/ArrNode.ts | 15 +++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/json-crdt/model/api/NodeEvents.ts b/src/json-crdt/model/api/NodeEvents.ts index c82d1f5d39..f86ffa617f 100644 --- a/src/json-crdt/model/api/NodeEvents.ts +++ b/src/json-crdt/model/api/NodeEvents.ts @@ -28,7 +28,7 @@ export class NodeEvents implements SyncStore) { this.onChanges = new MapFanOut(this.api.api.onChanges, this.getSnapshot); - this.onViewChanges = new OnNewFanOut(this.onChanges); + this.onViewChanges = new OnNewFanOut(this.onChanges, this.api.view()); } /** diff --git a/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts b/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts index a9dc4b88fa..f3d673002b 100644 --- a/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts +++ b/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts @@ -1,3 +1,4 @@ +import {konst} from '../../../../json-crdt-patch'; import {Model} from '../../Model'; test('can insert a value and delete all previous ones', () => { @@ -21,3 +22,41 @@ test('.length()', () => { const arr = doc.api.arr(['arr']); expect(arr.length()).toBe(3); }); + +describe('events', () => { + test('fires onViewChanges event on change', async () => { + const doc = Model.withLogicalClock(); + doc.api.root({ + myArr: [1, 2, 3], + }); + const events: any[] = []; + doc.api.arr(['myArr']).events.onViewChanges.listen(data => { + events.push(data); + }); + expect(events.length).toBe(0); + doc.api.arr(['myArr']).del(1, 1); + await new Promise(r => setTimeout(r, 1)); + expect(events.length).toBe(1); + }); + + test('does not fire onViewChanges event when resulting view is the same', async () => { + const doc = Model.withLogicalClock(); + doc.api.root({ + myArr: [1, 2, 3], + }); + const events: any[] = []; + doc.api.arr(['myArr']).events.onViewChanges.listen(data => { + events.push(data); + }); + await new Promise(r => setTimeout(r, 1)); + expect(events.length).toBe(0); + doc.api.arr(['myArr']).del(1, 1); + doc.api.arr(['myArr']).ins(1, [konst(2)]); + await new Promise(r => setTimeout(r, 1)); + expect(events.length).toBe(0); + doc.api.arr(['myArr']).del(1, 1); + doc.api.arr(['myArr']).ins(1, [2]); + await new Promise(r => setTimeout(r, 1)); + expect(events.length).toBe(0); + }); +}); \ No newline at end of file diff --git a/src/json-crdt/model/api/fanout.ts b/src/json-crdt/model/api/fanout.ts index 0f48f1c9d8..6b2cdaed81 100644 --- a/src/json-crdt/model/api/fanout.ts +++ b/src/json-crdt/model/api/fanout.ts @@ -94,10 +94,9 @@ export class MapFanOut extends FanOut { * emitted immediately. */ export class OnNewFanOut extends FanOut { - private last: D | undefined = undefined; private unsub?: FanOutUnsubscribe = undefined; - constructor(private readonly source: FanOut) { + constructor(private readonly source: FanOut, private last: D | undefined = undefined) { super(); } diff --git a/src/json-crdt/nodes/arr/ArrNode.ts b/src/json-crdt/nodes/arr/ArrNode.ts index fd06fc4644..9897414f1d 100644 --- a/src/json-crdt/nodes/arr/ArrNode.ts +++ b/src/json-crdt/nodes/arr/ArrNode.ts @@ -8,8 +8,6 @@ import type {Printable} from '../../../util/print/types'; type E = ITimestampStruct; -const Empty = [] as any[]; - /** * @ignore * @category CRDT Node @@ -120,9 +118,7 @@ export class ArrNode } /** @ignore */ - protected onChange(): void { - this._view = Empty as any; - } + protected onChange(): void {} protected toStringName(): string { return this.name(); @@ -143,13 +139,13 @@ export class ArrNode /** @ignore */ private _tick: number = 0; /** @ignore */ - private _view = Empty; + private _view: unknown[] = []; public view(): JsonNodeView[] { const doc = this.doc; const tick = doc.clock.time + doc.tick; const _view = this._view; - if (this._tick === tick) return _view; - const view = [] as JsonNodeView[]; + if (this._tick === tick) return _view as JsonNodeView[]; + const view = [] as unknown[]; const index = doc.index; let useCache = true; for (let chunk = this.first(); chunk; chunk = this.next(chunk)) { @@ -166,7 +162,8 @@ export class ArrNode } } if (_view.length !== view.length) useCache = false; - return useCache ? _view : ((this._tick = tick), (this._view = view)); + const result = useCache ? _view : ((this._tick = tick), (this._view = view)); + return result as JsonNodeView[]; } /** @ignore */ From ef7ddec3762395d8fef23f0014eb2589ae9d737c Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Thu, 29 Feb 2024 11:01:02 +0100 Subject: [PATCH 7/8] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20fix?= =?UTF-8?q?=20typos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level1.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/json-crdt/__demos__/events-level1.ts b/src/json-crdt/__demos__/events-level1.ts index 537b063435..ff4e33950d 100644 --- a/src/json-crdt/__demos__/events-level1.ts +++ b/src/json-crdt/__demos__/events-level1.ts @@ -33,7 +33,7 @@ const subscribe = (model: Model) => { console.log(`Called: "onLocalChanges", ${ticks}`); }); model.api.onFlush.listen((patch: Patch) => { - console.log(`Called: "onChanges"`); + console.log(`Called: "onFlush"`); }); // Here we subscribe to "patch" events, those are events triggered by applying @@ -59,7 +59,7 @@ const subscribe = (model: Model) => { console.log(`Called: "onBeforeTransaction"`); }); model.api.onTransaction.listen(() => { - console.log(`Called: "onBeforeTransaction"`); + console.log(`Called: "onTransaction"`); }); // Here we subscribe to "reset" events, which are triggered by From b7269d8934731850a9c503919595b9b69c033437 Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Thu, 29 Feb 2024 11:03:46 +0100 Subject: [PATCH 8/8] =?UTF-8?q?style(json-crdt):=20=F0=9F=92=84=20run=20Pr?= =?UTF-8?q?ettier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/__demos__/events-level1.ts | 8 ++++---- src/json-crdt/__demos__/events-level2-arr.ts | 17 +++++++++-------- src/json-crdt/__demos__/events-level2-obj.ts | 8 ++++---- .../model/api/__tests__/ArrayApi.spec.ts | 14 +++++++------- 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/json-crdt/__demos__/events-level1.ts b/src/json-crdt/__demos__/events-level1.ts index ff4e33950d..420e540d4c 100644 --- a/src/json-crdt/__demos__/events-level1.ts +++ b/src/json-crdt/__demos__/events-level1.ts @@ -95,7 +95,7 @@ const main = async () => { console.log('Executing: model.applyPatch(patch)'); console.log(''); model.applyPatch(patch); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -106,7 +106,7 @@ const main = async () => { console.log('Executing: model.reset(model0)'); console.log(''); model.reset(model0); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -117,7 +117,7 @@ const main = async () => { console.log('Executing: model.api.root(456)'); console.log(''); model.api.root(456); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -133,7 +133,7 @@ const main = async () => { a: 'b', }); }); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. diff --git a/src/json-crdt/__demos__/events-level2-arr.ts b/src/json-crdt/__demos__/events-level2-arr.ts index 61882237c8..755a9753f1 100644 --- a/src/json-crdt/__demos__/events-level2-arr.ts +++ b/src/json-crdt/__demos__/events-level2-arr.ts @@ -20,8 +20,8 @@ const main = async () => { my: { deep: { arr: [], - } - } + }, + }, }); // Print out the document state. @@ -39,7 +39,7 @@ const main = async () => { console.log('Changes which result in view change:'); console.log(''); model.api.arr(['my', 'deep', 'arr']).ins(0, [1, 2, 3]); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -55,7 +55,7 @@ const main = async () => { // .del(1, 1) // .ins(1, [konst(2)]); // }); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -66,11 +66,12 @@ const main = async () => { console.log('More changes which result in only model change:'); console.log(''); // model.api.transaction(() => { - model.api.arr(['my', 'deep', 'arr']) - .del(1, 1) - .ins(1, [konst(2)]); + model.api + .arr(['my', 'deep', 'arr']) + .del(1, 1) + .ins(1, [konst(2)]); // }); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. diff --git a/src/json-crdt/__demos__/events-level2-obj.ts b/src/json-crdt/__demos__/events-level2-obj.ts index 707b98a92c..e0f43fd9b0 100644 --- a/src/json-crdt/__demos__/events-level2-obj.ts +++ b/src/json-crdt/__demos__/events-level2-obj.ts @@ -20,8 +20,8 @@ const main = async () => { my: { deep: { obj: {}, - } - } + }, + }, }); // Print out the document state. @@ -39,7 +39,7 @@ const main = async () => { console.log('Changes which result in view change:'); console.log(''); model.api.obj(['my', 'deep', 'obj']).set({foo: 'bar'}); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. @@ -50,7 +50,7 @@ const main = async () => { console.log('Changes which result in only model change:'); console.log(''); model.api.obj(['my', 'deep', 'obj']).set({foo: konst('bar')}); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); console.log(''); // Print out the document state. diff --git a/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts b/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts index f3d673002b..570081f5f9 100644 --- a/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts +++ b/src/json-crdt/model/api/__tests__/ArrayApi.spec.ts @@ -30,12 +30,12 @@ describe('events', () => { myArr: [1, 2, 3], }); const events: any[] = []; - doc.api.arr(['myArr']).events.onViewChanges.listen(data => { + doc.api.arr(['myArr']).events.onViewChanges.listen((data) => { events.push(data); }); expect(events.length).toBe(0); doc.api.arr(['myArr']).del(1, 1); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); expect(events.length).toBe(1); }); @@ -45,18 +45,18 @@ describe('events', () => { myArr: [1, 2, 3], }); const events: any[] = []; - doc.api.arr(['myArr']).events.onViewChanges.listen(data => { + doc.api.arr(['myArr']).events.onViewChanges.listen((data) => { events.push(data); }); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); expect(events.length).toBe(0); doc.api.arr(['myArr']).del(1, 1); doc.api.arr(['myArr']).ins(1, [konst(2)]); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); expect(events.length).toBe(0); doc.api.arr(['myArr']).del(1, 1); doc.api.arr(['myArr']).ins(1, [2]); - await new Promise(r => setTimeout(r, 1)); + await new Promise((r) => setTimeout(r, 1)); expect(events.length).toBe(0); }); -}); \ No newline at end of file +});