// Really basic implementation of the observer pattern using mpsc channels. // Currently unbounded channels use tokio::sync::mpsc; #[derive(Debug)] pub struct Dispatcher where T: Clone, { senders: Vec>, } impl Default for Dispatcher where T: Clone, { fn default() -> Self { Self { senders: Default::default(), } } } impl Dispatcher where T: Clone, { pub fn register(&mut self) -> mpsc::UnboundedReceiver { let (tx, rx) = mpsc::unbounded_channel(); self.senders.push(tx); rx } pub fn dispatch(&mut self, msg: &T) { self.senders .retain(|sender| sender.send(msg.clone()).is_ok()); } }