From 8f3d49658199c5e4554fa1be17784d262591edfe Mon Sep 17 00:00:00 2001 From: Warfa Jibril Date: Mon, 19 Feb 2024 14:48:58 -0800 Subject: [PATCH 1/3] fixed component handlers --- .../cartridge_generation/macros.tera | 6 ++-- pax-runtime/src/engine/expanded_node.rs | 16 ++++++--- pax-runtime/src/engine/mod.rs | 33 +++++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/pax-compiler/templates/cartridge_generation/macros.tera b/pax-compiler/templates/cartridge_generation/macros.tera index e3aabbe93..67d3780c4 100644 --- a/pax-compiler/templates/cartridge_generation/macros.tera +++ b/pax-compiler/templates/cartridge_generation/macros.tera @@ -68,7 +68,9 @@ impl ComponentFactory for {{component.pascal_identifier}}Factory { fn build_component_handlers(&self, handlers: Vec<(String, Vec)>) -> Rc> { 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)) } @@ -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 diff --git a/pax-runtime/src/engine/expanded_node.rs b/pax-runtime/src/engine/expanded_node.rs index 0808fc31b..20a6f18a1 100644 --- a/pax-runtime/src/engine/expanded_node.rs +++ b/pax-runtime/src/engine/expanded_node.rs @@ -123,8 +123,14 @@ 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 crate::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), ); @@ -275,7 +281,7 @@ impl ExpandedNode { .get("tick") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, @@ -293,7 +299,7 @@ impl ExpandedNode { .get("pre_render") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, @@ -319,7 +325,7 @@ impl ExpandedNode { .get("mount") .unwrap_or(&Vec::new()) { - handler( + (handler.function)( Rc::clone(&self.properties), &self.get_node_context(context), None, diff --git a/pax-runtime/src/engine/mod.rs b/pax-runtime/src/engine/mod.rs index e8a2674d9..c67f0ef2e 100644 --- a/pax-runtime/src/engine/mod.rs +++ b/pax-runtime/src/engine/mod.rs @@ -80,9 +80,38 @@ impl PropertiesComputable for CommonProperties { } } +pub enum HandlerLocation { + Inline, + Component, +} + +pub struct Handler { + pub function: fn(Rc>, &NodeContext, Option>), + pub location: HandlerLocation, +} + +impl Handler { + pub fn new_inline_handler( + function: fn(Rc>, &NodeContext, Option>), + ) -> Self { + Handler { + function, + location: HandlerLocation::Inline, + } + } + + pub fn new_component_handler( + function: fn(Rc>, &NodeContext, Option>), + ) -> Self { + Handler { + function, + location: HandlerLocation::Component, + } + } +} + pub struct HandlerRegistry { - pub handlers: - HashMap>, &NodeContext, Option>)>>, + pub handlers: HashMap>, } impl Default for HandlerRegistry { From ec5f79b5f58643032922d9b1b85de7ba351c1364 Mon Sep 17 00:00:00 2001 From: Warfa Jibril Date: Mon, 19 Feb 2024 14:52:58 -0800 Subject: [PATCH 2/3] cleaning --- pax-runtime/src/engine/expanded_node.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pax-runtime/src/engine/expanded_node.rs b/pax-runtime/src/engine/expanded_node.rs index 20a6f18a1..ca18fa520 100644 --- a/pax-runtime/src/engine/expanded_node.rs +++ b/pax-runtime/src/engine/expanded_node.rs @@ -24,7 +24,7 @@ use crate::api::{ use crate::{ compute_tab, ComponentInstance, InstanceNode, InstanceNodePtr, PropertiesComputable, - RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, + RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, HandlerLocation }; pub struct ExpandedNode { @@ -124,7 +124,7 @@ macro_rules! dispatch_event_handler { if let Some(handlers) = borrowed_registry.handlers.get($handler_key) { handlers.iter().for_each(|handler| { let properties = - if let crate::HandlerLocation::Component = &handler.location { + if let HandlerLocation::Component = &handler.location { Rc::clone(&self.properties) } else { Rc::clone(&component_properties) From e57efbdacbc6b6e877494223c469b5a815928997 Mon Sep 17 00:00:00 2001 From: Warfa Jibril Date: Mon, 19 Feb 2024 14:53:09 -0800 Subject: [PATCH 3/3] cleaning --- pax-runtime/src/engine/expanded_node.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/pax-runtime/src/engine/expanded_node.rs b/pax-runtime/src/engine/expanded_node.rs index ca18fa520..c2a98fd75 100644 --- a/pax-runtime/src/engine/expanded_node.rs +++ b/pax-runtime/src/engine/expanded_node.rs @@ -23,8 +23,8 @@ use crate::api::{ }; use crate::{ - compute_tab, ComponentInstance, InstanceNode, InstanceNodePtr, PropertiesComputable, - RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, HandlerLocation + compute_tab, ComponentInstance, HandlerLocation, InstanceNode, InstanceNodePtr, + PropertiesComputable, RuntimeContext, RuntimePropertiesStackFrame, TransformAndBounds, }; pub struct ExpandedNode { @@ -123,12 +123,11 @@ 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| { - let properties = - if let HandlerLocation::Component = &handler.location { - Rc::clone(&self.properties) - } else { - 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,