Skip to content
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

What's the best approach for a listener that modifies self state on event? #13

Open
pherrymason opened this issue Sep 17, 2023 · 0 comments

Comments

@pherrymason
Copy link

pherrymason commented Sep 17, 2023

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
     }
}
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

No branches or pull requests

1 participant