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
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! A module that provides a generic, type-safe event bus for arbitrary event types.

use std::any::{Any, TypeId};

use dashmap::DashMap;

type Listener<'a> = dyn Fn(&dyn Any) + Send + Sync + 'a;

/// An event bus for arbitrary event types.
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> {
    /// Dispatch an event via this event bus.
    ///
    /// All active listeners registered for this event will be invoked.
    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))
        }
    }

    /// Add an event listener bound to a specific event type, `E`, and registered with the given ID.
    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,
        ));
    }

    /// Remove all event listeners registered with the given ID, dropping them in the process.
    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> {
    /// Add an event listener bound to a specific event type, `E`, and bound to a type `T`.
    ///
    /// This event listener will be removed when [`Bus::remove_listeners_by_id`] is called with the `TypeId` of `T`.
    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);
    }

    /// Add an event listener bound to a specific event type, `E`, registered using a hidden type that will prevent its
    /// removal until the event bus is dropped.
    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);
    }
}