From 83d4950a4e266cf07476ead5092b3d6d131988a7 Mon Sep 17 00:00:00 2001 From: Leon Zhao Date: Thu, 14 Mar 2024 20:46:18 +0800 Subject: [PATCH] fix: empty diff emits event (#292) --- crates/loro-internal/src/state.rs | 2 +- crates/loro-internal/tests/test.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/loro-internal/src/state.rs b/crates/loro-internal/src/state.rs index 714f077e..98ebb56d 100644 --- a/crates/loro-internal/src/state.rs +++ b/crates/loro-internal/src/state.rs @@ -292,7 +292,7 @@ impl DocState { /// Panic when the diff cannot be merged with the previous diff. /// Caller should call [pre_txn] before calling this to avoid panic. fn record_diff(&mut self, diff: InternalDocDiff) { - if !self.event_recorder.recording_diff { + if !self.event_recorder.recording_diff || diff.diff.is_empty() { return; } diff --git a/crates/loro-internal/tests/test.rs b/crates/loro-internal/tests/test.rs index b5fd05a4..5f1cbf5f 100644 --- a/crates/loro-internal/tests/test.rs +++ b/crates/loro-internal/tests/test.rs @@ -878,3 +878,17 @@ fn missing_event_when_checkout() { doc.attach(); assert!(value.lock().unwrap().contains_key("b")); } + +#[test] +fn empty_event() { + let doc = LoroDoc::new_auto_commit(); + doc.get_map("map").insert("key", 123).unwrap(); + doc.commit_then_renew(); + let fire = Arc::new(AtomicBool::new(false)); + let fire_clone = Arc::clone(&fire); + doc.subscribe_root(Arc::new(move |_e| { + fire_clone.store(true, std::sync::atomic::Ordering::Relaxed); + })); + doc.import(&doc.export_snapshot()).unwrap(); + assert!(!fire.load(std::sync::atomic::Ordering::Relaxed)); +}