diff --git a/examples/hello_world.rs b/examples/hello_world.rs index c21cbd5..89445cb 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use osui::prelude::*; +#[derive(Debug, Clone)] pub struct KeyPress; impl Event for KeyPress { @@ -18,7 +19,7 @@ fn main() { let cx = cx.clone(); move || loop { crossterm::event::read().unwrap(); - cx.event(&KeyPress); + cx.emit_event_threaded(&KeyPress); } }); diff --git a/src/component.rs b/src/component.rs index a7e30af..6cf344b 100644 --- a/src/component.rs +++ b/src/component.rs @@ -63,10 +63,26 @@ impl Context { ))); } - pub fn event(self: &Arc, event: &E) { + pub fn emit_event(self: &Arc, event: &E) { if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::()) { for i in v { - (i.lock().unwrap())(&self.clone(), event); + (i.lock().unwrap())(self, event); + } + } + } + + pub fn emit_event_threaded( + self: &Arc, + event: &E, + ) { + if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::()) { + for i in v { + let i = i.clone(); + let event = event.clone(); + let s = self.clone(); + std::thread::spawn(move || { + (i.lock().unwrap())(&s, &event); + }); } } }