publish client-sdk-native (#12)
* Create README.md * add example * crates & examples * fix syntax * add LICENSE * thirdparty LICENSE * rearrange repo * fix build * egui versions * prepare publish * fix demo compilation * add test ci * Update rust.yml * forgot runs-on * install protoc before building * avoid rate limit * include submodules * updates to readme * cache rust builds Co-authored-by: David Zhao <[email protected]> Co-authored-by: David Zhao <[email protected]>
This commit is contained in:
co-authored by
David Zhao
David Zhao
parent
a927baac94
commit
a07b3451a3
@@ -0,0 +1,39 @@
|
||||
// Really basic implementation of the observer pattern using mpsc channels.
|
||||
// Currently unbounded channels
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
senders: Vec<mpsc::UnboundedSender<T>>,
|
||||
}
|
||||
|
||||
impl<T> Default for Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
senders: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Dispatcher<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
pub fn register(&mut self) -> mpsc::UnboundedReceiver<T> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user