Skip to content
Merged
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
10 changes: 5 additions & 5 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ impl LoroDoc {
/// doc.attach();
/// text.insert(0, "Hi");
/// ```
pub fn attach(&mut self) {
pub fn attach(&self) {
self.doc.attach();
}

Expand Down Expand Up @@ -766,7 +766,7 @@ impl LoroDoc {
/// text.insert(0, "Hi");
/// ```
#[wasm_bindgen(js_name = "checkoutToLatest")]
pub fn checkout_to_latest(&mut self) -> JsResult<()> {
pub fn checkout_to_latest(&self) -> JsResult<()> {
self.doc.checkout_to_latest();
Ok(())
}
Expand Down Expand Up @@ -927,7 +927,7 @@ impl LoroDoc {
/// doc.checkout(frontiers);
/// console.log(doc.toJSON()); // {"text": ""}
/// ```
pub fn checkout(&mut self, frontiers: Vec<JsID>) -> JsResult<()> {
pub fn checkout(&self, frontiers: Vec<JsID>) -> JsResult<()> {
self.doc.checkout(&ids_to_frontiers(frontiers)?)?;
Ok(())
}
Expand Down Expand Up @@ -1585,7 +1585,7 @@ impl LoroDoc {
/// doc2.importBatch([snapshot, updates]);
/// ```
#[wasm_bindgen(js_name = "importUpdateBatch")]
pub fn import_update_batch(&mut self, data: JsBinaryArray) -> JsResult<JsImportStatus> {
pub fn import_update_batch(&self, data: JsBinaryArray) -> JsResult<JsImportStatus> {
self.import_batch(data)
}

Expand All @@ -1606,7 +1606,7 @@ impl LoroDoc {
/// doc2.importBatch([snapshot, updates]);
/// ```
#[wasm_bindgen(js_name = "importBatch")]
pub fn import_batch(&mut self, data: JsBinaryArray) -> JsResult<JsImportStatus> {
pub fn import_batch(&self, data: JsBinaryArray) -> JsResult<JsImportStatus> {
let data: Array = data.dyn_into()?;
let data = data
.iter()
Expand Down
101 changes: 101 additions & 0 deletions crates/loro-wasm/tests/event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,36 @@ describe("event", () => {
}
});

it("importBatch flushes pending events", async () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
try {
const source = new LoroDoc();
source.getText("text").insert(0, "Hello");
source.commit();
const snapshot = source.export({ mode: "snapshot" });

const doc = new LoroDoc();
let called = 0;
doc.subscribe(() => {
called += 1;
});

doc.importBatch([snapshot]);
await Promise.resolve();

expect(called).toBeGreaterThan(0);
expect(
errorSpy.mock.calls.some((args) =>
args.some((arg) =>
String(arg).includes("[LORO_INTERNAL_ERROR] Event not called"),
),
),
).toBe(false);
} finally {
errorSpy.mockRestore();
}
});

it("setDetachedEditing flushes pending events", async () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
try {
Expand Down Expand Up @@ -673,6 +703,77 @@ it("subscribe pre commit", () => {
expect(doc.getChangeAt({ peer: "0", counter: 0 }).message).toBe("test");
});

it("importBatch can re-enter doc in pre-commit callback", () => {
const remote = new LoroDoc();
remote.getText("remote").insert(0, "remote");
remote.commit();
const snapshot = remote.export({ mode: "snapshot" });

const doc = new LoroDoc();
doc.setPeerId(1);
doc.getText("local").insert(0, "local");

let callbackError: unknown;
let peerIdStr: string | undefined;
doc.subscribePreCommit(() => {
try {
peerIdStr = doc.peerIdStr;
} catch (error) {
callbackError = error;
}
});

doc.importBatch([snapshot]);

expect(callbackError).toBeUndefined();
expect(peerIdStr).toBe("1");
});

it("checkoutToLatest can re-enter doc in pre-commit callback", () => {
const doc = new LoroDoc();
doc.setPeerId(1);
doc.getText("local").insert(0, "local");

let callbackError: unknown;
let peerIdStr: string | undefined;
doc.subscribePreCommit(() => {
try {
peerIdStr = doc.peerIdStr;
} catch (error) {
callbackError = error;
}
});

doc.checkoutToLatest();

expect(callbackError).toBeUndefined();
expect(peerIdStr).toBe("1");
});

it("attach can re-enter doc in pre-commit callback", () => {
const doc = new LoroDoc();
doc.setPeerId(1);
doc.detach();
doc.setDetachedEditing(true);
doc.getText("local").insert(0, "local");

let callbackError: unknown;
let peerIdStr: string | undefined;
doc.subscribePreCommit(() => {
try {
peerIdStr = doc.peerIdStr;
} catch (error) {
callbackError = error;
}
});

doc.attach();

expect(callbackError).toBeUndefined();
expect(typeof peerIdStr).toBe("string");
expect(peerIdStr).not.toBe("");
});

function oneMs(): Promise<void> {
return new Promise((r) => setTimeout(r));
}
Expand Down
Loading