|
@@ -11,7 +11,6 @@ pub type SubscriptionId = u64;
|
|
|
pub struct Subscription<T> {
|
|
pub struct Subscription<T> {
|
|
|
id: SubscriptionId,
|
|
id: SubscriptionId,
|
|
|
recv_queue: async_channel::Receiver<T>,
|
|
recv_queue: async_channel::Receiver<T>,
|
|
|
- send_queue: async_channel::Sender<T>,
|
|
|
|
|
parent: Arc<Subscriber<T>>,
|
|
parent: Arc<Subscriber<T>>,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -31,15 +30,6 @@ 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
|
|
// Must be called manually since async Drop is not possible in Rust
|
|
|
pub async fn unsubscribe(&self) {
|
|
pub async fn unsubscribe(&self) {
|
|
|
self.parent.clone().unsubscribe(self.id).await
|
|
self.parent.clone().unsubscribe(self.id).await
|
|
@@ -66,9 +56,9 @@ impl<T: Clone> Subscriber<T> {
|
|
|
|
|
|
|
|
let sub_id = Self::random_id();
|
|
let sub_id = Self::random_id();
|
|
|
|
|
|
|
|
- self.subs.lock().await.insert(sub_id, sender.clone());
|
|
|
|
|
|
|
+ self.subs.lock().await.insert(sub_id, sender);
|
|
|
|
|
|
|
|
- Subscription { id: sub_id, recv_queue: recvr, send_queue: sender, parent: self.clone() }
|
|
|
|
|
|
|
+ Subscription { id: sub_id, recv_queue: recvr, parent: self.clone() }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async fn unsubscribe(self: Arc<Self>, sub_id: SubscriptionId) {
|
|
async fn unsubscribe(self: Arc<Self>, sub_id: SubscriptionId) {
|
|
@@ -86,6 +76,17 @@ impl<T: Clone> Subscriber<T> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ pub async fn notify_with_id(&self, message_result: T, id: u64) {
|
|
|
|
|
+ if let Some(sub) = (*self.subs.lock().await).get(&id) {
|
|
|
|
|
+ match sub.send(message_result.clone()).await {
|
|
|
|
|
+ Ok(()) => {}
|
|
|
|
|
+ Err(err) => {
|
|
|
|
|
+ panic!("Error returned sending message in notify() call! {}", err);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
pub async fn notify_with_exclude(&self, message_result: T, exclude_list: &[SubscriptionId]) {
|
|
pub async fn notify_with_exclude(&self, message_result: T, exclude_list: &[SubscriptionId]) {
|
|
|
for (id, sub) in (*self.subs.lock().await).iter() {
|
|
for (id, sub) in (*self.subs.lock().await).iter() {
|
|
|
if exclude_list.contains(id) {
|
|
if exclude_list.contains(id) {
|