multithreading

This commit is contained in:
2025-06-29 22:56:53 +02:00
parent 85bfa0f45f
commit bbaa9d09e1
5 changed files with 35 additions and 18 deletions
+1
View File
@@ -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
View File
@@ -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));
}
});
}
}