biz_handler.rs 759 B

1234567891011121314151617181920212223242526272829303132
  1. use crate::service::ws::WsClientData;
  2. use flowy_ws::WsModule;
  3. use std::{collections::HashMap, sync::Arc};
  4. pub trait WsBizHandler: Send + Sync {
  5. fn receive_data(&self, client_data: WsClientData);
  6. }
  7. pub type BizHandler = Arc<dyn WsBizHandler>;
  8. pub struct WsBizHandlers {
  9. inner: HashMap<WsModule, BizHandler>,
  10. }
  11. impl WsBizHandlers {
  12. pub fn new() -> Self {
  13. Self {
  14. inner: HashMap::new(),
  15. }
  16. }
  17. pub fn register(&mut self, source: WsModule, handler: BizHandler) {
  18. self.inner.insert(source, handler);
  19. }
  20. pub fn get(&self, source: &WsModule) -> Option<BizHandler> {
  21. match self.inner.get(source) {
  22. None => None,
  23. Some(handler) => Some(handler.clone()),
  24. }
  25. }
  26. }