pub struct EventBus { /* private fields */ }Expand description
A fan-out event bus backed by a bounded broadcast channel.
Each subscriber receives an independent broadcast::Receiver to drain on
its own task. Sending never blocks and no lock is held while receivers handle
events. If a receiver falls behind its bounded buffer, Tokio’s broadcast
channel reports a lag error to that receiver on its next receive.
Implementations§
Source§impl EventBus
impl EventBus
Sourcepub const DEFAULT_CAPACITY: usize = 256
pub const DEFAULT_CAPACITY: usize = 256
The default event buffer capacity.
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a bus with the given bounded buffer capacity.
Tokio broadcast channels require a non-zero capacity, so a capacity of zero is treated as one.
Sourcepub fn new_default() -> Self
pub fn new_default() -> Self
Create a bus with the default buffer capacity.
Sourcepub fn subscribe(&self) -> Receiver<RunEvent>
pub fn subscribe(&self) -> Receiver<RunEvent>
Subscribe to future events.
The caller owns the returned receiver and should drain it, typically on a dedicated task.
Sourcepub fn emit(&self, event: RunEvent) -> bool
pub fn emit(&self, event: RunEvent) -> bool
Emit an event to all current subscribers.
Returns false when there are no active receivers. Sending is
non-blocking; slow receivers observe broadcast::error::RecvError::Lagged
when they next receive.