Browse Source

system: add self_notify function to Subscription

ghassmo 4 years ago
parent
commit
bf946b3876
1 changed files with 12 additions and 2 deletions
  1. 12 2
      src/system/subscriber.rs

+ 12 - 2
src/system/subscriber.rs

@@ -11,6 +11,7 @@ pub type SubscriptionId = u64;
 pub struct Subscription<T> {
     id: SubscriptionId,
     recv_queue: async_channel::Receiver<T>,
+    send_queue: async_channel::Sender<T>,
     parent: Arc<Subscriber<T>>,
 }
 
@@ -26,6 +27,15 @@ impl<T: Clone> Subscription<T> {
         }
     }
 
+    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<T: Clone> Subscriber<T> {
 
         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<Self>, sub_id: SubscriptionId) {