-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_op_and_change.test.ts
84 lines (79 loc) · 2.05 KB
/
10_op_and_change.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Change, LoroDoc } from "npm:[email protected]";
import { expect } from "npm:[email protected]";
Deno.test("op and change", () => {
const docA = new LoroDoc();
docA.setPeerId("0");
const textA = docA.getText("text");
// This create 3 operations
textA.insert(0, "123");
// This create a new Change
docA.commit();
// This create 2 operations
textA.insert(0, "ab");
// This will NOT create a new Change
docA.commit();
{
const changeMap: Map<`${number}`, Change[]> = docA.getAllChanges();
expect(changeMap.size).toBe(1);
expect(changeMap.get("0")).toStrictEqual([
{
lamport: 0,
length: 5,
peer: "0",
counter: 0,
deps: [],
timestamp: 0,
message: undefined,
},
]);
}
// Create docB from doc
const docB = LoroDoc.fromSnapshot(docA.export({ mode: "snapshot" }));
docB.setPeerId("1");
const textB = docB.getText("text");
// This create 2 operations
textB.insert(0, "cd");
// Import the Change from docB to doc
const bytes = docB.export({ mode: "update" }); // Exporting has implicit commit
docA.import(bytes);
// This create 1 operations
textA.insert(0, "1");
// Because doc import a Change from docB, it will create a new Change for
// new commit to record this causal order
docA.commit();
{
const changeMap: Map<`${number}`, Change[]> = docA.getAllChanges();
expect(changeMap.size).toBe(2);
expect(changeMap.get("0")).toStrictEqual([
{
lamport: 0,
length: 5,
peer: "0",
counter: 0,
deps: [],
timestamp: 0,
message: undefined,
},
{
lamport: 7,
length: 1,
peer: "0",
counter: 5,
deps: [{ peer: "1", counter: 1 }],
timestamp: 0,
message: undefined,
},
]);
expect(changeMap.get("1")).toStrictEqual([
{
lamport: 5,
length: 2,
peer: "1",
counter: 0,
deps: [{ peer: "0", counter: 4 }],
timestamp: 0,
message: undefined,
},
]);
}
});