From bf946b3876e8edb6e81925c4eeb10edce66ce11c Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sat, 25 Jun 2022 15:41:50 +0300 Subject: [PATCH] system: add self_notify function to Subscription --- src/system/subscriber.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/system/subscriber.rs b/src/system/subscriber.rs index b5332e157..e5d31a8af 100644 --- a/src/system/subscriber.rs +++ b/src/system/subscriber.rs @@ -11,6 +11,7 @@ pub type SubscriptionId = u64; pub struct Subscription { id: SubscriptionId, recv_queue: async_channel::Receiver, + send_queue: async_channel::Sender, parent: Arc>, } @@ -26,6 +27,15 @@ impl Subscription { } } + pub async fn self_notify(&self, message: T) { + match self.send_queue.send(message).await { + Ok(_) => {} + Err(err) => { + panic!("MessageSubscription::self_notify() send_queue failed! {}", err); + } + } + } + // Must be called manually since async Drop is not possible in Rust pub async fn unsubscribe(&self) { self.parent.clone().unsubscribe(self.id).await @@ -52,9 +62,9 @@ impl Subscriber { let sub_id = Self::random_id(); - self.subs.lock().await.insert(sub_id, sender); + self.subs.lock().await.insert(sub_id, sender.clone()); - Subscription { id: sub_id, recv_queue: recvr, parent: self.clone() } + Subscription { id: sub_id, recv_queue: recvr, send_queue: sender, parent: self.clone() } } async fn unsubscribe(self: Arc, sub_id: SubscriptionId) {