Expand description
An in-memory typed publish/subscribe event bus.
BroadcastBus implements the core [EventBus] trait
over a bounded broadcast channel: every event published is delivered to every
current subscriber. Producers such as sensors and transports publish events,
and consumers await them, all statically typed to one event type per bus.
EventPublisher is the handle for a part that only announces. It has no
queue of its own, it clones into every task and callback that publishes, and
publishing from it never waits, so it can announce while a subscriber on the
same bus is waiting for its next event.
The bus is bounded, so a subscriber that falls far enough behind drops the
events it missed and resumes from the most recent ones, and
BroadcastBus::missed counts what it lost. This keeps a slow consumer from
holding memory without bound, which matters on constrained devices.
§Examples
A pump controller’s events reach a logger and a dashboard: each subscriber hears every event published after it joined, in order.
use pamoja_bus::BroadcastBus;
use pamoja_core::EventBus;
let bus = BroadcastBus::new(16);
let mut logger = bus.subscribe();
let mut dashboard = bus.subscribe();
bus.publish("pump started").await?;
bus.publish("pressure low").await?;
assert_eq!(logger.next_event().await?, Some("pump started"));
assert_eq!(logger.next_event().await?, Some("pressure low"));
assert_eq!(dashboard.next_event().await?, Some("pump started"));Structs§
- Broadcast
Bus - A typed publish/subscribe bus that broadcasts each event to all subscribers.
- Event
Publisher - A publish-only handle to a bus.
Constants§
- MAX_
CAPACITY - The largest buffer a bus sets aside; a larger capacity is lowered to it.