Skip to content

Commit

Permalink
fix: panic when detach then attach (#592)
Browse files Browse the repository at this point in the history
detaching should commit the pending ops
  • Loading branch information
zxch3n authored Dec 28, 2024
1 parent 18e7920 commit 9faa149
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-penguins-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"loro-crdt": patch
---

Fix detach + attach error
1 change: 1 addition & 0 deletions crates/loro-internal/src/loro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ impl LoroDoc {

#[inline(always)]
pub fn detach(&self) {
self.commit_then_stop();
self.set_detached(true);
}

Expand Down
9 changes: 9 additions & 0 deletions crates/loro-wasm/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,12 @@ it("can use version vector multiple times", () => {
doc.vvToFrontiers(v);
v.toJSON();
})

it("detach and attach on empty doc", () => {
const doc = new LoroDoc();
expect(doc.isDetached()).toBe(false);
doc.detach();
expect(doc.isDetached()).toBe(true);
doc.attach();
expect(doc.isDetached()).toBe(false);
})
10 changes: 10 additions & 0 deletions crates/loro/tests/loro_rust_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2339,3 +2339,13 @@ fn test_get_or_create_container_with_null() {
json!({ "root": { "key": {} } })
);
}

#[test]
fn test_detach_and_attach() {
let doc = LoroDoc::new();
assert!(!doc.is_detached());
doc.detach();
assert!(doc.is_detached());
doc.attach();
assert!(!doc.is_detached());
}

0 comments on commit 9faa149

Please sign in to comment.