From a6d39202d49b18a10f2c67672adc12da095d02cd Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 7 May 2022 15:15:14 +0200 Subject: [PATCH] =?UTF-8?q?Modifica=20y=20simplifica=20identificaci=C3=B3n?= =?UTF-8?q?=20de=20acciones?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/api/action/all.rs | 13 +++++++------ pagetop/src/api/action/definition.rs | 4 ---- pagetop/src/api/component/action.rs | 6 ------ pagetop/src/api/component/definition.rs | 7 +++---- pagetop/src/api/component/mod.rs | 5 +---- pagetop/src/api/mod.rs | 2 ++ pagetop/src/prelude.rs | 1 + pagetop/src/response/page/action.rs | 6 ------ pagetop/src/response/page/mod.rs | 5 +---- pagetop/src/response/page/page.rs | 6 +++--- 10 files changed, 18 insertions(+), 37 deletions(-) diff --git a/pagetop/src/api/action/all.rs b/pagetop/src/api/action/all.rs index 8003d09e..4e301777 100644 --- a/pagetop/src/api/action/all.rs +++ b/pagetop/src/api/action/all.rs @@ -1,29 +1,30 @@ use crate::Lazy; +use crate::api::TypeId; use super::{ActionItem, ActionsHolder}; use std::sync::RwLock; use std::collections::HashMap; // Registered actions. -static ACTIONS: Lazy>> = Lazy::new(|| { +static ACTIONS: Lazy>> = Lazy::new(|| { RwLock::new(HashMap::new()) }); pub fn add_action(action: ActionItem) { let mut hmap = ACTIONS.write().unwrap(); - let action_name = action.machine_name(); - if let Some(actions) = hmap.get_mut(action_name) { + let action_id = action.type_id(); + if let Some(actions) = hmap.get_mut(&action_id) { actions.add(action); } else { - hmap.insert(action_name, ActionsHolder::new_with(action)); + hmap.insert(action_id, ActionsHolder::new_with(action)); } } -pub fn run_actions(machine_name: &'static str, f: F) +pub fn run_actions(action_id: TypeId, f: F) where F: FnMut(&ActionItem) -> B { - if let Some(actions) = ACTIONS.read().unwrap().get(machine_name) { + if let Some(actions) = ACTIONS.read().unwrap().get(&action_id) { actions.iter_map(f) } } diff --git a/pagetop/src/api/action/definition.rs b/pagetop/src/api/action/definition.rs index 1bd554df..9acb1258 100644 --- a/pagetop/src/api/action/definition.rs +++ b/pagetop/src/api/action/definition.rs @@ -3,10 +3,6 @@ pub use std::any::Any as AnyAction; pub trait ActionTrait: AnyAction + Send + Sync { fn new() -> Self where Self: Sized; - fn machine_name(&self) -> &'static str { - std::any::type_name::() - } - fn weight(&self) -> isize { 0 } diff --git a/pagetop/src/api/component/action.rs b/pagetop/src/api/component/action.rs index 9e372570..d4e1f460 100644 --- a/pagetop/src/api/component/action.rs +++ b/pagetop/src/api/component/action.rs @@ -1,8 +1,6 @@ use crate::api::action::{ActionTrait, AnyAction}; use super::{Assets, ComponentTrait}; -pub const ACTION_BEFORE_RENDER_COMPONENT: &str = "pagetop::render::before_render_component"; - pub struct ActionBeforeRenderComponent { action: Option, weight: isize, @@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderComponent { } } - fn machine_name(&self) -> &'static str { - ACTION_BEFORE_RENDER_COMPONENT - } - fn weight(&self) -> isize { self.weight } diff --git a/pagetop/src/api/component/definition.rs b/pagetop/src/api/component/definition.rs index 61893243..687b4eae 100644 --- a/pagetop/src/api/component/definition.rs +++ b/pagetop/src/api/component/definition.rs @@ -1,8 +1,7 @@ use crate::html::{Markup, html}; -use crate::api::action::{action_ref, run_actions}; +use crate::api::{TypeId, action::{action_ref, run_actions}}; use crate::util; -use super::{ACTION_BEFORE_RENDER_COMPONENT, ActionBeforeRenderComponent}; -use super::Assets; +use super::{ActionBeforeRenderComponent, Assets}; pub use std::any::Any as AnyComponent; @@ -76,7 +75,7 @@ pub fn render_component(component: &mut dyn ComponentTrait, assets: &mut Assets) // Acciones de los módulos antes de renderizar el componente. run_actions( - ACTION_BEFORE_RENDER_COMPONENT, + TypeId::of::(), |a| action_ref::(&**a).run(component, assets) ); diff --git a/pagetop/src/api/component/mod.rs b/pagetop/src/api/component/mod.rs index a119089f..10a894b9 100644 --- a/pagetop/src/api/component/mod.rs +++ b/pagetop/src/api/component/mod.rs @@ -1,8 +1,5 @@ mod action; -pub use action::{ - ACTION_BEFORE_RENDER_COMPONENT, - ActionBeforeRenderComponent, -}; +pub use action::ActionBeforeRenderComponent; mod assets; pub use assets::{ diff --git a/pagetop/src/api/mod.rs b/pagetop/src/api/mod.rs index b69ec91b..8d84af05 100644 --- a/pagetop/src/api/mod.rs +++ b/pagetop/src/api/mod.rs @@ -1,3 +1,5 @@ +pub use std::any::TypeId; + pub mod action; // API to define functions that alter the behavior of PageTop core. pub mod component; // API para crear nuevos componentes. pub mod module; // API para añadir módulos con nuevas funcionalidades. diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index 357c31cf..a20d1563 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -21,6 +21,7 @@ pub use crate::{ }; pub use crate::{action_item, api::{ + TypeId, action::*, component::*, module::*, diff --git a/pagetop/src/response/page/action.rs b/pagetop/src/response/page/action.rs index d47b5ca6..7fe35a7f 100644 --- a/pagetop/src/response/page/action.rs +++ b/pagetop/src/response/page/action.rs @@ -1,8 +1,6 @@ use crate::api::action::{ActionTrait, AnyAction}; use super::Page; -pub const ACTION_BEFORE_RENDER_PAGE: &str = "pagetop::render::before_render_page"; - pub struct ActionBeforeRenderPage { action: Option, weight: isize, @@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderPage { } } - fn machine_name(&self) -> &'static str { - ACTION_BEFORE_RENDER_PAGE - } - fn weight(&self) -> isize { self.weight } diff --git a/pagetop/src/response/page/mod.rs b/pagetop/src/response/page/mod.rs index 3d8194f2..30f95d72 100644 --- a/pagetop/src/response/page/mod.rs +++ b/pagetop/src/response/page/mod.rs @@ -1,8 +1,5 @@ mod action; -pub use action::{ - ACTION_BEFORE_RENDER_PAGE, - ActionBeforeRenderPage, -}; +pub use action::ActionBeforeRenderPage; mod page; pub use page::Page; diff --git a/pagetop/src/response/page/page.rs b/pagetop/src/response/page/page.rs index 0fc9c627..99a81cb3 100644 --- a/pagetop/src/response/page/page.rs +++ b/pagetop/src/response/page/page.rs @@ -1,9 +1,9 @@ use crate::{Lazy, app, trace}; use crate::config::SETTINGS; use crate::html::*; -use crate::api::action::{action_ref, run_actions}; +use crate::api::{TypeId, action::{action_ref, run_actions}}; use crate::api::component::*; -use super::{ACTION_BEFORE_RENDER_PAGE, ActionBeforeRenderPage}; +use super::ActionBeforeRenderPage; use std::collections::HashMap; @@ -152,7 +152,7 @@ impl<'a> Page<'a> { pub fn render(&mut self) -> app::Result { // Acciones de los módulos antes de renderizar la página. run_actions( - ACTION_BEFORE_RENDER_PAGE, + TypeId::of::(), |a| action_ref::(&**a).run(self) );