Skip to main content

ciab_core/traits/
stream.rs

1use async_trait::async_trait;
2use tokio::sync::broadcast;
3use uuid::Uuid;
4
5use crate::error::CiabResult;
6use crate::types::stream::StreamEvent;
7
8#[async_trait]
9pub trait StreamHandler: Send + Sync {
10    /// Publish an event for a sandbox.
11    async fn publish(&self, event: StreamEvent) -> CiabResult<()>;
12
13    /// Subscribe to events for a sandbox, returning a broadcast receiver.
14    async fn subscribe(&self, sandbox_id: &Uuid) -> CiabResult<broadcast::Receiver<StreamEvent>>;
15
16    /// Subscribe to events and replay buffered events for reconnection support.
17    /// Returns `(replayed_events, live_receiver)`.
18    ///
19    /// When `last_event_id` is provided, replays events after that ID.
20    /// When `None`, replays the entire buffer so reconnecting clients catch up.
21    async fn subscribe_with_replay(
22        &self,
23        sandbox_id: &Uuid,
24        last_event_id: Option<&str>,
25    ) -> CiabResult<(Vec<StreamEvent>, broadcast::Receiver<StreamEvent>)>;
26
27    /// Unsubscribe / clean up resources for a sandbox stream.
28    async fn unsubscribe(&self, sandbox_id: &Uuid) -> CiabResult<()>;
29}