feat: LifeLinkedNodeList

This commit is contained in:
Nova
2022-06-19 19:37:28 -04:00
parent d1f6a34ac8
commit bbaf95b79f
3 changed files with 36 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
pub mod client;
pub mod eventloop;
pub mod nodelist;
pub mod registry;
pub mod scenegraph;

31
src/core/nodelist.rs Normal file
View File

@@ -0,0 +1,31 @@
use crate::nodes::core::Node;
use anyhow::{anyhow, ensure, Result};
use parking_lot::RwLock;
use std::sync::Weak;
#[derive(Default)]
pub struct LifeLinkedNodeList {
nodes: RwLock<Vec<Weak<Node>>>,
}
impl LifeLinkedNodeList {
pub fn add(&self, node: Weak<Node>) {
self.nodes.write().push(node);
}
pub fn clear(&self) {
self.nodes
.read()
.iter()
.filter_map(|node| node.upgrade())
.filter_map(|node| node.get_client().zip(Some(node.get_path().to_string())))
.for_each(|(client, path)| {
client.scenegraph.remove_node(&path);
});
self.nodes.write().clear();
}
}
impl Drop for LifeLinkedNodeList {
fn drop(&mut self) {
self.clear();
}
}