Skip to main content

pamoja_core/
bus.rs

1//! A typed publish/subscribe event bus used internally and by application code.
2
3use crate::error::Result;
4
5/// A typed publish/subscribe channel carrying events of a single type.
6///
7/// Producers such as sensors and transports publish events, and consumers await
8/// them. The event type is fixed per bus so that delivery is statically typed.
9pub trait EventBus {
10    /// The event type carried by this bus.
11    type Event;
12
13    /// Publishes an event to every current subscriber.
14    ///
15    /// # Arguments
16    ///
17    /// * `event` - the event to broadcast, consumed by the call.
18    ///
19    /// # Returns
20    ///
21    /// `Ok(())` once the event has been accepted for delivery.
22    ///
23    /// # Errors
24    ///
25    /// Returns [`Error::Closed`](crate::Error::Closed) if the bus has been shut
26    /// down.
27    async fn publish(&self, event: Self::Event) -> Result<()>;
28
29    /// Awaits the next event for this subscriber.
30    ///
31    /// # Returns
32    ///
33    /// `Some(event)` when an event is available, or `None` once the bus is closed.
34    ///
35    /// # Errors
36    ///
37    /// Returns [`Error::Closed`](crate::Error::Closed) if the bus has been shut
38    /// down unexpectedly.
39    async fn next_event(&mut self) -> Result<Option<Self::Event>>;
40}