Skip to content

Commit

Permalink
Merge pull request #115 from paxproject/wj/fix-component-handlers
Browse files Browse the repository at this point in the history
Fix Component level handlers
  • Loading branch information
samuelselleck authored Feb 19, 2024
2 parents 923d655 + e57efbd commit cc11d41
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
6 changes: 4 additions & 2 deletions pax-compiler/templates/cartridge_generation/macros.tera
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ impl ComponentFactory for {{component.pascal_identifier}}Factory {
fn build_component_handlers(&self, handlers: Vec<(String, Vec<String>)>) -> Rc<RefCell<HandlerRegistry>> {
let mut handler_registry = HandlerRegistry::default();
for (event, functions) in &handlers {
handler_registry.handlers.insert(event.clone(), functions.iter().map(|fn_name| self.build_handler(&fn_name)).collect());
handler_registry.handlers.insert(event.clone(), functions.iter().map(|fn_name| {
Handler::new_component_handler(self.build_handler(&fn_name))
}).collect());
}
Rc::new(RefCell::new(handler_registry))
}
Expand All @@ -78,7 +80,7 @@ impl ComponentFactory for {{component.pascal_identifier}}Factory {
let mut handler_registry_mut = handler_registry.borrow_mut();
for (event, fn_name) in &handlers {
let handler_vec = handler_registry_mut.handlers.entry(event.clone()).or_insert(Vec::new());
handler_vec.push(self.build_handler(&fn_name));
handler_vec.push(Handler::new_inline_handler(self.build_handler(&fn_name)));
}
}
handler_registry
Expand Down
19 changes: 12 additions & 7 deletions pax-runtime/src/engine/expanded_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::api::{
};

use crate::{
compute_tab, ComponentInstance, InstanceNode, InstanceNodePtr, PropertiesComputable,
RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds,
compute_tab, ComponentInstance, HandlerLocation, InstanceNode, InstanceNodePtr,
PropertiesComputable, RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds,
};

pub struct ExpandedNode {
Expand Down Expand Up @@ -123,8 +123,13 @@ macro_rules! dispatch_event_handler {
let borrowed_registry = &(*registry).borrow();
if let Some(handlers) = borrowed_registry.handlers.get($handler_key) {
handlers.iter().for_each(|handler| {
handler(
Rc::clone(&component_properties),
let properties = if let HandlerLocation::Component = &handler.location {
Rc::clone(&self.properties)
} else {
Rc::clone(&component_properties)
};
(handler.function)(
Rc::clone(&properties),
&context,
Some(Box::new(args.clone()) as Box<dyn Any>),
);
Expand Down Expand Up @@ -275,7 +280,7 @@ impl ExpandedNode {
.get("tick")
.unwrap_or(&Vec::new())
{
handler(
(handler.function)(
Rc::clone(&self.properties),
&self.get_node_context(context),
None,
Expand All @@ -293,7 +298,7 @@ impl ExpandedNode {
.get("pre_render")
.unwrap_or(&Vec::new())
{
handler(
(handler.function)(
Rc::clone(&self.properties),
&self.get_node_context(context),
None,
Expand All @@ -319,7 +324,7 @@ impl ExpandedNode {
.get("mount")
.unwrap_or(&Vec::new())
{
handler(
(handler.function)(
Rc::clone(&self.properties),
&self.get_node_context(context),
None,
Expand Down
33 changes: 31 additions & 2 deletions pax-runtime/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,38 @@ impl PropertiesComputable for CommonProperties {
}
}

pub enum HandlerLocation {
Inline,
Component,
}

pub struct Handler {
pub function: fn(Rc<RefCell<dyn Any>>, &NodeContext, Option<Box<dyn Any>>),
pub location: HandlerLocation,
}

impl Handler {
pub fn new_inline_handler(
function: fn(Rc<RefCell<dyn Any>>, &NodeContext, Option<Box<dyn Any>>),
) -> Self {
Handler {
function,
location: HandlerLocation::Inline,
}
}

pub fn new_component_handler(
function: fn(Rc<RefCell<dyn Any>>, &NodeContext, Option<Box<dyn Any>>),
) -> Self {
Handler {
function,
location: HandlerLocation::Component,
}
}
}

pub struct HandlerRegistry {
pub handlers:
HashMap<String, Vec<fn(Rc<RefCell<dyn Any>>, &NodeContext, Option<Box<dyn Any>>)>>,
pub handlers: HashMap<String, Vec<Handler>>,
}

impl Default for HandlerRegistry {
Expand Down

0 comments on commit cc11d41

Please sign in to comment.