Properly cleanup node when disappearing
This commit is contained in:
parent
1a3c02a577
commit
3b74ea2bfc
27
src/main.rs
27
src/main.rs
@ -24,8 +24,8 @@ fn main() -> Result<()> {
|
||||
let registry = Rc::new(core.get_registry()?);
|
||||
let registry_weak = Rc::downgrade(®istry);
|
||||
|
||||
let nodes: RefCell<HashMap<u32, Box<dyn pw::proxy::ProxyT>>> = RefCell::new(HashMap::new());
|
||||
let listeners: RefCell<HashMap<u32, Vec<Box<dyn pw::proxy::Listener>>>> = RefCell::new(HashMap::new());
|
||||
let nodes: Rc<RefCell<HashMap<u32, Box<dyn pw::proxy::ProxyT>>>> = Rc::new(RefCell::new(HashMap::new()));
|
||||
let listeners: Rc<RefCell<HashMap<u32, Vec<Box<dyn pw::proxy::Listener>>>>> = Rc::new(RefCell::new(HashMap::new()));
|
||||
|
||||
// Listen for new nodes
|
||||
let _registry_listener = registry
|
||||
@ -46,10 +46,27 @@ fn main() -> Result<()> {
|
||||
})
|
||||
.register();
|
||||
|
||||
let node_id = node.upcast_ref().id();
|
||||
let proxy = node.upcast_ref();
|
||||
let proxy_id = proxy.id();
|
||||
|
||||
nodes.borrow_mut().insert(node_id, Box::new(node));
|
||||
listeners.borrow_mut().entry(node_id).or_default().push(Box::new(node_listener));
|
||||
let nodes_weak = Rc::downgrade(&nodes);
|
||||
let listeners_weak = Rc::downgrade(&listeners);
|
||||
|
||||
let proxy_listener = proxy
|
||||
.add_listener_local()
|
||||
.removed(move || {
|
||||
if let Some(nodes) = nodes_weak.upgrade() {
|
||||
nodes.borrow_mut().remove(&proxy_id);
|
||||
}
|
||||
if let Some(listeners) = listeners_weak.upgrade() {
|
||||
listeners.borrow_mut().remove(&proxy_id);
|
||||
}
|
||||
})
|
||||
.register();
|
||||
|
||||
nodes.borrow_mut().insert(proxy_id, Box::new(node));
|
||||
listeners.borrow_mut().entry(proxy_id).or_default().push(Box::new(node_listener));
|
||||
listeners.borrow_mut().entry(proxy_id).or_default().push(Box::new(proxy_listener));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user