diff --git a/src/json-crdt-extensions/peritext/util/__tests__/ChunkSlice.spec.ts b/src/json-crdt-extensions/peritext/util/__tests__/ChunkSlice.spec.ts index 365ed1b867..5183b2ed9a 100644 --- a/src/json-crdt-extensions/peritext/util/__tests__/ChunkSlice.spec.ts +++ b/src/json-crdt-extensions/peritext/util/__tests__/ChunkSlice.spec.ts @@ -3,7 +3,7 @@ import {Model} from '../../../../json-crdt/model'; import {ChunkSlice} from '../ChunkSlice'; const setup = () => { - const model = Model.withLogicalClock().setSchema(s.str('Hello world')); + const model = Model.withLogicalClock().setSchema(s.str('Hello world'), false); const node = model.root.node(); const chunk = node.first()!; return { diff --git a/src/json-crdt/model/Model.ts b/src/json-crdt/model/Model.ts index 51c82481d8..9aaba10f9d 100644 --- a/src/json-crdt/model/Model.ts +++ b/src/json-crdt/model/Model.ts @@ -403,13 +403,16 @@ export class Model> implements Printable { * @returns Strictly typed model. */ public setSchema(schema: S, useGlobalSession: boolean = true): Model> { - const clock = this.clock; - const sid = useGlobalSession ? SESSION.GLOBAL : clock.sid; - if (clock.time < 2) { - const oldSid = clock.sid; - clock.sid = sid; + const c = this.clock; + const isNewDocument = c.time === 1; + if (isNewDocument) { + const oldSid = c.sid; + if (useGlobalSession) c.sid = SESSION.GLOBAL; this.api.root(schema); - clock.sid = oldSid; + if (useGlobalSession) { + c.sid = oldSid; + c.observe(new clock.Timestamp(SESSION.GLOBAL, c.time - 1), 1); + } } return this; }