Skip to main content

EventBus

Struct EventBus 

Source
pub struct EventBus { /* private fields */ }
Expand description

Central pub/sub hub for routing events between hats.

Implementations§

Source§

impl EventBus

Source

pub fn new() -> Self

Creates a new empty event bus.

Source

pub fn add_observer<F>(&mut self, observer: F)
where F: Fn(&Event) + Send + 'static,

Adds an observer that receives all published events.

Multiple observers can be added (e.g., session recorder + TUI). Each observer is called before events are routed to subscribers. This enables recording sessions by subscribing to the event stream without modifying the routing logic.

Source

pub fn set_observer<F>(&mut self, observer: F)
where F: Fn(&Event) + Send + 'static,

👎Deprecated since 2.0.0:

Use add_observer instead

Sets a single observer, clearing any existing observers.

Prefer add_observer when multiple observers are needed. This method is kept for backwards compatibility.

Source

pub fn clear_observers(&mut self)

Clears all observer callbacks.

Source

pub fn register(&mut self, hat: Hat)

Registers a hat with the event bus.

Source

pub fn publish(&mut self, event: Event) -> Vec<HatId>

Publishes an event to all subscribed hats.

Returns the list of hat IDs that received the event. If an observer is set, it receives the event before routing.

Source

pub fn take_pending(&mut self, hat_id: &HatId) -> Vec<Event>

Takes all pending events for a hat.

Source

pub fn take_human_pending(&mut self) -> Vec<Event>

Takes all pending human interaction events.

Source

pub fn peek_pending(&self, hat_id: &HatId) -> Option<&Vec<Event>>

Returns a reference to pending events for a hat without consuming them.

Source

pub fn peek_human_pending(&self) -> &[Event]

Returns a reference to pending human interaction events without consuming them.

Source

pub fn has_pending(&self) -> bool

Checks if there are any pending events for any hat.

Source

pub fn has_human_pending(&self) -> bool

Checks if there are any pending human interaction events.

Source

pub fn next_hat_with_pending(&self) -> Option<&HatId>

Returns the next hat with pending events. BTreeMap iteration is already sorted by key.

Source

pub fn get_hat(&self, id: &HatId) -> Option<&Hat>

Gets a hat by ID.

Source

pub fn hat_ids(&self) -> impl Iterator<Item = &HatId>

Returns all registered hat IDs.

Trait Implementations§

Source§

impl Default for EventBus

Source§

fn default() -> EventBus

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.