fix: empty diff emits event (#292)

This commit is contained in:
Leon Zhao 2024-03-14 20:46:18 +08:00 committed by GitHub
parent f7fe0d4b26
commit 83d4950a4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View file

@ -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;
}

View file

@ -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));
}