From ca51a78bacc1894c49bd34c52f5bef7de622d8f5 Mon Sep 17 00:00:00 2001 From: Leon Zhao Date: Mon, 6 Jan 2025 16:02:38 +0800 Subject: [PATCH] chore: revert --- crates/loro-ffi/src/undo.rs | 33 ++++++++++++------- crates/loro/src/lib.rs | 16 +++++---- .../loro/tests/integration_test/undo_test.rs | 16 ++++----- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/crates/loro-ffi/src/undo.rs b/crates/loro-ffi/src/undo.rs index 4ff276e9e..7c43de49d 100644 --- a/crates/loro-ffi/src/undo.rs +++ b/crates/loro-ffi/src/undo.rs @@ -55,21 +55,32 @@ impl UndoManager { /// Set the listener for push events. /// The listener will be called when a new undo/redo item is pushed into the stack. - pub fn set_on_push(&self, on_push: Arc) { - self.0 - .write() - .unwrap() - .set_on_push(Box::new(move |u, c, e| { - loro::UndoItemMeta::from(on_push.on_push(u, c, e.map(|x| x.into()))) - })); + pub fn set_on_push(&self, on_push: Option>) { + if let Some(on_push) = on_push { + self.0 + .write() + .unwrap() + .set_on_push(Some(Box::new(move |u, c, e| { + loro::UndoItemMeta::from(on_push.on_push(u, c, e.map(|x| x.into()))) + }))); + } else { + self.0.write().unwrap().set_on_push(None); + } } /// Set the listener for pop events. /// The listener will be called when an undo/redo item is popped from the stack. - pub fn set_on_pop(&self, on_pop: Arc) { - self.0.write().unwrap().set_on_pop(Box::new(move |u, c, m| { - on_pop.on_pop(u, c, UndoItemMeta::from(m)) - })) + pub fn set_on_pop(&self, on_pop: Option>) { + if let Some(on_pop) = on_pop { + self.0 + .write() + .unwrap() + .set_on_pop(Some(Box::new(move |u, c, m| { + on_pop.on_pop(u, c, UndoItemMeta::from(m)) + }))); + } else { + self.0.write().unwrap().set_on_pop(None); + } } } diff --git a/crates/loro/src/lib.rs b/crates/loro/src/lib.rs index 45d173b33..4a7098336 100644 --- a/crates/loro/src/lib.rs +++ b/crates/loro/src/lib.rs @@ -2699,16 +2699,20 @@ impl UndoManager { /// Set the listener for push events. /// The listener will be called when a new undo/redo item is pushed into the stack. - pub fn set_on_push(&mut self, on_push: OnPush) { - self.0.set_on_push(Some(Box::new(move |u, c, e| { - on_push(u, c, e.map(|x| x.into())) - }))) + pub fn set_on_push(&mut self, on_push: Option) { + if let Some(on_push) = on_push { + self.0.set_on_push(Some(Box::new(move |u, c, e| { + on_push(u, c, e.map(|x| x.into())) + }))); + } else { + self.0.set_on_push(None); + } } /// Set the listener for pop events. /// The listener will be called when an undo/redo item is popped from the stack. - pub fn set_on_pop(&mut self, on_pop: OnPop) { - self.0.set_on_pop(Some(on_pop)); + pub fn set_on_pop(&mut self, on_pop: Option) { + self.0.set_on_pop(on_pop); } /// Clear the undo stack and the redo stack diff --git a/crates/loro/tests/integration_test/undo_test.rs b/crates/loro/tests/integration_test/undo_test.rs index 83b2ec6ae..a164f52b6 100644 --- a/crates/loro/tests/integration_test/undo_test.rs +++ b/crates/loro/tests/integration_test/undo_test.rs @@ -1536,17 +1536,17 @@ fn undo_manager_events() -> anyhow::Result<()> { let pop_count_clone = pop_count.clone(); let popped_value = Arc::new(Mutex::new(LoroValue::Null)); let popped_value_clone = popped_value.clone(); - undo.set_on_push(Box::new(move |_source, span, _| { + undo.set_on_push(Some(Box::new(move |_source, span, _| { push_count_clone.fetch_add(1, atomic::Ordering::SeqCst); UndoItemMeta { value: LoroValue::I64(span.start as i64), cursors: Default::default(), } - })); - undo.set_on_pop(Box::new(move |_source, _span, v| { + }))); + undo.set_on_pop(Some(Box::new(move |_source, _span, v| { pop_count_clone.fetch_add(1, atomic::Ordering::SeqCst); *popped_value_clone.try_lock().unwrap() = v.value; - })); + }))); text.insert(0, "Hello")?; assert_eq!(push_count.load(atomic::Ordering::SeqCst), 0); doc.commit(); @@ -1583,19 +1583,19 @@ fn undo_transform_cursor_position() -> anyhow::Result<()> { let mut undo = UndoManager::new(&doc); let cursors: Arc>> = Arc::new(Mutex::new(Vec::new())); let cursors_clone = cursors.clone(); - undo.set_on_push(Box::new(move |_, _, _| { + undo.set_on_push(Some(Box::new(move |_, _, _| { let mut ans = UndoItemMeta::new(); let cursors = cursors_clone.try_lock().unwrap(); for c in cursors.iter() { ans.add_cursor(c) } ans - })); + }))); let popped_cursors = Arc::new(Mutex::new(Vec::new())); let popped_cursors_clone = popped_cursors.clone(); - undo.set_on_pop(Box::new(move |_, _, meta| { + undo.set_on_pop(Some(Box::new(move |_, _, meta| { *popped_cursors_clone.try_lock().unwrap() = meta.cursors; - })); + }))); text.insert(0, "Hello world!")?; doc.commit(); cursors