From 68c551bfb3536ab0ed232edcac828918bcdbef48 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 3 Nov 2023 22:45:12 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Apply=20Han?= =?UTF-8?q?dle=20changes=20to=20improve=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/base/action/component.rs | 136 +----------------- .../component/after_prepare_component.rs | 61 ++++++++ .../component/before_prepare_component.rs | 61 ++++++++ .../base/action/page/after_prepare_body.rs | 10 +- .../base/action/page/before_prepare_body.rs | 10 +- pagetop/src/core/action/all.rs | 6 +- pagetop/src/core/action/definition.rs | 8 +- pagetop/src/core/component/definition.rs | 8 ++ 8 files changed, 150 insertions(+), 150 deletions(-) create mode 100644 pagetop/src/base/action/component/after_prepare_component.rs create mode 100644 pagetop/src/base/action/component/before_prepare_component.rs diff --git a/pagetop/src/base/action/component.rs b/pagetop/src/base/action/component.rs index 5cf72aa6..36987018 100644 --- a/pagetop/src/base/action/component.rs +++ b/pagetop/src/base/action/component.rs @@ -1,133 +1,9 @@ -#[macro_export] -macro_rules! actions_for_component { - ( $Component:ty ) => { - $crate::paste! { - #[allow(unused_imports)] - use $crate::prelude::*; +use crate::prelude::*; - pub type [] = fn(component: &$Component, cx: &mut Context); +pub type FnAction = fn(component: &C, cx: &mut Context); - // ************************************************************************************* - // ACTION BEFORE PREPARE COMPONENT - // ************************************************************************************* +mod before_prepare_component; +pub use before_prepare_component::*; - $crate::new_handle!([] for Crate); - - pub struct [] { - action: Option<[]>, - weight: Weight, - } - - impl ActionTrait for [] { - fn new() -> Self { - [] { - action: None, - weight: 0, - } - } - - fn handle(&self) -> Handle { - [] - } - - fn weight(&self) -> Weight { - self.weight - } - } - - impl [] { - #[allow(dead_code)] - pub fn with(action: []) -> Self { - [] { - action: Some(action), - weight: 0, - } - } - - #[allow(dead_code)] - pub fn with_weight(mut self, value: Weight) -> Self { - self.weight = value; - self - } - - pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) { - if let Some(action) = self.action { - action(component, cx) - } - } - } - - #[inline(always)] - pub(crate) fn []( - component: &mut $Component, - cx: &mut Context - ) { - run_actions([], |action| - action_ref::<[]>(&**action) - .run(component, cx) - ); - } - - // ************************************************************************************* - // ACTION AFTER PREPARE COMPONENT - // ************************************************************************************* - - $crate::new_handle!([] for Crate); - - pub struct [] { - action: Option<[]>, - weight: Weight, - } - - impl ActionTrait for [] { - fn new() -> Self { - [] { - action: None, - weight: 0, - } - } - - fn handle(&self) -> Handle { - [] - } - - fn weight(&self) -> Weight { - self.weight - } - } - - impl [] { - #[allow(dead_code)] - pub fn with(action: []) -> Self { - [] { - action: Some(action), - weight: 0, - } - } - - #[allow(dead_code)] - pub fn with_weight(mut self, value: Weight) -> Self { - self.weight = value; - self - } - - pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) { - if let Some(action) = self.action { - action(component, cx) - } - } - } - - #[inline(always)] - pub(crate) fn []( - component: &mut $Component, - cx: &mut Context - ) { - run_actions([], |action| - action_ref::<[]>(&**action) - .run(component, cx) - ); - } - } - }; -} +mod after_prepare_component; +pub use after_prepare_component::*; diff --git a/pagetop/src/base/action/component/after_prepare_component.rs b/pagetop/src/base/action/component/after_prepare_component.rs new file mode 100644 index 00000000..43791326 --- /dev/null +++ b/pagetop/src/base/action/component/after_prepare_component.rs @@ -0,0 +1,61 @@ +use crate::prelude::*; + +use super::FnAction; + +pub struct AfterPrepareComponent { + action: Option>, + referer: Option, + weight: Weight, +} + +impl_handle!(ACTION_AFTER_PREPARE_COMPONENT for AfterPrepareComponent); + +impl ActionTrait for AfterPrepareComponent { + fn new() -> Self { + AfterPrepareComponent { + action: None, + referer: Some(C::static_handle()), + weight: 0, + } + } + + fn referer_handle(&self) -> Option { + self.referer + } + + fn weight(&self) -> Weight { + self.weight + } +} + +impl AfterPrepareComponent { + pub fn with(action: FnAction) -> Self { + AfterPrepareComponent { + action: Some(action), + referer: Some(C::static_handle()), + weight: 0, + } + } + + pub fn with_weight(mut self, value: Weight) -> Self { + self.weight = value; + self + } + + pub(crate) fn run(&self, component: &mut C, cx: &mut Context) { + if let Some(action) = self.action { + action(component, cx) + } + } +} + +#[inline(always)] +pub(crate) fn run_actions_after_prepare_component( + component: &mut C, + cx: &mut Context, +) { + run_actions( + (ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle())), + |action| action_ref::>(&**action).run(component, cx), + ); +} diff --git a/pagetop/src/base/action/component/before_prepare_component.rs b/pagetop/src/base/action/component/before_prepare_component.rs new file mode 100644 index 00000000..2eb26272 --- /dev/null +++ b/pagetop/src/base/action/component/before_prepare_component.rs @@ -0,0 +1,61 @@ +use crate::prelude::*; + +use super::FnAction; + +pub struct BeforePrepareComponent { + action: Option>, + referer: Option, + weight: Weight, +} + +impl_handle!(ACTION_BEFORE_PREPARE_COMPONENT for BeforePrepareComponent); + +impl ActionTrait for BeforePrepareComponent { + fn new() -> Self { + BeforePrepareComponent { + action: None, + referer: Some(C::static_handle()), + weight: 0, + } + } + + fn referer_handle(&self) -> Option { + self.referer + } + + fn weight(&self) -> Weight { + self.weight + } +} + +impl BeforePrepareComponent { + pub fn with(action: FnAction) -> Self { + BeforePrepareComponent { + action: Some(action), + referer: Some(C::static_handle()), + weight: 0, + } + } + + pub fn with_weight(mut self, value: Weight) -> Self { + self.weight = value; + self + } + + pub(crate) fn run(&self, component: &mut C, cx: &mut Context) { + if let Some(action) = self.action { + action(component, cx) + } + } +} + +#[inline(always)] +pub(crate) fn run_actions_before_prepare_component( + component: &mut C, + cx: &mut Context, +) { + run_actions( + (ACTION_BEFORE_PREPARE_COMPONENT, Some(component.handle())), + |action| action_ref::>(&**action).run(component, cx), + ); +} diff --git a/pagetop/src/base/action/page/after_prepare_body.rs b/pagetop/src/base/action/page/after_prepare_body.rs index 0e2ced8d..d6295ef4 100644 --- a/pagetop/src/base/action/page/after_prepare_body.rs +++ b/pagetop/src/base/action/page/after_prepare_body.rs @@ -2,13 +2,13 @@ use crate::prelude::*; use super::FnActionPage; -new_handle!(ACTION_AFTER_PREPARE_BODY for Crate); - pub struct AfterPrepareBody { action: Option, weight: Weight, } +impl_handle!(ACTION_AFTER_PREPARE_BODY for AfterPrepareBody); + impl ActionTrait for AfterPrepareBody { fn new() -> Self { AfterPrepareBody { @@ -17,10 +17,6 @@ impl ActionTrait for AfterPrepareBody { } } - fn handle(&self) -> Handle { - ACTION_AFTER_PREPARE_BODY - } - fn weight(&self) -> Weight { self.weight } @@ -48,7 +44,7 @@ impl AfterPrepareBody { #[inline(always)] pub(crate) fn run_actions_after_prepare_body(page: &mut Page) { - run_actions(ACTION_AFTER_PREPARE_BODY, |action| { + run_actions((ACTION_AFTER_PREPARE_BODY, None), |action| { action_ref::(&**action).run(page) }); } diff --git a/pagetop/src/base/action/page/before_prepare_body.rs b/pagetop/src/base/action/page/before_prepare_body.rs index 3198e6f3..6afcd88f 100644 --- a/pagetop/src/base/action/page/before_prepare_body.rs +++ b/pagetop/src/base/action/page/before_prepare_body.rs @@ -2,13 +2,13 @@ use crate::prelude::*; use super::FnActionPage; -new_handle!(ACTION_BEFORE_PREPARE_BODY for Crate); - pub struct BeforePrepareBody { action: Option, weight: Weight, } +impl_handle!(ACTION_BEFORE_PREPARE_BODY for BeforePrepareBody); + impl ActionTrait for BeforePrepareBody { fn new() -> Self { BeforePrepareBody { @@ -17,10 +17,6 @@ impl ActionTrait for BeforePrepareBody { } } - fn handle(&self) -> Handle { - ACTION_BEFORE_PREPARE_BODY - } - fn weight(&self) -> Weight { self.weight } @@ -48,7 +44,7 @@ impl BeforePrepareBody { #[inline(always)] pub(crate) fn run_actions_before_prepare_body(page: &mut Page) { - run_actions(ACTION_BEFORE_PREPARE_BODY, |action| { + run_actions((ACTION_BEFORE_PREPARE_BODY, None), |action| { action_ref::(&**action).run(page) }); } diff --git a/pagetop/src/core/action/all.rs b/pagetop/src/core/action/all.rs index 4be2afe0..c42e1bee 100644 --- a/pagetop/src/core/action/all.rs +++ b/pagetop/src/core/action/all.rs @@ -5,12 +5,12 @@ use std::collections::HashMap; use std::sync::RwLock; // Registered actions. -static ACTIONS: LazyStatic>> = +static ACTIONS: LazyStatic), ActionsList>>> = LazyStatic::new(|| RwLock::new(HashMap::new())); pub fn add_action(action: Action) { let mut actions = ACTIONS.write().unwrap(); - let action_handle = action.handle(); + let action_handle = (action.handle(), action.referer_handle()); if let Some(list) = actions.get_mut(&action_handle) { list.add(action); } else { @@ -18,7 +18,7 @@ pub fn add_action(action: Action) { } } -pub fn run_actions(action_handle: Handle, f: F) +pub fn run_actions(action_handle: (Handle, Option), f: F) where F: FnMut(&Action) -> B, { diff --git a/pagetop/src/core/action/definition.rs b/pagetop/src/core/action/definition.rs index ad46f11b..a1b9fcc0 100644 --- a/pagetop/src/core/action/definition.rs +++ b/pagetop/src/core/action/definition.rs @@ -1,4 +1,4 @@ -use crate::{Handle, Weight}; +use crate::{Handle, HasHandle, Weight}; use std::any::Any; @@ -6,12 +6,14 @@ pub trait ActionBase: Any { fn as_ref_any(&self) -> &dyn Any; } -pub trait ActionTrait: ActionBase + Send + Sync { +pub trait ActionTrait: ActionBase + HasHandle + Send + Sync { fn new() -> Self where Self: Sized; - fn handle(&self) -> Handle; + fn referer_handle(&self) -> Option { + None + } fn weight(&self) -> Weight { 0 diff --git a/pagetop/src/core/component/definition.rs b/pagetop/src/core/component/definition.rs index 60b7ab46..411af1fb 100644 --- a/pagetop/src/core/component/definition.rs +++ b/pagetop/src/core/component/definition.rs @@ -1,3 +1,5 @@ +use crate::base::action::component::run_actions_after_prepare_component; +use crate::base::action::component::run_actions_before_prepare_component; use crate::core::component::Context; use crate::html::{html, Markup, PrepareMarkup}; use crate::{util, HasHandle, Weight}; @@ -59,6 +61,9 @@ impl ComponentBase for C { // Acciones del tema antes de preparar el componente. cx.theme().before_prepare_component(self, cx); + // Acciones de los módulos antes de preparar el componente. + run_actions_before_prepare_component(self, cx); + let markup = match cx.theme().render_component(self, cx) { Some(html) => html, None => match self.prepare_component(cx) { @@ -74,6 +79,9 @@ impl ComponentBase for C { // Acciones del tema después de preparar el componente. cx.theme().after_prepare_component(self, cx); + // Acciones de los módulos después de preparar el componente. + run_actions_after_prepare_component(self, cx); + markup } else { html! {}