extern crate event_dispatcher; use event_dispatcher::{Event, Listener, Dispatcher, Dispatchable}; struct TestEvent { stopped: bool, data: String } impl TestEvent { pub fn new(event_data: String) -> TestEvent { TestEvent { stopped: false, data: event_data } } } impl Event for TestEvent { fn is_propagation_stopped(&self) -> bool{ self.stopped } fn stop_propagation(&mut self){ self.stopped = true; } } fn event_test(event: &TestEvent) { println!("called \"event_test\""); } fn my_event_test(event: &TestEvent) { println!("called \"my_event_test\" with {:?}", event.data); } fn main() { let mut dispatcher = Dispatcher::new(); // the event let event = TestEvent::new("my_event".to_string()); // registering a listener dispatcher.add_listener(&event, Listener::new(event_test)); // registering a listener with a priority and // storing the listener, so it can be removed later let listener = dispatcher.add_listener(&event, Listener::new_priority(my_event_test, 1)); // dispatching the event dispatcher.dispatch(&event); // removing the listener dispatcher.remove_listener(&event, listener); // dispatching the event again dispatcher.dispatch(&event); }