Multithreaded
This commit is contained in:
+11
-2
@@ -5,10 +5,19 @@ use osui::prelude::*;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut cx = Context::new(app);
|
let mut cx = Context::new(app);
|
||||||
cx.refresh();
|
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<Node> {
|
fn app(cx: &Arc<Context>) -> Vec<Node> {
|
||||||
let count = use_state(0);
|
let count = use_state(0);
|
||||||
|
|
||||||
vec![Node::String(Arc::new(move || format!("{count}")))]
|
vec![Node::String(Arc::new(move || format!("{count}")))]
|
||||||
|
|||||||
+14
-13
@@ -1,27 +1,28 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use crate::Node;
|
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 {
|
pub struct Context {
|
||||||
component: Component,
|
component: Mutex<Component>,
|
||||||
nodes: Vec<Node>,
|
nodes: Mutex<Vec<Node>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
pub fn new<F: Fn(&mut Context) -> Vec<Node> + 'static>(component: F) -> Self {
|
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + 'static>(component: F) -> Arc<Self> {
|
||||||
Self {
|
Arc::new(Self {
|
||||||
component: Arc::new(component),
|
component: Mutex::new(Arc::new(component)),
|
||||||
nodes: Vec::new(),
|
nodes: Mutex::new(Vec::new()),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh(&mut self) {
|
pub fn refresh(self: &Arc<Context>) {
|
||||||
self.nodes = (self.component.clone())(self);
|
let c = self.component.lock().unwrap().clone();
|
||||||
|
*self.nodes.lock().unwrap() = (c)(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_nodes(&self) -> Vec<Node> {
|
pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> {
|
||||||
self.nodes.clone()
|
self.nodes.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user