Skip to content
Open
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
2 changes: 1 addition & 1 deletion crates/loro-internal/src/encoding/shallow_snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub(crate) fn encode_snapshot_at<W: std::io::Write>(
w: &mut W,
) -> Result<(), LoroEncodeError> {
let was_detached = doc.is_detached();
let version_before_start = doc.oplog_frontiers();
let version_before_start = doc.state_frontiers();
doc._checkout_without_emitting(frontiers, true, false)
.unwrap();
let result = 'block: {
Expand Down
38 changes: 38 additions & 0 deletions crates/loro-wasm/tests/checkout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,42 @@ describe("Checkout", () => {
doc.import(docB.export({ mode: "update" }));
expect(docB.cmpWithFrontiers(doc.frontiers())).toBe(0);
});

it("forkAt inside subscription during checkout event should not corrupt state", async () => {
const doc = new LoroDoc();
doc.setPeerId("1");

// Make some changes
doc.getText("text").insert(0, "Hello");
doc.commit();
const frontier1 = doc.frontiers(); // [{ peer: "1", counter: 4 }]

doc.getText("text").insert(5, " World");
doc.commit();

// Verify initial state
expect(doc.toJSON()).toStrictEqual({ text: "Hello World" });

// Subscribe and call forkAt inside the callback
let forkResult: any = null;
doc.subscribe((event) => {
if (event.by === "checkout") {
// BUG: This corrupts the checkout state
const fork = doc.forkAt(doc.frontiers());
forkResult = fork.toJSON();
}
});

// Checkout to earlier state
doc.checkout(frontier1);

// Wait for events to be processed
await new Promise((resolve) => setTimeout(resolve, 1));

expect(doc.frontiers()).toStrictEqual(frontier1);
expect(doc.toJSON()).toStrictEqual({ text: "Hello" });

// The fork should also have the correct state at frontier1
expect(forkResult).toStrictEqual({ text: "Hello" });
});
});
Loading