Skip to content

Commit e668d84

Browse files
committed
fix(json-crdt): 🐛 do not detach builder clock on .reset()
1 parent 6fe2faf commit e668d84

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/json-crdt/model/Model.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
308308

309309
/**
310310
* Applies a single patch to the document. All mutations to the model must go
311-
* through this method.
311+
* through this method. (With the only exception of local changes through API,
312+
* which have an alternative path.)
312313
*/
313314
public applyPatch(patch: Patch) {
314315
this.onbeforepatch?.(patch);
@@ -492,7 +493,11 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
492493
decoder.decode(blob, <any>this);
493494
this.clock = to.clock.clone();
494495
this.ext = to.ext.clone();
495-
this._api?.flush();
496+
const api = this._api;
497+
if (api) {
498+
api.flush();
499+
api.builder.clock = this.clock;
500+
}
496501
index.forEach(({v: node}) => {
497502
const api = node.api as NodeApi | undefined;
498503
if (!api) return;

src/json-crdt/model/__tests__/Model.cloning.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,19 @@ describe('reset()', () => {
252252
doc1.reset(doc2);
253253
expect(str.view()).toBe('hello');
254254
});
255+
256+
test('uses the same clock in Model and NodeBuilder', async () => {
257+
const doc1 = Model.create().setSchema(
258+
schema.obj({
259+
text: schema.str('hell'),
260+
}),
261+
);
262+
const doc2 = doc1.fork();
263+
doc2.s.text.toApi().ins(4, 'o');
264+
expect(doc1.clock).toBe(doc1.api.builder.clock);
265+
expect(doc2.clock).toBe(doc2.api.builder.clock);
266+
doc1.reset(doc2);
267+
expect(doc1.clock).toBe(doc1.api.builder.clock);
268+
expect(doc2.clock).toBe(doc2.api.builder.clock);
269+
});
255270
});

0 commit comments

Comments
 (0)