Skip to main content

reifydb_core/event/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::{
5	any::{Any, TypeId, type_name},
6	collections::HashMap,
7	sync,
8	sync::Arc,
9};
10
11use reifydb_runtime::actor::{
12	context::Context,
13	mailbox::ActorRef,
14	system::ActorSpawner,
15	traits::{Actor, Directive},
16};
17use sync::mpsc::Sender;
18
19pub mod lifecycle;
20pub mod operator;
21#[macro_use]
22pub mod r#macro;
23pub mod metric;
24pub mod procedure;
25pub mod row;
26pub mod store;
27pub mod transaction;
28
29type EventListenerInstaller = Box<dyn FnOnce(&mut HashMap<TypeId, Box<dyn EventListenerList>>) + Send>;
30
31pub trait Event: Any + Send + Sync + Clone + 'static {
32	fn as_any(&self) -> &dyn Any;
33	fn into_any(self) -> Box<dyn Any + Send>;
34}
35
36pub trait EventListener<E>: Send + Sync + 'static
37where
38	E: Event,
39{
40	fn on(&self, event: &E);
41}
42
43trait EventListenerList: Any + Send + Sync {
44	fn on_any(&self, event: Box<dyn Any + Send>);
45	fn as_any_mut(&mut self) -> &mut dyn Any;
46}
47
48struct EventListenerListImpl<E> {
49	listeners: Vec<Arc<dyn EventListener<E>>>,
50}
51
52impl<E> EventListenerListImpl<E>
53where
54	E: Event,
55{
56	fn new() -> Self {
57		Self {
58			listeners: Vec::new(),
59		}
60	}
61
62	fn add(&mut self, listener: Arc<dyn EventListener<E>>) {
63		self.listeners.push(listener);
64	}
65}
66
67impl<E> EventListenerList for EventListenerListImpl<E>
68where
69	E: Event,
70{
71	fn on_any(&self, event: Box<dyn Any + Send>) {
72		if let Ok(event) = event.downcast::<E>() {
73			for listener in &self.listeners {
74				listener.on(&*event);
75			}
76		}
77	}
78
79	fn as_any_mut(&mut self) -> &mut dyn Any {
80		self
81	}
82}
83
84struct EventEnvelope {
85	type_id: TypeId,
86	event: Box<dyn Any + Send>,
87}
88
89enum EventBusMessage {
90	Emit(EventEnvelope),
91	Register {
92		installer: EventListenerInstaller,
93	},
94	WaitForCompletion(Sender<()>),
95}
96
97struct EventBusActor;
98
99impl Actor for EventBusActor {
100	type State = HashMap<TypeId, Box<dyn EventListenerList>>;
101	type Message = EventBusMessage;
102
103	fn init(&self, _ctx: &Context<Self::Message>) -> Self::State {
104		HashMap::new()
105	}
106
107	fn handle(&self, state: &mut Self::State, msg: Self::Message, _ctx: &Context<Self::Message>) -> Directive {
108		match msg {
109			EventBusMessage::Emit(envelope) => {
110				if let Some(list) = state.get(&envelope.type_id) {
111					list.on_any(envelope.event);
112				}
113			}
114			EventBusMessage::Register {
115				installer,
116			} => {
117				installer(state);
118			}
119			EventBusMessage::WaitForCompletion(tx) => {
120				let _ = tx.send(());
121			}
122		}
123		Directive::Continue
124	}
125}
126
127#[derive(Clone)]
128pub struct EventBus {
129	actor_ref: ActorRef<EventBusMessage>,
130	spawner: ActorSpawner,
131}
132
133impl EventBus {
134	pub fn new(spawner: &ActorSpawner) -> Self {
135		let handle = spawner.spawn_coordination("event-bus", EventBusActor);
136		Self {
137			actor_ref: handle.actor_ref().clone(),
138			spawner: spawner.clone(),
139		}
140	}
141
142	pub fn register<E, L>(&self, listener: L)
143	where
144		E: Event,
145		L: EventListener<E>,
146	{
147		let type_id = TypeId::of::<E>();
148		let listener = Arc::new(listener);
149
150		let installer: EventListenerInstaller = Box::new(move |map| {
151			let list = map.entry(type_id).or_insert_with(|| Box::new(EventListenerListImpl::<E>::new()));
152			list.as_any_mut().downcast_mut::<EventListenerListImpl<E>>().unwrap().add(listener);
153		});
154
155		if self.actor_ref
156			.send(EventBusMessage::Register {
157				installer,
158			})
159			.is_err()
160		{
161			let shutting_down = self.spawner.cancellation_token().is_none_or(|token| token.is_cancelled());
162			assert!(
163				shutting_down,
164				"the event bus rejected a listener for {} while the system was running; the \
165				 listener would never install and every later event of this type is lost silently",
166				type_name::<E>()
167			);
168		}
169	}
170
171	pub fn emit<E>(&self, event: E)
172	where
173		E: Event,
174	{
175		let type_id = TypeId::of::<E>();
176		if self.actor_ref
177			.send(EventBusMessage::Emit(EventEnvelope {
178				type_id,
179				event: event.into_any(),
180			}))
181			.is_err()
182		{
183			let shutting_down = self.spawner.cancellation_token().is_none_or(|token| token.is_cancelled());
184			assert!(
185				shutting_down,
186				"the event bus rejected a {} while the system was running; dropping it silently \
187				 loses a post-commit update no subscriber will ever see",
188				type_name::<E>()
189			);
190		}
191	}
192
193	/// Waits until the bus has drained everything queued ahead of this call.
194	///
195	/// Under dst the caller's thread is also the thread that steps the actor system, so it drives
196	/// the system to quiescence itself and then picks the reply up without blocking. A blocking
197	/// receive here would park the only thread that can run the bus actor, so the reply it waits
198	/// for could never be sent; that deadlocks subsystem start-up before any test can run.
199	#[cfg(reifydb_dst)]
200	pub fn wait_for_completion(&self) {
201		let (tx, rx) = sync::mpsc::channel();
202		if self.actor_ref.send(EventBusMessage::WaitForCompletion(tx)).is_err() {
203			return;
204		}
205		if self.spawner.is_alive() {
206			self.spawner.system().run_until_idle();
207		}
208		let _ = rx.try_recv();
209	}
210
211	/// Waits until the bus has drained everything queued ahead of this call.
212	///
213	/// Off dst the bus actor runs on its own thread (host) or inline on `send` (wasm), so a
214	/// blocking receive is both correct and the cheapest way to wait.
215	#[cfg(not(reifydb_dst))]
216	pub fn wait_for_completion(&self) {
217		let (tx, rx) = sync::mpsc::channel();
218		let _ = self.actor_ref.send(EventBusMessage::WaitForCompletion(tx));
219		let _ = rx.recv();
220	}
221}
222
223#[cfg(test)]
224pub mod tests {
225	use std::{
226		sync::{Arc, Mutex},
227		thread,
228	};
229
230	use reifydb_runtime::{
231		actor::system::ActorSystem,
232		context::clock::Clock,
233		pool::{PoolConfig, Pools},
234	};
235
236	use crate::event::{Event, EventBus, EventListener};
237
238	fn test_actor_system() -> ActorSystem {
239		let pools = Pools::new(PoolConfig::default());
240		ActorSystem::new(pools, Clock::Real)
241	}
242
243	define_event! {
244		pub struct TestEvent{}
245	}
246
247	define_event! {
248		pub struct AnotherEvent{}
249	}
250
251	#[derive(Default, Debug, Clone)]
252	pub struct TestEventListener(Arc<TestHandlerInner>);
253
254	#[derive(Default, Debug)]
255	pub struct TestHandlerInner {
256		pub counter: Arc<Mutex<i32>>,
257	}
258
259	impl EventListener<TestEvent> for TestEventListener {
260		fn on(&self, _event: &TestEvent) {
261			let mut x = self.0.counter.lock().unwrap();
262			*x += 1;
263		}
264	}
265
266	impl EventListener<AnotherEvent> for TestEventListener {
267		fn on(&self, _event: &AnotherEvent) {
268			let mut x = self.0.counter.lock().unwrap();
269			*x *= 2;
270		}
271	}
272
273	#[test]
274	fn test_event_bus_new() {
275		let actor_system = test_actor_system();
276		let event_bus = EventBus::new(&actor_system.spawner());
277		event_bus.emit(TestEvent::new());
278		event_bus.wait_for_completion();
279	}
280
281	#[test]
282	fn test_register_single_listener() {
283		let actor_system = test_actor_system();
284		let event_bus = EventBus::new(&actor_system.spawner());
285		let listener = TestEventListener::default();
286
287		event_bus.register::<TestEvent, TestEventListener>(listener.clone());
288		event_bus.emit(TestEvent::new());
289		event_bus.wait_for_completion();
290		assert_eq!(*listener.0.counter.lock().unwrap(), 1);
291	}
292
293	#[test]
294	fn test_emit_unregistered_event() {
295		let actor_system = test_actor_system();
296		let event_bus = EventBus::new(&actor_system.spawner());
297		event_bus.emit(TestEvent::new());
298		event_bus.wait_for_completion();
299	}
300
301	#[test]
302	fn test_multiple_listeners_same_event() {
303		let actor_system = test_actor_system();
304		let event_bus = EventBus::new(&actor_system.spawner());
305		let listener1 = TestEventListener::default();
306		let listener2 = TestEventListener::default();
307
308		event_bus.register::<TestEvent, TestEventListener>(listener1.clone());
309		event_bus.register::<TestEvent, TestEventListener>(listener2.clone());
310
311		event_bus.emit(TestEvent::new());
312		event_bus.wait_for_completion();
313		assert_eq!(*listener1.0.counter.lock().unwrap(), 1);
314		assert_eq!(*listener2.0.counter.lock().unwrap(), 1);
315	}
316
317	#[test]
318	fn test_event_bus_clone() {
319		let actor_system = test_actor_system();
320		let event_bus1 = EventBus::new(&actor_system.spawner());
321		let listener = TestEventListener::default();
322		event_bus1.register::<TestEvent, TestEventListener>(listener.clone());
323
324		let event_bus2 = event_bus1.clone();
325		event_bus2.emit(TestEvent::new());
326		event_bus2.wait_for_completion();
327		assert_eq!(*listener.0.counter.lock().unwrap(), 1);
328	}
329
330	#[test]
331	fn test_concurrent_registration() {
332		let actor_system = test_actor_system();
333		let event_bus = Arc::new(EventBus::new(&actor_system.spawner()));
334		let mut handles = Vec::new();
335
336		for _ in 0..10 {
337			let event_bus = event_bus.clone();
338			handles.push(thread::spawn(move || {
339				let listener = TestEventListener::default();
340				event_bus.register::<TestEvent, TestEventListener>(listener);
341			}));
342		}
343
344		for handle in handles {
345			handle.join().unwrap();
346		}
347
348		event_bus.emit(TestEvent::new());
349		event_bus.wait_for_completion();
350	}
351
352	#[test]
353	fn test_concurrent_emitting() {
354		let actor_system = test_actor_system();
355		let event_bus = Arc::new(EventBus::new(&actor_system.spawner()));
356		let listener = TestEventListener::default();
357		event_bus.register::<TestEvent, TestEventListener>(listener.clone());
358		event_bus.wait_for_completion();
359
360		let mut handles = Vec::new();
361
362		for _ in 0..10 {
363			let event_bus = event_bus.clone();
364			handles.push(thread::spawn(move || {
365				event_bus.emit(TestEvent::new());
366			}));
367		}
368
369		for handle in handles {
370			handle.join().unwrap();
371		}
372
373		event_bus.wait_for_completion();
374		assert_eq!(*listener.0.counter.lock().unwrap(), 10);
375	}
376
377	define_event! {
378		pub struct MacroTestEvent {
379			pub value: i32,
380		}
381	}
382
383	#[test]
384	fn testine_event_macro() {
385		let event = MacroTestEvent::new(42);
386		let any_ref = event.as_any();
387		assert!(any_ref.downcast_ref::<MacroTestEvent>().is_some());
388		assert_eq!(any_ref.downcast_ref::<MacroTestEvent>().unwrap().value(), &42);
389	}
390
391	#[test]
392	fn test_multi_event_listener() {
393		let actor_system = test_actor_system();
394		let event_bus = EventBus::new(&actor_system.spawner());
395		let listener = TestEventListener::default();
396
397		event_bus.register::<TestEvent, TestEventListener>(listener.clone());
398		event_bus.register::<AnotherEvent, TestEventListener>(listener.clone());
399
400		// Each event type triggers only its own listeners
401		event_bus.emit(TestEvent::new());
402		event_bus.wait_for_completion();
403		assert_eq!(*listener.0.counter.lock().unwrap(), 1);
404
405		event_bus.emit(TestEvent::new());
406		event_bus.wait_for_completion();
407		assert_eq!(*listener.0.counter.lock().unwrap(), 2);
408
409		event_bus.emit(AnotherEvent::new());
410		event_bus.wait_for_completion();
411		assert_eq!(*listener.0.counter.lock().unwrap(), 4); // 2 * 2
412	}
413}