-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_save_and_load.test.ts
66 lines (58 loc) · 2.05 KB
/
05_save_and_load.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
import { LoroDoc } from "npm:[email protected]";
import { expect } from "npm:[email protected]";
Deno.test("Save and load", () => {
// To save the document, you can call `doc.exportSnapshot()` to get the binary data of the whole document.
// When you want to load the document, you can call `doc.import(data)` to load the binary data.
const doc = new LoroDoc();
doc.getText("text").insert(0, "Hello world!");
const data = doc.export({ mode: "snapshot" });
const newDoc = new LoroDoc();
newDoc.import(data);
expect(newDoc.toJSON()).toStrictEqual({
text: "Hello world!",
});
});
Deno.test("Save and load incrementally", () => {
// It's costly to export the whole document on every keypress.
// So you can call `doc.export({ mode: "update" })()` to get the binary data of the operations since last export.
const doc = new LoroDoc();
doc.getText("text").insert(0, "Hello world!");
const data = doc.export({ mode: "snapshot" });
let lastSavedVersion = doc.version();
doc.getText("text").insert(0, "✨");
const update0 = doc.export({ mode: "update", from: lastSavedVersion });
lastSavedVersion = doc.version();
doc.getText("text").insert(0, "😶🌫️");
const update1 = doc.export({ mode: "update", from: lastSavedVersion });
{
/**
* You can import the snapshot and the updates to get the latest version of the document.
*/
// import the snapshot
const newDoc = new LoroDoc();
newDoc.import(data);
expect(newDoc.toJSON()).toStrictEqual({
text: "Hello world!",
});
// import update0
newDoc.import(update0);
expect(newDoc.toJSON()).toStrictEqual({
text: "✨Hello world!",
});
// import update1
newDoc.import(update1);
expect(newDoc.toJSON()).toStrictEqual({
text: "😶🌫️✨Hello world!",
});
}
{
/**
* You may also import them in a batch
*/
const newDoc = new LoroDoc();
newDoc.importUpdateBatch([update1, update0, data]);
expect(newDoc.toJSON()).toStrictEqual({
text: "😶🌫️✨Hello world!",
});
}
});