pub struct EventBus { /* private fields */ }Expand description
Broadcast-based event bus for the framework
Uses tokio::sync::broadcast which allows multiple receivers and is
designed for exactly this kind of pub/sub pattern.
The bus is cheap to clone (Arc internally) and can be shared across threads.
§EventLog Bridge
When an EventLog is attached via with_event_log(), every published event
is also appended to the persistent log. The EventLog becomes the source of
truth, while the broadcast channel remains the real-time notification path.
publish(event) ──┬──▶ broadcast channel (real-time, fire-and-forget)
└──▶ EventLog.append() (persistent, replayable)Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a new EventBus with the given channel capacity
The capacity determines how many events can be buffered before slow receivers start losing events (lagged).
§Arguments
capacity- Buffer size for the broadcast channel (recommended: 1024)
Sourcepub fn with_event_log(self, event_log: Arc<dyn EventLog>) -> Self
pub fn with_event_log(self, event_log: Arc<dyn EventLog>) -> Self
Attach a persistent EventLog to this bus
When set, every publish() call also appends the event to the log.
The append is done via tokio::spawn to avoid blocking the publisher.
This enables the event flow system to consume events from the durable log instead of the ephemeral broadcast channel.
Sourcepub fn event_log(&self) -> Option<&Arc<dyn EventLog>>
pub fn event_log(&self) -> Option<&Arc<dyn EventLog>>
Get a reference to the attached EventLog, if any
Sourcepub fn publish(&self, event: FrameworkEvent) -> usize
pub fn publish(&self, event: FrameworkEvent) -> usize
Publish an event to all subscribers
This is non-blocking and will never fail. If there are no subscribers,
the event is simply dropped. If subscribers are lagging, they will
receive a Lagged error on their next recv().
If an EventLog is attached, the event is also appended to the log asynchronously (fire-and-forget via tokio::spawn).
Returns the number of broadcast receivers that will receive the event.
Sourcepub fn subscribe(&self) -> Receiver<EventEnvelope>
pub fn subscribe(&self) -> Receiver<EventEnvelope>
Subscribe to events
Returns a receiver that will get all future events published to the bus. Events published before this call are not received.
Sourcepub fn receiver_count(&self) -> usize
pub fn receiver_count(&self) -> usize
Get the current number of active subscribers