intendednull / yewdux

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

Listener Injection #52

Closed wainwrightmark closed 1 year ago

wainwrightmark commented 1 year ago

Closes https://github.com/intendednull/yewdux/issues/51

This allows you to inject listeners into the #[store] macro

#[derive(Default, Clone, PartialEq, Eq, Deserialize, Serialize, Store)]
#[store(storage = "local", listener(LogListener))]
struct State {
    count: u32,
}

struct LogListener;
impl Listener for LogListener {
    type Store = State;

    fn on_change(&mut self, state: Rc<Self::Store>) {
        log!(Level::Info, "Count changed to {}", state.count);
    }
}

This also fixes a bug where adding more than one listener would drop previous listeners.

intendednull commented 1 year ago

Thank you for your contribution