A simple event bus that can be cloned and shared across threads.

Limitations

At present the only limitation is the inability to use the same EventBus inside on blocks because it will lead to a deadlock.

Example

```rust

[derive(PartialEq, Eq, Hash)]

enum EventType { Start, Stop, }

[derive(Debug, PartialEq)]

enum Status { Stopped, Started, }

let bus: EventBus = EventBus::unbound(); let status = Rc::new(RefCell::new(Status::Stopped)); let statusclosure = Rc::clone(&status); let statusclosure2 = Rc::clone(&status); bus.on(EventType::Start, move || { *statusclosure.borrow_mut() = Status::Started; }) .unwrap();

bus.on(EventType::Stop, move || { *statusclosure2.borrow_mut() = Status::Stopped; }) .unwrap();

bus.emit(EventType::Start).expect("Failed to emit");

asserteq!(*status.borrow(), Status::Started); asserteq!(bus.event_count(), 1);

bus.emit(EventType::Stop).expect("Failed to emit");

asserteq!(*status.borrow(), Status::Stopped); asserteq!(bus.event_count(), 2); ```

Using it in threads

```rust let bus: EventBusclone = bus.clone(); let status = Arc::new(Mutex::new(Status::Stopped)); let finalstatus = Arc::clone(&status);

let t1 = std::thread::spawn(move || { bus.on(EventType::Start, move || { let mut statuslock = status.lock().unwrap(); *status_lock = Status::Started; }).unwrap(); });

let t2 = std::thread::spawn(move || { bus_clone.emit(EventType::Start).unwrap(); });

t1.join().unwrap(); t2.join().unwrap();

let finalstatuslock = finalstatus.lock().unwrap(); asserteq!(*finalstatuslock, Status::Started) ```

Passing data to events

```rust let bus: EventBus = EventBus::unbound(); let status: Rc>> = Rc::new(RefCell::new(None)); let status_closure = Rc::clone(&status);

bus.on(EventType::Start, move |startupdata| { *statusclosure.borrowmut() = Some(*startupdata.unwrap()); }) .unwrap();

bus.emitwithvalue(EventType::Start, Some(&123)).expect("Failed to emit");

asserteq!(*status.borrow(), Some(123)); asserteq!(bus.event_count(), 1); ```