forked from neovide/neovide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_aggregator.rs
73 lines (62 loc) · 2.39 KB
/
event_aggregator.rs
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
66
67
68
69
70
71
72
73
use std::{
any::{type_name, Any, TypeId},
collections::{hash_map::Entry, HashMap},
fmt::Debug,
};
use parking_lot::RwLock;
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};
use crate::channel_utils::*;
lazy_static! {
pub static ref EVENT_AGGREGATOR: EventAggregator = EventAggregator::default();
}
pub struct EventAggregator {
parent_senders: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
unclaimed_receivers: RwLock<HashMap<TypeId, Box<dyn Any + Send + Sync>>>,
}
impl Default for EventAggregator {
fn default() -> Self {
EventAggregator {
parent_senders: RwLock::new(HashMap::new()),
unclaimed_receivers: RwLock::new(HashMap::new()),
}
}
}
impl EventAggregator {
fn get_sender<T: Any + Clone + Debug + Send>(&self) -> LoggingTx<T> {
match self.parent_senders.write().entry(TypeId::of::<T>()) {
Entry::Occupied(entry) => {
let sender = entry.get();
sender.downcast_ref::<LoggingTx<T>>().unwrap().clone()
}
Entry::Vacant(entry) => {
let (sender, receiver) = unbounded_channel();
let logging_tx = LoggingTx::attach(sender, type_name::<T>().to_owned());
entry.insert(Box::new(logging_tx.clone()));
self.unclaimed_receivers
.write()
.insert(TypeId::of::<T>(), Box::new(receiver));
logging_tx
}
}
}
pub fn send<T: Any + Clone + Debug + Send>(&self, event: T) {
let sender = self.get_sender::<T>();
sender.send(event).unwrap();
}
pub fn register_event<T: Any + Clone + Debug + Send>(&self) -> UnboundedReceiver<T> {
let type_id = TypeId::of::<T>();
if let Some(receiver) = self.unclaimed_receivers.write().remove(&type_id) {
*receiver.downcast::<UnboundedReceiver<T>>().unwrap()
} else {
let (sender, receiver) = unbounded_channel();
let logging_sender = LoggingTx::attach(sender, type_name::<T>().to_owned());
match self.parent_senders.write().entry(type_id) {
Entry::Occupied(_) => panic!("EventAggregator: type already registered"),
Entry::Vacant(entry) => {
entry.insert(Box::new(logging_sender));
}
}
receiver
}
}
}