hey_listen
hey_listen copied to clipboard
What's the best approach for a listener that modifies self state on event?
Hello.
I was trying to implement a hey_listen::rc::DispatcherRequest
with the intention of being able to modify the internal state of the listener.
But noticed the signature of the on_event method defines self as immutable:
fn on_event(&self, event: &T) -> Option<DispatcherRequest>;
What should be a better approach of what I'm trying to achieve? Move the state out of the listener and modify it from it? If I'm not mistaken this would also require a mutable reference to self.
Interior mutability maybe?
struct MyStruct {
state: RefCell<State>
}
impl Listener<Event> for MyStruct {
fn on_event(&self, event: &Event) -> Option<DispatcherRequest> {
self.state.replace(State::NewState);
None
}
}