pub struct EventBus { /* private fields */ }Expand description
Generic event bus that dispatches events through staged handlers.
Handlers register for specific event types at specific stages.
During dispatch, handlers run in (Stage, priority) order.
If any Validate handler cancels the event, Process and Post
handlers are skipped.
The bus is Send + Sync and can be shared via Arc across
connection tasks. Registration happens at startup; dispatch
happens per-task.
Implementations§
Source§impl EventBus
impl EventBus
Sourcepub fn on<E>(
&mut self,
stage: Stage,
priority: i32,
handler: impl Fn(&mut E, &dyn Context) + Send + Sync + 'static,
)where
E: Event + 'static,
pub fn on<E>(
&mut self,
stage: Stage,
priority: i32,
handler: impl Fn(&mut E, &dyn Context) + Send + Sync + 'static,
)where
E: Event + 'static,
Registers a handler for a specific event type at a given stage.
The handler receives a mutable reference to the concrete event
and a &dyn Context reference. Lower priority values run first
within the same stage.
E must be 'static for type erasure via Any. The handler
closure must be Send + Sync since the bus is shared across
connection tasks.
Sourcepub fn dispatch<E>(&self, event: &mut E, ctx: &dyn Context)where
E: Event + 'static,
pub fn dispatch<E>(&self, event: &mut E, ctx: &dyn Context)where
E: Event + 'static,
Dispatches a concrete event through all registered handlers.
Runs handlers in (Stage, priority) order. If the event is
cancelled during Validate, Process and Post are skipped.
Sourcepub fn dispatch_dyn(&self, event: &mut dyn Event, ctx: &dyn Context)
pub fn dispatch_dyn(&self, event: &mut dyn Event, ctx: &dyn Context)
Dispatches a type-erased event using its runtime TypeId.
Used when the concrete event type is not known at the call
site (e.g., Box<dyn Event> from packet_to_event).
Sourcepub fn event_type_count(&self) -> usize
pub fn event_type_count(&self) -> usize
Returns the number of event types that have registered handlers.
Sourcepub fn handler_count(&self) -> usize
pub fn handler_count(&self) -> usize
Returns the total number of registered handler entries.