Multithreaded
This commit is contained in:
+14
-13
@@ -1,27 +1,28 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::Node;
|
||||
|
||||
pub type Component = Arc<dyn Fn(&mut Context) -> Vec<Node>>;
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node>>;
|
||||
|
||||
pub struct Context {
|
||||
component: Component,
|
||||
nodes: Vec<Node>,
|
||||
component: Mutex<Component>,
|
||||
nodes: Mutex<Vec<Node>>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new<F: Fn(&mut Context) -> Vec<Node> + 'static>(component: F) -> Self {
|
||||
Self {
|
||||
component: Arc::new(component),
|
||||
nodes: Vec::new(),
|
||||
}
|
||||
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + 'static>(component: F) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
component: Mutex::new(Arc::new(component)),
|
||||
nodes: Mutex::new(Vec::new()),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
self.nodes = (self.component.clone())(self);
|
||||
pub fn refresh(self: &Arc<Context>) {
|
||||
let c = self.component.lock().unwrap().clone();
|
||||
*self.nodes.lock().unwrap() = (c)(self);
|
||||
}
|
||||
|
||||
pub fn get_nodes(&self) -> Vec<Node> {
|
||||
self.nodes.clone()
|
||||
pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> {
|
||||
self.nodes.lock().unwrap().clone()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user