rok_broadcasting/
driver.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use tokio::sync::broadcast;
5
6use crate::{BroadcastError, BroadcastEvent};
7
8#[async_trait]
13pub trait BroadcastDriver: Send + Sync + 'static {
14 async fn broadcast(&self, event: BroadcastEvent) -> Result<(), BroadcastError>;
15}
16
17pub struct InMemoryDriver {
24 tx: broadcast::Sender<BroadcastEvent>,
25}
26
27impl Default for InMemoryDriver {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl InMemoryDriver {
34 pub fn new() -> Self {
35 let (tx, _) = broadcast::channel(1024);
36 Self { tx }
37 }
38
39 pub fn subscribe(&self) -> broadcast::Receiver<BroadcastEvent> {
41 self.tx.subscribe()
42 }
43}
44
45#[async_trait]
46impl BroadcastDriver for InMemoryDriver {
47 async fn broadcast(&self, event: BroadcastEvent) -> Result<(), BroadcastError> {
48 let _ = self.tx.send(event);
49 Ok(())
50 }
51}
52
53pub type SharedDriver = Arc<dyn BroadcastDriver>;