From bcb84a1383bdf2d39e6438e5a8be3fb2d698ff9b Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Apr 2024 14:23:05 +0200 Subject: [PATCH 1/3] =?UTF-8?q?feat(json-crdt):=20=F0=9F=8E=B8=20add=20typ?= =?UTF-8?q?ing=20support=20for=20Log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/log/Log.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/json-crdt/log/Log.ts b/src/json-crdt/log/Log.ts index 6267ec6cba..aff98b8805 100644 --- a/src/json-crdt/log/Log.ts +++ b/src/json-crdt/log/Log.ts @@ -5,8 +5,9 @@ import {AvlMap} from 'sonic-forest/lib/avl/AvlMap'; import {Model} from '../model'; import {first, next} from 'sonic-forest/lib/util'; import type {Printable} from '../../util/print/types'; +import type {JsonNode} from '../nodes/types'; -export class Log implements Printable { +export class Log> implements Printable { /** * Creates a `PatchLog` instance from a newly JSON CRDT model. Checks if * the model API buffer has any initial operations applied, if yes, it @@ -16,9 +17,9 @@ export class Log implements Printable { * `Model.withLogicalClock()` or `Model.withServerClock()`. * @returns A new `PatchLog` instance. */ - public static fromNewModel(model: Model): Log { + public static fromNewModel>(model: Model): Log { const clock = model.clock.clone(); - const log = new Log(() => new Model(clock)); + const log = new Log(() => new Model(clock)); const api = model.api; if (api.builder.patch.ops.length) log.end.applyPatch(api.flush()); return log; @@ -32,7 +33,7 @@ export class Log implements Printable { * @readonly Internally this function may be updated, but externally it is * read-only. */ - public start: () => Model; + public start: () => Model; /** * The end of the log, the current state of the document. It is the model @@ -40,7 +41,7 @@ export class Log implements Printable { * * @readonly */ - public readonly end: Model; + public readonly end: Model; /** * The collection of patches which are applied to the `start()` model to reach @@ -55,7 +56,7 @@ export class Log implements Printable { private __onPatch: FanOutUnsubscribe; private __onFlush: FanOutUnsubscribe; - constructor(start: () => Model) { + constructor(start: () => Model) { this.start = start; const end = (this.end = start()); const onPatch = (patch: Patch) => { @@ -84,7 +85,7 @@ export class Log implements Printable { * * @returns A new model instance with all patches replayed. */ - public replayToEnd(): Model { + public replayToEnd(): Model { const clone = this.start().clone(); for (let node = first(this.patches.root); node; node = next(node)) clone.applyPatch(node.v); return clone; @@ -98,7 +99,7 @@ export class Log implements Printable { * @param ts Timestamp ID of the patch to replay to. * @returns A new model instance with patches replayed up to the given timestamp. */ - public replayTo(ts: ITimestampStruct): Model { + public replayTo(ts: ITimestampStruct): Model { const clone = this.start().clone(); for (let node = first(this.patches.root); node && compare(ts, node.k) >= 0; node = next(node)) clone.applyPatch(node.v); @@ -119,7 +120,7 @@ export class Log implements Printable { for (; node && compare(ts, node.k) >= 0; node = next(node)) newStartPatches.push(node.v); for (const patch of newStartPatches) this.patches.del(patch.getId()!); const oldStart = this.start; - this.start = (): Model => { + this.start = (): Model => { const model = oldStart(); for (const patch of newStartPatches) model.applyPatch(patch); return model; From 464951731211ceac07083d90551756c80b050d91 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Apr 2024 14:26:11 +0200 Subject: [PATCH 2/3] =?UTF-8?q?docs(json-crdt):=20=E2=9C=8F=EF=B8=8F=20add?= =?UTF-8?q?=20Log=20JSDoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json-crdt/log/Log.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/json-crdt/log/Log.ts b/src/json-crdt/log/Log.ts index aff98b8805..388daf235d 100644 --- a/src/json-crdt/log/Log.ts +++ b/src/json-crdt/log/Log.ts @@ -7,6 +7,16 @@ import {first, next} from 'sonic-forest/lib/util'; import type {Printable} from '../../util/print/types'; import type {JsonNode} from '../nodes/types'; +/** + * The `Log` represents a history of patches applied to a JSON CRDT model. It + * consists of: (1) a starting {@link Model} instance, (2) a list of {@link Patch} instances, + * that can be applied to the starting model to reach the current state of the + * document, and (3) the current state of the document, the `end` {@link Model}. + * + * The log can be used to replay the history of patches to any point in time, + * from the "start" to the "end" of the log, and return the resulting {@link Model} + * state. + */ export class Log> implements Printable { /** * Creates a `PatchLog` instance from a newly JSON CRDT model. Checks if From 6ae8b3061ce6855cadbc29b3fc2e4034bc383030 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 29 Apr 2024 14:32:04 +0200 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=F0=9F=A4=96=20improve=20Prettier?= =?UTF-8?q?=20ignore=20paths?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierignore | 1 + package.json | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000000..ac1d11ddd2 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +/CHANGELOG* diff --git a/package.json b/package.json index 1d9f545148..e9ba205d09 100644 --- a/package.json +++ b/package.json @@ -76,8 +76,8 @@ "lib/" ], "scripts": { - "prettier": "npx prettier@3.2.5 --ignore-path .gitignore --write .", - "prettier:check": "npx prettier@3.2.5 --ignore-path .gitignore --check .", + "prettier": "npx prettier@3.2.5 --write .", + "prettier:check": "npx prettier@3.2.5 --check .", "lint": "yarn tslint", "lint:fix": "yarn tslint --fix", "tslint": "tslint 'src/**/*.{js,jsx,ts,tsx}' -t verbose --project .",