1use evno::{Bus, many, on, once};
2use std::time::Duration;
3use tokio::join;
4
5#[derive(Clone)]
6struct EventA;
7
8#[derive(Clone)]
9struct EventB;
10
11#[derive(Clone)]
12struct EventC;
13
14#[derive(Clone)]
15struct EventD;
16
17async fn event_c_listener(_: EventC) {
18 println!("Handled Event C");
19}
20
21#[tokio::main]
22async fn main() {
23 let bus = Bus::new(2);
24
25 let handle1 = bus.bind::<EventA>(once(|_| async {
27 println!("Handled Event A");
28 }));
29
30 let handle2 = bus.bind(on(|_: EventB| async {
32 println!("Handled Event B");
33 }));
34
35 let handle3 = bus.bind(many(3, event_c_listener));
37
38 bus.emit(EventA).await;
40 bus.emit(EventB).await;
41 bus.emit(EventC).await;
42 bus.emit(EventD).await;
43
44 handle2.cancel();
46 tokio::time::sleep(Duration::from_millis(1)).await;
47 let handle4 = bus.bind(on(|_: EventD| async {
48 println!("Handled Event D");
49 }));
50
51 bus.emit(EventA).await;
53 bus.emit(EventB).await;
54 bus.clone().emit(EventD).await;
55
56 drop(bus);
57
58 let _ = join!(handle1, handle3, handle4);
59}