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