Working event handling
This commit is contained in:
+48
-3
@@ -1,19 +1,38 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
collections::HashMap,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::Node;
|
||||
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node>>;
|
||||
pub trait Event {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
}
|
||||
|
||||
impl<'a> dyn Event + 'a {
|
||||
pub fn get<T: Event + 'static>(&self) -> Option<&T> {
|
||||
self.as_any().downcast_ref()
|
||||
}
|
||||
}
|
||||
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node> + Send + Sync>;
|
||||
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Event) + Send + Sync>>;
|
||||
|
||||
pub struct Context {
|
||||
component: Mutex<Component>,
|
||||
nodes: Mutex<Vec<Node>>,
|
||||
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + 'static>(component: F) -> Arc<Self> {
|
||||
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + Send + Sync + 'static>(
|
||||
component: F,
|
||||
) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
component: Mutex::new(Arc::new(component)),
|
||||
nodes: Mutex::new(Vec::new()),
|
||||
event_handlers: Mutex::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -25,4 +44,30 @@ impl Context {
|
||||
pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> {
|
||||
self.nodes.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Context>, &T) + Send + Sync + 'static>(
|
||||
self: &Arc<Context>,
|
||||
handler: F,
|
||||
) {
|
||||
self.event_handlers
|
||||
.lock()
|
||||
.unwrap()
|
||||
.entry(TypeId::of::<T>())
|
||||
.or_insert_with(|| Vec::new())
|
||||
.push(Arc::new(Mutex::new(
|
||||
move |ctx: &Arc<Context>, event: &dyn Event| {
|
||||
if let Some(e) = event.get::<T>() {
|
||||
(handler)(ctx, e);
|
||||
}
|
||||
},
|
||||
)));
|
||||
}
|
||||
|
||||
pub fn event<E: Event + 'static>(self: &Arc<Context>, event: &E) {
|
||||
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
|
||||
for i in v {
|
||||
(i.lock().unwrap())(&self.clone(), event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ pub mod prelude {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Node {
|
||||
String(Arc<dyn Fn() -> String>),
|
||||
String(Arc<dyn Fn() -> String + Send + Sync>),
|
||||
Component(component::Component),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user