diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 1e45d48..5ebf3d8 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -5,10 +5,19 @@ use osui::prelude::*; fn main() { let mut cx = Context::new(app); cx.refresh(); - println!("{:?}", cx.get_nodes()); + + std::thread::spawn({ + let cx = cx.clone(); + move || {} + }); + + loop { + println!("{:?}", cx.get_nodes()); + std::thread::sleep(std::time::Duration::from_millis(16)); + } } -fn app(cx: &mut Context) -> Vec { +fn app(cx: &Arc) -> Vec { let count = use_state(0); vec![Node::String(Arc::new(move || format!("{count}")))] diff --git a/src/component.rs b/src/component.rs index 2a5d095..9134446 100644 --- a/src/component.rs +++ b/src/component.rs @@ -1,27 +1,28 @@ -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use crate::Node; -pub type Component = Arc Vec>; +pub type Component = Arc) -> Vec>; pub struct Context { - component: Component, - nodes: Vec, + component: Mutex, + nodes: Mutex>, } impl Context { - pub fn new Vec + 'static>(component: F) -> Self { - Self { - component: Arc::new(component), - nodes: Vec::new(), - } + pub fn new) -> Vec + 'static>(component: F) -> Arc { + 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) { + let c = self.component.lock().unwrap().clone(); + *self.nodes.lock().unwrap() = (c)(self); } - pub fn get_nodes(&self) -> Vec { - self.nodes.clone() + pub fn get_nodes(self: &Arc) -> Vec { + self.nodes.lock().unwrap().clone() } }