multithreading
This commit is contained in:
@@ -6,5 +6,6 @@ use std::sync::Arc;
|
||||
use crate::widget::Widget;
|
||||
|
||||
pub trait Extension {
|
||||
fn init(&self, _widgets: &Vec<Arc<Widget>>) {}
|
||||
fn render(&self, _widget: &Arc<Widget>) {}
|
||||
}
|
||||
|
||||
+14
-5
@@ -2,13 +2,22 @@ use std::sync::Arc;
|
||||
|
||||
use crate::{component, extensions::Extension, widget::Widget};
|
||||
|
||||
pub struct TickExtension;
|
||||
pub struct TickExtension(pub u16);
|
||||
|
||||
impl Extension for TickExtension {
|
||||
fn render(&self, _widget: &Arc<Widget>) {
|
||||
if let Some(on_tick) = _widget.get::<OnTick>() {
|
||||
(on_tick.0)(_widget)
|
||||
}
|
||||
fn init(&self, widgets: &Vec<Arc<Widget>>) {
|
||||
let rate_dur = 1000 / self.0 as u64;
|
||||
std::thread::spawn({
|
||||
let widgets = widgets.clone();
|
||||
move || loop {
|
||||
for w in &widgets {
|
||||
if let Some(on_tick) = w.get::<OnTick>() {
|
||||
(on_tick.0)(&w)
|
||||
}
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user