bee_runtime/
event.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4//! A module that provides a generic, type-safe event bus for arbitrary event types.
5
6use std::any::{Any, TypeId};
7
8use dashmap::DashMap;
9
10type Listener<'a> = dyn Fn(&dyn Any) + Send + Sync + 'a;
11
12/// An event bus for arbitrary event types.
13pub 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    /// Dispatch an event via this event bus.
27    ///
28    /// All active listeners registered for this event will be invoked.
29    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    /// Add an event listener bound to a specific event type, `E`, and registered with the given ID.
36    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    /// Remove all event listeners registered with the given ID, dropping them in the process.
44    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    /// Add an event listener bound to a specific event type, `E`, and bound to a type `T`.
53    ///
54    /// This event listener will be removed when [`Bus::remove_listeners_by_id`] is called with the `TypeId` of `T`.
55    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    /// Add an event listener bound to a specific event type, `E`, registered using a hidden type that will prevent its
60    /// removal until the event bus is dropped.
61    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}