Skip to content

Commit

Permalink
fix(json-crdt): 🐛 do not detach builder clock on .reset()
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Sep 17, 2024
1 parent 6fe2faf commit e668d84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/json-crdt/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {

/**
* Applies a single patch to the document. All mutations to the model must go
* through this method.
* through this method. (With the only exception of local changes through API,
* which have an alternative path.)
*/
public applyPatch(patch: Patch) {
this.onbeforepatch?.(patch);
Expand Down Expand Up @@ -492,7 +493,11 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
decoder.decode(blob, <any>this);
this.clock = to.clock.clone();
this.ext = to.ext.clone();
this._api?.flush();
const api = this._api;
if (api) {
api.flush();
api.builder.clock = this.clock;
}
index.forEach(({v: node}) => {
const api = node.api as NodeApi | undefined;
if (!api) return;
Expand Down
15 changes: 15 additions & 0 deletions src/json-crdt/model/__tests__/Model.cloning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,19 @@ describe('reset()', () => {
doc1.reset(doc2);
expect(str.view()).toBe('hello');
});

test('uses the same clock in Model and NodeBuilder', async () => {
const doc1 = Model.create().setSchema(
schema.obj({
text: schema.str('hell'),
}),
);
const doc2 = doc1.fork();
doc2.s.text.toApi().ins(4, 'o');
expect(doc1.clock).toBe(doc1.api.builder.clock);
expect(doc2.clock).toBe(doc2.api.builder.clock);
doc1.reset(doc2);
expect(doc1.clock).toBe(doc1.api.builder.clock);
expect(doc2.clock).toBe(doc2.api.builder.clock);
});
});

0 comments on commit e668d84

Please sign in to comment.