use std::sync::Arc; use dashmap::DashMap; use tokio::sync::mpsc; use uuid::Uuid; use super::WsOutbound; pub type WsSender = mpsc::UnboundedSender; pub type WsReceiver = mpsc::UnboundedReceiver; #[derive(Clone, Default)] pub struct WsSinkManager { sinks: Arc>, } impl WsSinkManager { pub fn new() -> Self { Self::default() } pub fn channel() -> (WsSender, WsReceiver) { mpsc::unbounded_channel() } pub fn attach(&self, connection_id: Uuid, sender: WsSender) { self.sinks.insert(connection_id, sender); } pub fn detach(&self, connection_id: Uuid) { self.sinks.remove(&connection_id); } pub fn send(&self, connection_id: Uuid, message: WsOutbound) -> bool { self.sinks .get(&connection_id) .map(|sink| sink.send(message).is_ok()) .unwrap_or(false) } pub fn send_many(&self, ids: I, message: WsOutbound) -> usize where I: IntoIterator, { ids.into_iter() .filter(|id| self.send(*id, message.clone())) .count() } pub fn contains(&self, connection_id: Uuid) -> bool { self.sinks.contains_key(&connection_id) } }