navi_core/runtime/
event_bus.rs1use crate::event::{RuntimeEvent, RuntimeEventKind};
2use tokio::sync::broadcast;
3
4#[derive(Clone)]
5pub struct EventBus {
6 tx: broadcast::Sender<RuntimeEvent>,
7}
8
9impl Default for EventBus {
10 fn default() -> Self {
11 Self::new()
12 }
13}
14
15impl EventBus {
16 pub fn new() -> Self {
17 let (tx, _) = broadcast::channel(256);
18 Self { tx }
19 }
20
21 pub fn stream_events(&self) -> broadcast::Receiver<RuntimeEvent> {
22 self.tx.subscribe()
23 }
24
25 pub fn publish(&self, kind: RuntimeEventKind) {
26 let _ = self.tx.send(RuntimeEvent::new(kind));
27 }
28
29 pub fn sender(&self) -> broadcast::Sender<RuntimeEvent> {
30 self.tx.clone()
31 }
32}