39 lines
856 B
Rust
39 lines
856 B
Rust
#[macro_use]
|
|
extern crate eventbus;
|
|
|
|
use eventbus::{Event, EventBus};
|
|
|
|
struct MyEvent {
|
|
i: i32
|
|
}
|
|
|
|
impl Event for MyEvent {
|
|
}
|
|
|
|
fn add_handler(e: &mut MyEvent) {
|
|
/* adds 1 */
|
|
e.i += 1;
|
|
}
|
|
|
|
fn no_handler(e: &mut MyEvent) {
|
|
/* does nothing */
|
|
e.i += 0;
|
|
}
|
|
|
|
#[test]
|
|
fn test_basic_usage() {
|
|
let event_bus = EventBus::new();
|
|
//let handler_id = event_bus.register(add_handler, 0);
|
|
register_hook!(&event_bus, 0, MyEvent, add_handler);
|
|
let mut event = MyEvent { i: 3 };
|
|
assert_eq!(event.i, 3);
|
|
post_event!(&event_bus, MyEvent, &mut event);
|
|
assert_eq!(event.i, 4);
|
|
register_hook!(&event_bus, 1, MyEvent, no_handler);
|
|
post_event!(&event_bus, MyEvent, &mut event);
|
|
assert_eq!(event.i, 5);
|
|
//event_bus.unregister(handler_id);
|
|
post_event!(&event_bus, MyEvent, &mut event);
|
|
//assert_eq!(event.i, 5);
|
|
}
|