1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::any::{Any, TypeId};
use dashmap::DashMap;
type Listener<'a> = dyn Fn(&dyn Any) + Send + Sync + 'a;
pub struct Bus<'a, ID = TypeId> {
listeners: DashMap<TypeId, Vec<(Box<Listener<'a>>, ID)>>,
}
impl<'a, ID> Default for Bus<'a, ID> {
fn default() -> Self {
Self {
listeners: DashMap::default(),
}
}
}
impl<'a, ID: Clone + PartialEq> Bus<'a, ID> {
pub fn dispatch<E: Any>(&self, event: E) {
if let Some(mut ls) = self.listeners.get_mut(&TypeId::of::<E>()) {
ls.iter_mut().for_each(|(l, _)| l(&event))
}
}
pub fn add_listener_raw<E: Any, F: Fn(&E) + Send + Sync + 'a>(&self, id: ID, handler: F) {
self.listeners.entry(TypeId::of::<E>()).or_default().push((
Box::new(move |event| handler(event.downcast_ref().expect("Invalid event"))),
id,
));
}
pub fn remove_listeners_by_id(&self, id: ID) {
self.listeners
.iter_mut()
.for_each(|mut listeners| listeners.retain(|(_, listener_id)| listener_id != &id));
}
}
impl<'a> Bus<'a, TypeId> {
pub fn add_listener<T: Any, E: Any, F: Fn(&E) + Send + Sync + 'a>(&self, handler: F) {
self.add_listener_raw(TypeId::of::<T>(), handler);
}
pub fn add_static_listener<E: Any, F: Fn(&E) + Send + Sync + 'a>(&self, handler: F) {
struct Static;
self.add_listener_raw(TypeId::of::<Static>(), handler);
}
}