Skip to content

Commit

Permalink
feat: upgrade loro-crdt to v0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Nov 27, 2023
1 parent 5dee246 commit 8bfe24b
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 10 deletions.
6 changes: 3 additions & 3 deletions 01_basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, LoroList, LoroMap } from "npm:loro-crdt@0.4.0"
import { Loro, LoroList, LoroMap } from "npm:loro-crdt@0.5.0"
import { expect } from "npm:[email protected]"

Deno.test("Basic usage", () => {
Expand Down Expand Up @@ -39,11 +39,11 @@ Deno.test("Sub containers", () => {
// insert a List container at index 0, and get the handler to that list
const subList = list.insertContainer(0, "List");
subList.insert(0, "A");
expect(list.getDeepValue()).toStrictEqual([["A"]]);
expect(list.toJson()).toStrictEqual([["A"]]);
// create a Text container inside the Map container
const subtext = map.setContainer("text", "Text");
subtext.insert(0, "Hi");
expect(map.getDeepValue()).toStrictEqual({ text: "Hi" });
expect(map.toJson()).toStrictEqual({ text: "Hi" });
});

Deno.test("Sync", () => {
Expand Down
4 changes: 1 addition & 3 deletions 02_text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Delta, Loro } from "npm:loro-crdt@0.4.0";
import { Delta, Loro } from "npm:loro-crdt@0.5.0";
import { expect } from "npm:[email protected]";

Deno.test("Text", () => {
Expand Down Expand Up @@ -87,8 +87,6 @@ Deno.test("Rich text event", () => {
attributes: { bold: true }
}]);
ran = true;
} else {
throw new Error()
}
});
text.mark({ start: 0, end: 5 }, "bold", true);
Expand Down
2 changes: 1 addition & 1 deletion 03_version.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro, OpId } from "npm:loro-crdt@0.4.0";
import { Loro, OpId } from "npm:loro-crdt@0.5.0";
import { expect } from "npm:[email protected]";


Expand Down
2 changes: 1 addition & 1 deletion 04_time_travel.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Loro } from "npm:loro-crdt@0.4.0";
import { Loro } from "npm:loro-crdt@0.5.0";
import { expect } from "npm:[email protected]";

Deno.test("Time Travel", () => {
Expand Down
67 changes: 67 additions & 0 deletions 05_save_and_load.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Loro } 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 Loro();
doc.getText("text").insert(0, "Hello world!");
const data = doc.exportSnapshot();

const newDoc = new Loro();
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.exportFrom()` to get the binary data of the operations since last export.
const doc = new Loro();
doc.getText("text").insert(0, "Hello world!");
const data = doc.exportSnapshot();
let lastSavedVersion = doc.version();
doc.getText("text").insert(0, "✨");
const update0 = doc.exportFrom(lastSavedVersion);
lastSavedVersion = doc.version();
doc.getText("text").insert(0, "😶‍🌫️");
const update1 = doc.exportFrom(lastSavedVersion);

{
/**
* You can import the snapshot and the updates to get the latest version of the document.
*/

// import the snapshot
const newDoc = new Loro();
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 Loro();
newDoc.importUpdateBatch([update1, update0, data])
expect(newDoc.toJson()).toStrictEqual({
text: "😶‍🌫️✨Hello world!"
});
}
})
92 changes: 90 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8bfe24b

Please sign in to comment.