Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loro-js-detached-import-delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"loro.js": patch
---

Fix `loro.js` losing Text/List deletions that were imported while the document was detached. After `checkout(...)`, importing an update that deleted characters or list items, and then calling `checkoutToLatest()` or `checkout(...)` to a version that includes the delete, kept the deleted content visible. It now matches `loro-crdt` and a fresh import of the same updates.
11 changes: 11 additions & 0 deletions loro-js/src/runtime/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3824,6 +3824,17 @@ export class LoroDoc<T extends Record<string, Container> = Record<string, Contai
if (!(container instanceof LoroList || container instanceof LoroText)) {
return false;
}
// A delete imported while detached is in the history but not in the
// sequence's deletion index yet; only a replay applies it.
let recorded = 0;
for (const run of container._sequence.idRunsDeletedBy(
change.id.peer,
operation.counter,
operation.counter + operation.length,
)) {
recorded += run.length;
}
if (recorded !== operation.length) return false;
} else if (content.type === "map-insert" || content.type === "map-delete") {
if (!(container instanceof LoroMap)) return false;
} else if (
Expand Down
38 changes: 38 additions & 0 deletions loro-js/tests/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,44 @@ describe("loro-wasm-compatible runtime", () => {
expect(doc.getText("text").toString()).toBe("base latest");
});

// A delete imported while detached was left out of the next transition:
// the transition looked it up in the sequence's deletion index, which only
// attached imports update. loro-crdt applies it.
test.each([
["text", "latest"],
["text", "checkout"],
["list", "latest"],
["list", "checkout"],
] as const)("applies a %s delete imported while detached (%s)", (kind, target) => {
const read = (doc: LoroDoc): unknown =>
kind === "text" ? doc.getText("seq").toString() : doc.getList("seq").toArray();
const source = new LoroDoc();
source.setPeerId(2);
if (kind === "text") source.getText("seq").insert(0, "hello");
else for (const value of "hello") source.getList("seq").push(value);
source.commit();

const doc = new LoroDoc();
doc.setPeerId(1);
doc.import(source.export({ mode: "update" }));
if (kind === "text") source.getText("seq").delete(4, 1);
else source.getList("seq").delete(4, 1);
source.commit();
doc.getCounter("counter").increment(1);
doc.commit();

doc.checkout([{ peer: "2", counter: 1 }]);
expect(read(doc)).toEqual(kind === "text" ? "he" : ["h", "e"]);
doc.import(source.export({ mode: "update", from: doc.oplogVersion() }));
if (target === "latest") doc.checkoutToLatest();
else doc.checkout(doc.oplogFrontiers());

expect(read(doc)).toEqual(kind === "text" ? "hell" : ["h", "e", "l", "l"]);
const replay = new LoroDoc();
replay.import(doc.export({ mode: "update" }));
expect(read(replay)).toEqual(read(doc));
});

test("holds causally incomplete updates pending until dependencies arrive", () => {
const source = new LoroDoc();
source.setPeerId(1);
Expand Down
Loading