pub struct HookRegistry { /* private fields */ }Expand description
A thread-safe registry that maps typed events to ordered observe-only handlers.
Registration and emission are constructor-injected through registry values;
there is no global hook registry. Handlers for the same event type run in
registration order. A fatal HookError short-circuits the remaining
handlers for that event type; non-fatal errors are collected and the first
non-fatal error is returned after all handlers run.
Implementations§
Source§impl HookRegistry
impl HookRegistry
Sourcepub fn on<E>(
&self,
event_type: EventType,
handler: impl Fn(CancellationToken, &E) -> HookResult + Send + Sync + 'static,
) -> Box<dyn FnOnce() + Send>where
E: Event,
pub fn on<E>(
&self,
event_type: EventType,
handler: impl Fn(CancellationToken, &E) -> HookResult + Send + Sync + 'static,
) -> Box<dyn FnOnce() + Send>where
E: Event,
Register a typed handler for event E.
The returned function unregisters this handler when invoked.
Sourcepub fn emit<E>(&self, event: &E, cancel: CancellationToken) -> HookResultwhere
E: Event,
pub fn emit<E>(&self, event: &E, cancel: CancellationToken) -> HookResultwhere
E: Event,
Emit an event to all registered handlers for its concrete type.
Dispatch uses snapshot semantics: handlers registered or removed during an in-flight emit do not affect that emit, but apply to subsequent emits.
Sourcepub fn has_handlers<E>(&self, event_type: &EventType) -> boolwhere
E: Event,
pub fn has_handlers<E>(&self, event_type: &EventType) -> boolwhere
E: Event,
Check whether any handlers are registered for event E and event_type.