pub struct EventBus { /* private fields */ }Expand description
A broadcast event bus that fans out GameEvent values to subscribers.
Wraps a tokio::sync::broadcast channel. The bus owns the sender half;
each call to subscribe creates a new receiver that
independently tracks its read position.
§Capacity
The channel has a fixed capacity set at construction time (default 256).
When the channel is full the oldest message is overwritten, and any
subscriber that has not yet read it will receive a lag notification on
its next recv().
Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a new event bus with the given channel capacity.
capacity is the maximum number of events that can be buffered
before the oldest event is overwritten. Values below 1 are clamped
to 1 (the minimum tokio::broadcast allows).
Sourcepub fn with_default_capacity() -> Self
pub fn with_default_capacity() -> Self
Creates a new event bus with the default capacity (256).
Sourcepub fn send(&self, event: GameEvent) -> usize
pub fn send(&self, event: GameEvent) -> usize
Sends a GameEvent to all current subscribers.
Returns the number of subscribers that received the event. If there
are no active subscribers the event is silently dropped and 0 is
returned.
Sourcepub fn subscribe(&self) -> Subscriber
pub fn subscribe(&self) -> Subscriber
Creates a new Subscriber that will receive all future events.
Subscribers can be added at any time. A new subscriber starts
receiving events from the next send() call; it does not see
events that were sent before it subscribed.
Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Returns the current number of active subscribers (receivers).