intendednull / yewdux

Ergonomic state management for Yew applications
https://intendednull.github.io/yewdux/
Apache License 2.0
322 stars 31 forks source link

Feature: History/Undo/Redo #54

Closed wainwrightmark closed 1 year ago

wainwrightmark commented 1 year ago

https://github.com/intendednull/yewdux/issues/53#issue-1709773761

This change adds a yewdux-utils crate with a listener that tracks the history of a state and enables undo/redo. It works very nicely - try running new history example.

There's a couple of issues with it that I would like some feedback on.

Firstly, it should be able to initialize it by just injecting the listener using the attribute macro like this:

#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[store(listener(HistoryListener<State>)]
struct State {
    count: u32,
}

but the person who implemented that attribute macro (it was me!) decided to use PathList which apparently doesn't support generic types. I've asked on the Darling repo but I'm pretty sure there's no way to make that work. So you have to initialize it the old way

impl Store for State {
    fn new() -> Self {
        init_listener(HistoryListener::<State>::default());
        Self::default()
    }

    fn should_notify(&self, old: &Self) -> bool {
        self != old
    }
}

The other thing is that the undo tracking currently tracks a potentially infinite number of states which could lead to running out of memory quite quickly if you don't clear it periodically. I think it might be better to let you set a limit on the number of states.

intendednull commented 1 year ago

Looks great! Thank you for your contribution