Skip to main content

mcpr_core/event/
manager.rs

1//! `EventManager` — collects registered sinks and starts the bus.
2//!
3//! ```rust,ignore
4//! let mut manager = EventManager::new();
5//! manager.register(Box::new(StderrSink::new(fmt)));
6//! manager.register(Box::new(SqliteSink::new(store)));
7//! let handle = manager.start();
8//! let bus = handle.bus.clone();
9//! // … use bus.emit(event) from the proxy hot path …
10//! handle.shutdown().await;
11//! ```
12
13use super::bus::{self, EventBusHandle};
14use super::sink::EventSink;
15
16/// Builder that holds the set of sinks to register before the bus starts.
17#[derive(Default)]
18pub struct EventManager {
19    sinks: Vec<Box<dyn EventSink>>,
20}
21
22impl EventManager {
23    pub fn new() -> Self {
24        Self { sinks: Vec::new() }
25    }
26
27    /// Register a sink. Sinks receive events in the order they were registered.
28    pub fn register(&mut self, sink: Box<dyn EventSink>) -> &mut Self {
29        self.sinks.push(sink);
30        self
31    }
32
33    /// Number of sinks currently registered.
34    pub fn len(&self) -> usize {
35        self.sinks.len()
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.sinks.is_empty()
40    }
41
42    /// Spawn the background dispatch task and return a live handle.
43    pub fn start(self) -> EventBusHandle {
44        bus::spawn(self.sinks)
45    }
46}