feat(managed): automatically unregister dropped event subscriptions
This commit is contained in:
1 file changed
+20
-2
@@ -2,7 +2,7 @@ use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
Arc, Condvar, Mutex,
|
||||
Arc, Condvar, Mutex, Weak,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -43,6 +43,8 @@ struct HubInner {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EventSubscription {
|
||||
id: u64,
|
||||
hub: Weak<HubInner>,
|
||||
inner: Arc<SubscriberInner>,
|
||||
}
|
||||
|
||||
@@ -94,7 +96,11 @@ impl EventHub {
|
||||
.expect("event hub mutex poisoned")
|
||||
.insert(id, inner.clone());
|
||||
|
||||
Ok(EventSubscription { inner })
|
||||
Ok(EventSubscription {
|
||||
id,
|
||||
hub: Arc::downgrade(&self.inner),
|
||||
inner,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn publish(&self, event: ClientEvent) {
|
||||
@@ -180,6 +186,18 @@ impl EventSubscription {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for EventSubscription {
|
||||
fn drop(&mut self) {
|
||||
self.inner.disconnect();
|
||||
if let Some(hub) = self.hub.upgrade() {
|
||||
hub.subscribers
|
||||
.lock()
|
||||
.expect("event hub mutex poisoned")
|
||||
.remove(&self.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SubscriberInner {
|
||||
fn publish(&self, event: ClientEvent) -> bool {
|
||||
let mut state = self.state.lock().expect("subscriber mutex poisoned");
|
||||
|
||||
Reference in new issue
Block a user