Skip to content

feat: support optional manual checkpointing for UndoManager #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

synoet
Copy link
Contributor

@synoet synoet commented Apr 28, 2025

I believe there are several use cases where you may not want a push to the undo stack to be directly tied to a loroDoc.commit(). For example, if you're building a drawing application with a pencil tool, you might push changes to the loroDoc with some debounce as the user draws a line. However, you likely only want to push the fully completed stroke to the undo stack. While mergeInterval can assist with this, some applications ultimately require manual control over when a set of changes is pushed to the undo stack.

Changes:

  • Added support for creating an UndoManager with manual checkpointing.

Example:

fn undo_manual_checkpoint() {
    let doc = LoroDoc::new();
    let mut undo_manager = UndoManager::new_with_manual_checkpoint(&doc);
    let text = doc.get_text("text");

    let counter = Arc::new(AtomicU64::new(0));
    let counter_clone = Arc::clone(&counter);
    undo_manager.set_on_push(Some(Box::new(move |_, _, _| {
        counter_clone.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        UndoItemMeta {
            value: LoroValue::Null,
            cursors: Default::default(),
        }
    })));

    text.update("hello", UpdateOptions::default()).unwrap();

    doc.commit_then_renew();

    // Nothing should have been pushed to the stack
    assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 0);

    undo_manager.record_new_checkpoint().unwrap();

    // assert only one thing was pushed to the stack
    assert_eq!(counter.load(std::sync::atomic::Ordering::Relaxed), 1);
}

@synoet synoet marked this pull request as ready for review April 28, 2025 20:43
Copy link
Member

@zxch3n zxch3n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants