feat: init
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use dashmap::DashMap;
|
||||
use tokio::sync::mpsc;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::WsOutbound;
|
||||
|
||||
pub type WsSender = mpsc::UnboundedSender<WsOutbound>;
|
||||
pub type WsReceiver = mpsc::UnboundedReceiver<WsOutbound>;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct WsSinkManager {
|
||||
sinks: Arc<DashMap<Uuid, WsSender>>,
|
||||
}
|
||||
|
||||
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<I>(&self, ids: I, message: WsOutbound) -> usize
|
||||
where
|
||||
I: IntoIterator<Item = Uuid>,
|
||||
{
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user