From 9c25e6d2738f8095a3c99fb6f0fedcc401d1e8ed Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sat, 10 Feb 2024 22:19:52 +0800 Subject: [PATCH] fix: should not reset the state when calling checkout to latest (#265) --- crates/loro-internal/src/loro.rs | 7 +++++++ crates/loro-internal/tests/test.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/crates/loro-internal/src/loro.rs b/crates/loro-internal/src/loro.rs index d7ab3217..14a36eb0 100644 --- a/crates/loro-internal/src/loro.rs +++ b/crates/loro-internal/src/loro.rs @@ -260,6 +260,8 @@ impl LoroDoc { immediate_renew: bool, ) { if !self.auto_commit.load(Acquire) { + // if not auto_commit, nothing should happen + // because the global txn is not used return; } @@ -660,6 +662,11 @@ impl LoroDoc { } pub fn checkout_to_latest(&self) { + if !self.is_detached() { + self.commit_then_renew(); + return; + } + debug_log::debug_log!("Attached {}", self.peer_id()); let f = self.oplog_frontiers(); self.checkout(&f).unwrap(); diff --git a/crates/loro-internal/tests/test.rs b/crates/loro-internal/tests/test.rs index a9d3c23f..68168f4a 100644 --- a/crates/loro-internal/tests/test.rs +++ b/crates/loro-internal/tests/test.rs @@ -8,6 +8,18 @@ use loro_internal::{ }; use serde_json::json; +#[test] +fn issue_502() -> LoroResult<()> { + let doc = LoroDoc::new_auto_commit(); + doc.get_map("map").insert("stringA", "Original data")?; + doc.commit_then_renew(); + doc.get_map("map").insert("stringA", "Updated data")?; + doc.attach(); + doc.get_map("map").insert("stringB", "Something else")?; + doc.commit_then_renew(); + Ok(()) +} + #[test] fn issue_225() -> LoroResult<()> { let doc = LoroDoc::new_auto_commit();