Skip to content

Commit

Permalink
fix(json-crdt): 馃悰 store golbal session in clock vector
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 28, 2024
1 parent 4813bc9 commit 407c383
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions src/json-crdt/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,16 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
* @returns Strictly typed model.
*/
public setSchema<S extends NodeBuilder>(schema: S, useGlobalSession: boolean = true): Model<SchemaToJsonNode<S>> {
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 <any>this;
}
Expand Down

0 comments on commit 407c383

Please sign in to comment.