Skip to content

Commit

Permalink
chore: revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeeon233 committed Jan 6, 2025
1 parent 477c4f6 commit ca51a78
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
33 changes: 22 additions & 11 deletions crates/loro-ffi/src/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn OnPush>) {
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<Arc<dyn OnPush>>) {
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<dyn OnPop>) {
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<Arc<dyn OnPop>>) {
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);
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/loro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnPush>) {
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<OnPop>) {
self.0.set_on_pop(on_pop);
}

/// Clear the undo stack and the redo stack
Expand Down
16 changes: 8 additions & 8 deletions crates/loro/tests/integration_test/undo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1583,19 +1583,19 @@ fn undo_transform_cursor_position() -> anyhow::Result<()> {
let mut undo = UndoManager::new(&doc);
let cursors: Arc<Mutex<Vec<Cursor>>> = 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
Expand Down

0 comments on commit ca51a78

Please sign in to comment.