Skip to content

Commit bcb84a1

Browse files
committed
feat(json-crdt): 🎸 add typing support for Log
1 parent dbdb619 commit bcb84a1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/json-crdt/log/Log.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {AvlMap} from 'sonic-forest/lib/avl/AvlMap';
55
import {Model} from '../model';
66
import {first, next} from 'sonic-forest/lib/util';
77
import type {Printable} from '../../util/print/types';
8+
import type {JsonNode} from '../nodes/types';
89

9-
export class Log implements Printable {
10+
export class Log<N extends JsonNode = JsonNode<any>> implements Printable {
1011
/**
1112
* Creates a `PatchLog` instance from a newly JSON CRDT model. Checks if
1213
* the model API buffer has any initial operations applied, if yes, it
@@ -16,9 +17,9 @@ export class Log implements Printable {
1617
* `Model.withLogicalClock()` or `Model.withServerClock()`.
1718
* @returns A new `PatchLog` instance.
1819
*/
19-
public static fromNewModel(model: Model<any>): Log {
20+
public static fromNewModel<N extends JsonNode = JsonNode<any>>(model: Model<N>): Log<N> {
2021
const clock = model.clock.clone();
21-
const log = new Log(() => new Model(clock));
22+
const log = new Log<N>(() => new Model(clock));
2223
const api = model.api;
2324
if (api.builder.patch.ops.length) log.end.applyPatch(api.flush());
2425
return log;
@@ -32,15 +33,15 @@ export class Log implements Printable {
3233
* @readonly Internally this function may be updated, but externally it is
3334
* read-only.
3435
*/
35-
public start: () => Model;
36+
public start: () => Model<N>;
3637

3738
/**
3839
* The end of the log, the current state of the document. It is the model
3940
* instance that is used to apply new patches to the log.
4041
*
4142
* @readonly
4243
*/
43-
public readonly end: Model;
44+
public readonly end: Model<N>;
4445

4546
/**
4647
* The collection of patches which are applied to the `start()` model to reach
@@ -55,7 +56,7 @@ export class Log implements Printable {
5556
private __onPatch: FanOutUnsubscribe;
5657
private __onFlush: FanOutUnsubscribe;
5758

58-
constructor(start: () => Model) {
59+
constructor(start: () => Model<N>) {
5960
this.start = start;
6061
const end = (this.end = start());
6162
const onPatch = (patch: Patch) => {
@@ -84,7 +85,7 @@ export class Log implements Printable {
8485
*
8586
* @returns A new model instance with all patches replayed.
8687
*/
87-
public replayToEnd(): Model {
88+
public replayToEnd(): Model<N> {
8889
const clone = this.start().clone();
8990
for (let node = first(this.patches.root); node; node = next(node)) clone.applyPatch(node.v);
9091
return clone;
@@ -98,7 +99,7 @@ export class Log implements Printable {
9899
* @param ts Timestamp ID of the patch to replay to.
99100
* @returns A new model instance with patches replayed up to the given timestamp.
100101
*/
101-
public replayTo(ts: ITimestampStruct): Model {
102+
public replayTo(ts: ITimestampStruct): Model<N> {
102103
const clone = this.start().clone();
103104
for (let node = first(this.patches.root); node && compare(ts, node.k) >= 0; node = next(node))
104105
clone.applyPatch(node.v);
@@ -119,7 +120,7 @@ export class Log implements Printable {
119120
for (; node && compare(ts, node.k) >= 0; node = next(node)) newStartPatches.push(node.v);
120121
for (const patch of newStartPatches) this.patches.del(patch.getId()!);
121122
const oldStart = this.start;
122-
this.start = (): Model => {
123+
this.start = (): Model<N> => {
123124
const model = oldStart();
124125
for (const patch of newStartPatches) model.applyPatch(patch);
125126
return model;

0 commit comments

Comments
 (0)