From c108235613363fe16be36936e9da965a98047711 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 4 Nov 2023 12:57:14 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20the=20code=20for=20act?= =?UTF-8?q?ions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-admin/src/lib.rs | 2 +- .../component/after_prepare_component.rs | 16 ++++++--------- .../component/before_prepare_component.rs | 20 ++++++------------- .../base/action/page/after_prepare_body.rs | 13 ++++-------- .../base/action/page/before_prepare_body.rs | 13 ++++-------- pagetop/src/core/action.rs | 2 +- pagetop/src/core/action/all.rs | 14 ++++++------- pagetop/src/prelude.rs | 2 +- 8 files changed, 30 insertions(+), 52 deletions(-) diff --git a/pagetop-admin/src/lib.rs b/pagetop-admin/src/lib.rs index 98b5a4b3..bf7f5ca0 100644 --- a/pagetop-admin/src/lib.rs +++ b/pagetop-admin/src/lib.rs @@ -21,7 +21,7 @@ impl ModuleTrait for Admin { actions![ action::page::BeforePrepareBody::with(before_prepare_body), action::component::BeforePrepareComponent::::with(before_prepare_menu) - .filtering_id("admin-menu-test"), + .filter_by_referer_id("admin-menu-test"), ] } diff --git a/pagetop/src/base/action/component/after_prepare_component.rs b/pagetop/src/base/action/component/after_prepare_component.rs index 6396480e..55087bd8 100644 --- a/pagetop/src/base/action/component/after_prepare_component.rs +++ b/pagetop/src/base/action/component/after_prepare_component.rs @@ -3,7 +3,7 @@ use crate::prelude::*; use super::FnAction; pub struct AfterPrepareComponent { - f: Option>, + f: FnAction, referer_handle: Option, referer_id: OptionId, weight: Weight, @@ -28,14 +28,14 @@ impl ActionTrait for AfterPrepareComponent { impl AfterPrepareComponent { pub fn with(f: FnAction) -> Self { AfterPrepareComponent { - f: Some(f), + f, referer_handle: Some(C::static_handle()), referer_id: OptionId::default(), weight: 0, } } - pub fn filtering_id(mut self, id: impl Into) -> Self { + pub fn filter_by_referer_id(mut self, id: impl Into) -> Self { self.referer_id.alter_value(id); self } @@ -46,14 +46,10 @@ impl AfterPrepareComponent { } #[inline(always)] - pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option) { + pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option) { dispatch_actions( - (ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle()), id), - |action| { - if let Some(f) = action_ref::>(&**action).f { - f(component, cx) - } - }, + (Self::static_handle(), Some(component.handle()), referer_id), + |action| (action_ref::>(&**action).f)(component, cx), ); } } diff --git a/pagetop/src/base/action/component/before_prepare_component.rs b/pagetop/src/base/action/component/before_prepare_component.rs index 331fb87d..b4a6106c 100644 --- a/pagetop/src/base/action/component/before_prepare_component.rs +++ b/pagetop/src/base/action/component/before_prepare_component.rs @@ -3,7 +3,7 @@ use crate::prelude::*; use super::FnAction; pub struct BeforePrepareComponent { - f: Option>, + f: FnAction, referer_handle: Option, referer_id: OptionId, weight: Weight, @@ -28,14 +28,14 @@ impl ActionTrait for BeforePrepareComponent { impl BeforePrepareComponent { pub fn with(f: FnAction) -> Self { BeforePrepareComponent { - f: Some(f), + f, referer_handle: Some(C::static_handle()), referer_id: OptionId::default(), weight: 0, } } - pub fn filtering_id(mut self, id: impl Into) -> Self { + pub fn filter_by_referer_id(mut self, id: impl Into) -> Self { self.referer_id.alter_value(id); self } @@ -46,18 +46,10 @@ impl BeforePrepareComponent { } #[inline(always)] - pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option) { + pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option) { dispatch_actions( - ( - ACTION_BEFORE_PREPARE_COMPONENT, - Some(component.handle()), - id, - ), - |action| { - if let Some(f) = action_ref::>(&**action).f { - f(component, cx) - } - }, + (Self::static_handle(), Some(component.handle()), referer_id), + |action| (action_ref::>(&**action).f)(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 8f270d19..342d5fc2 100644 --- a/pagetop/src/base/action/page/after_prepare_body.rs +++ b/pagetop/src/base/action/page/after_prepare_body.rs @@ -3,7 +3,7 @@ use crate::prelude::*; use super::FnActionPage; pub struct AfterPrepareBody { - f: Option, + f: FnActionPage, weight: Weight, } @@ -17,10 +17,7 @@ impl ActionTrait for AfterPrepareBody { impl AfterPrepareBody { pub fn with(f: FnActionPage) -> Self { - AfterPrepareBody { - f: Some(f), - weight: 0, - } + AfterPrepareBody { f, weight: 0 } } pub fn with_weight(mut self, value: Weight) -> Self { @@ -30,10 +27,8 @@ impl AfterPrepareBody { #[inline(always)] pub(crate) fn dispatch(page: &mut Page) { - dispatch_actions((ACTION_AFTER_PREPARE_BODY, None, None), |action| { - if let Some(f) = action_ref::(&**action).f { - f(page) - } + dispatch_actions((Self::static_handle(), None, None), |action| { + (action_ref::(&**action).f)(page) }); } } diff --git a/pagetop/src/base/action/page/before_prepare_body.rs b/pagetop/src/base/action/page/before_prepare_body.rs index 6c19be9b..9dd69539 100644 --- a/pagetop/src/base/action/page/before_prepare_body.rs +++ b/pagetop/src/base/action/page/before_prepare_body.rs @@ -3,7 +3,7 @@ use crate::prelude::*; use super::FnActionPage; pub struct BeforePrepareBody { - f: Option, + f: FnActionPage, weight: Weight, } @@ -17,10 +17,7 @@ impl ActionTrait for BeforePrepareBody { impl BeforePrepareBody { pub fn with(f: FnActionPage) -> Self { - BeforePrepareBody { - f: Some(f), - weight: 0, - } + BeforePrepareBody { f, weight: 0 } } pub fn with_weight(mut self, value: Weight) -> Self { @@ -30,10 +27,8 @@ impl BeforePrepareBody { #[inline(always)] pub(crate) fn dispatch(page: &mut Page) { - dispatch_actions((ACTION_BEFORE_PREPARE_BODY, None, None), |action| { - if let Some(f) = action_ref::(&**action).f { - f(page) - } + dispatch_actions((Self::static_handle(), None, None), |action| { + (action_ref::(&**action).f)(page) }); } } diff --git a/pagetop/src/core/action.rs b/pagetop/src/core/action.rs index e4918a8e..1add337b 100644 --- a/pagetop/src/core/action.rs +++ b/pagetop/src/core/action.rs @@ -7,4 +7,4 @@ use list::ActionsList; mod all; pub(crate) use all::add_action; -pub use all::dispatch_actions; +pub use all::{dispatch_actions, KeyAction}; diff --git a/pagetop/src/core/action/all.rs b/pagetop/src/core/action/all.rs index d6b70cd9..45a466c6 100644 --- a/pagetop/src/core/action/all.rs +++ b/pagetop/src/core/action/all.rs @@ -4,31 +4,31 @@ use crate::{Handle, LazyStatic}; use std::collections::HashMap; use std::sync::RwLock; -type KeyHandles = (Handle, Option, Option); +pub type KeyAction = (Handle, Option, Option); // Registered actions. -static ACTIONS: LazyStatic>> = +static ACTIONS: LazyStatic>> = LazyStatic::new(|| RwLock::new(HashMap::new())); pub fn add_action(action: Action) { let mut actions = ACTIONS.write().unwrap(); - let action_handle = ( + let key_action = ( action.handle(), action.referer_handle(), action.referer_id(), ); - if let Some(list) = actions.get_mut(&action_handle) { + if let Some(list) = actions.get_mut(&key_action) { list.add(action); } else { - actions.insert(action_handle, ActionsList::with(action)); + actions.insert(key_action, ActionsList::with(action)); } } -pub fn dispatch_actions(action_handle: (Handle, Option, Option), f: F) +pub fn dispatch_actions(key_action: KeyAction, f: F) where F: FnMut(&Action) -> B, { - if let Some(list) = ACTIONS.read().unwrap().get(&action_handle) { + if let Some(list) = ACTIONS.read().unwrap().get(&key_action) { list.iter_map(f) } } diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index af7a5d43..1ac38762 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -4,7 +4,7 @@ pub use crate::{concat_string, fn_builder, main, paste, test}; // Global. -pub use crate::{Handle, HashMapResources, LazyStatic, Weight}; +pub use crate::{Handle, HasHandle, HashMapResources, LazyStatic, Weight}; // Functions and macro helpers. pub use crate::util;