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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
/// Event system - handles events and hooks
/// Based on Gun.js onto.js and event system
pub type EventCallback = Box<dyn Fn(&Event) + Send + Sync>;
#[derive(Clone, Debug)]
pub struct Event {
pub event_type: String,
pub data: serde_json::Value,
}
struct ListenerEntry {
id: u64,
callback: Arc<EventCallback>,
}
pub struct EventEmitter {
listeners: Arc<RwLock<HashMap<String, Vec<ListenerEntry>>>>,
id_counter: Arc<Mutex<u64>>,
}
impl EventEmitter {
pub fn new() -> Self {
Self {
listeners: Arc::new(RwLock::new(HashMap::new())),
id_counter: Arc::new(Mutex::new(0)),
}
}
/// Register an event listener
/// Returns the listener ID for later removal
///
/// # Panics
/// This function will panic if the lock is poisoned, which should never happen
/// in practice since we don't panic while holding the lock.
pub fn on(&self, event_type: &str, callback: EventCallback) -> u64 {
let mut listeners = self.listeners.write().expect("EventEmitter lock poisoned");
let callbacks = listeners.entry(event_type.to_string()).or_default();
let id = {
let mut counter = self.id_counter.lock();
*counter += 1;
*counter
};
callbacks.push(ListenerEntry {
id,
callback: Arc::new(callback),
});
id
}
/// Remove an event listener by ID
///
/// # Panics
/// This function will panic if the lock is poisoned, which should never happen
/// in practice since we don't panic while holding the lock.
pub fn off(&self, event_type: &str, id: u64) {
let mut listeners = self.listeners.write().expect("EventEmitter lock poisoned");
if let Some(callbacks) = listeners.get_mut(event_type) {
callbacks.retain(|entry| entry.id != id);
// Remove empty event types
if callbacks.is_empty() {
listeners.remove(event_type);
}
}
}
/// Remove all listeners for an event type
///
/// # Panics
/// This function will panic if the lock is poisoned, which should never happen
/// in practice since we don't panic while holding the lock.
pub fn off_all(&self, event_type: &str) {
let mut listeners = self.listeners.write().expect("EventEmitter lock poisoned");
listeners.remove(event_type);
}
/// Emit an event
///
/// # Panics
/// This function will panic if the lock is poisoned, which should never happen
/// in practice since we don't panic while holding the lock.
pub fn emit(&self, event: &Event) {
let listeners = self.listeners.read().expect("EventEmitter lock poisoned");
if let Some(callbacks) = listeners.get(&event.event_type) {
// Clone callbacks to avoid holding lock during execution
let callbacks: Vec<Arc<EventCallback>> = callbacks
.iter()
.map(|entry| entry.callback.clone())
.collect();
drop(listeners); // Release lock before calling callbacks
for callback in callbacks.iter() {
callback(event);
}
}
}
/// Remove all listeners for an event type
pub fn remove_all_listeners(&self, event_type: &str) {
self.off_all(event_type);
}
/// Get count of listeners for an event type
///
/// # Panics
/// This function will panic if the lock is poisoned, which should never happen
/// in practice since we don't panic while holding the lock.
pub fn listener_count(&self, event_type: &str) -> usize {
let listeners = self.listeners.read().expect("EventEmitter lock poisoned");
listeners.get(event_type).map(|v| v.len()).unwrap_or(0)
}
}
impl Default for EventEmitter {
fn default() -> Self {
Self::new()
}
}