AppFlowy/backend/src/service/ws/biz_handler.rs

27 lines
712 B
Rust
Raw Normal View History

2021-09-24 16:02:17 +08:00
use crate::service::ws::WsClientData;
use lib_ws::WsModule;
use std::{collections::HashMap, sync::Arc};
2021-09-23 17:50:28 +08:00
pub trait WsBizHandler: Send + Sync {
2021-09-24 16:02:17 +08:00
fn receive_data(&self, client_data: WsClientData);
2021-09-23 17:50:28 +08:00
}
pub type BizHandler = Arc<dyn WsBizHandler>;
2021-09-23 17:50:28 +08:00
pub struct WsBizHandlers {
inner: HashMap<WsModule, BizHandler>,
2021-09-23 17:50:28 +08:00
}
impl WsBizHandlers {
pub fn new() -> Self { Self { inner: HashMap::new() } }
2021-09-23 17:50:28 +08:00
pub fn register(&mut self, source: WsModule, handler: BizHandler) { self.inner.insert(source, handler); }
2021-09-23 17:50:28 +08:00
pub fn get(&self, source: &WsModule) -> Option<BizHandler> {
2021-09-23 17:50:28 +08:00
match self.inner.get(source) {
None => None,
Some(handler) => Some(handler.clone()),
}
}
}