diff --git a/pagetop/src/app/application.rs b/pagetop/src/app/application.rs index c2fde5fd..f2b9323a 100644 --- a/pagetop/src/app/application.rs +++ b/pagetop/src/app/application.rs @@ -41,7 +41,7 @@ impl Application { theme::all::register_themes(app.themes()); // Registra las acciones de todos los módulos. - module::all::register_hooks(); + module::all::register_actions(); // Ejecuta actualizaciones pendientes de la base de datos (opcional). #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] diff --git a/pagetop/src/core/component/definition.rs b/pagetop/src/core/component/definition.rs index fc5da576..d43b8f70 100644 --- a/pagetop/src/core/component/definition.rs +++ b/pagetop/src/core/component/definition.rs @@ -84,7 +84,7 @@ macro_rules! hook_before_render_component { weight: isize, } - impl HookTrait for [< BeforeRender $Component >] { + impl HookActionTrait for [< BeforeRender $Component >] { fn new() -> Self { [< BeforeRender $Component >] { action: None, @@ -100,7 +100,7 @@ macro_rules! hook_before_render_component { self.weight } - fn as_ref_any(&self) -> &dyn AnyHook { + fn as_ref_any(&self) -> &dyn AnyHookAction { self } } diff --git a/pagetop/src/core/hook.rs b/pagetop/src/core/hook.rs index 8cc94cb0..579c3bbc 100644 --- a/pagetop/src/core/hook.rs +++ b/pagetop/src/core/hook.rs @@ -1,10 +1,10 @@ mod definition; -pub use definition::{action_ref, AnyHook, HookTrait}; +pub use definition::{action_ref, AnyHookAction, HookActionTrait}; mod holder; pub use holder::HookAction; -use holder::HooksHolder; +use holder::ActionsHolder; mod all; -pub(crate) use all::add_hook; +pub(crate) use all::add_action; pub use all::run_actions; diff --git a/pagetop/src/core/hook/all.rs b/pagetop/src/core/hook/all.rs index d3d00342..05b2f510 100644 --- a/pagetop/src/core/hook/all.rs +++ b/pagetop/src/core/hook/all.rs @@ -1,20 +1,20 @@ -use super::{HookAction, HooksHolder}; +use super::{HookAction, ActionsHolder}; use crate::Lazy; use std::collections::HashMap; use std::sync::RwLock; // Registered actions. -static ACTIONS: Lazy>> = +static ACTIONS: Lazy>> = Lazy::new(|| RwLock::new(HashMap::new())); -pub fn add_hook(hook: HookAction) { - let mut hmap = ACTIONS.write().unwrap(); - let action_handler = hook.handler(); - if let Some(actions) = hmap.get_mut(action_handler) { - actions.add(hook); +pub fn add_action(action: HookAction) { + let mut actions = ACTIONS.write().unwrap(); + let action_handler = action.handler(); + if let Some(holder) = actions.get_mut(action_handler) { + holder.add(action); } else { - hmap.insert(action_handler, HooksHolder::new_with(hook)); + actions.insert(action_handler, ActionsHolder::new_with(action)); } } @@ -22,7 +22,7 @@ pub fn run_actions(action_handler: &str, f: F) where F: FnMut(&HookAction) -> B, { - if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) { - actions.iter_map(f) + if let Some(holder) = ACTIONS.read().unwrap().get(action_handler) { + holder.iter_map(f) } } diff --git a/pagetop/src/core/hook/definition.rs b/pagetop/src/core/hook/definition.rs index 2b89b969..ae739fa6 100644 --- a/pagetop/src/core/hook/definition.rs +++ b/pagetop/src/core/hook/definition.rs @@ -1,6 +1,6 @@ -pub use std::any::Any as AnyHook; +pub use std::any::Any as AnyHookAction; -pub trait HookTrait: AnyHook + Send + Sync { +pub trait HookActionTrait: AnyHookAction + Send + Sync { fn new() -> Self where Self: Sized; @@ -11,9 +11,9 @@ pub trait HookTrait: AnyHook + Send + Sync { 0 } - fn as_ref_any(&self) -> &dyn AnyHook; + fn as_ref_any(&self) -> &dyn AnyHookAction; } -pub fn action_ref(hook: &dyn HookTrait) -> &A { - hook.as_ref_any().downcast_ref::().unwrap() +pub fn action_ref(action: &dyn HookActionTrait) -> &A { + action.as_ref_any().downcast_ref::().unwrap() } diff --git a/pagetop/src/core/hook/holder.rs b/pagetop/src/core/hook/holder.rs index 4a607c1d..31f69711 100644 --- a/pagetop/src/core/hook/holder.rs +++ b/pagetop/src/core/hook/holder.rs @@ -1,8 +1,8 @@ -use super::HookTrait; +use super::HookActionTrait; use std::sync::{Arc, RwLock}; -pub type HookAction = Box; +pub type HookAction = Box; #[macro_export] macro_rules! hook_action { @@ -11,23 +11,23 @@ macro_rules! hook_action { }}; } -pub struct HooksHolder(Arc>>); +pub struct ActionsHolder(Arc>>); -impl HooksHolder { +impl ActionsHolder { pub fn new() -> Self { - HooksHolder(Arc::new(RwLock::new(Vec::new()))) + ActionsHolder(Arc::new(RwLock::new(Vec::new()))) } - pub fn new_with(hook: HookAction) -> Self { - let mut container = HooksHolder::new(); - container.add(hook); - container + pub fn new_with(action: HookAction) -> Self { + let mut holder = ActionsHolder::new(); + holder.add(action); + holder } - pub fn add(&mut self, hook: HookAction) { - let mut actions = self.0.write().unwrap(); - actions.push(hook); - actions.sort_by_key(|a| a.weight()); + pub fn add(&mut self, action: HookAction) { + let mut holder = self.0.write().unwrap(); + holder.push(action); + holder.sort_by_key(|a| a.weight()); } pub fn iter_map(&self, f: F) diff --git a/pagetop/src/core/module/all.rs b/pagetop/src/core/module/all.rs index ae36ac29..87d952b1 100644 --- a/pagetop/src/core/module/all.rs +++ b/pagetop/src/core/module/all.rs @@ -1,5 +1,5 @@ use super::ModuleTrait; -use crate::core::hook::add_hook; +use crate::core::hook::add_action; use crate::{app, trace, Lazy}; #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] @@ -56,10 +56,10 @@ pub fn modules(cfg: &mut app::web::ServiceConfig) { } } -pub fn register_hooks() { +pub fn register_actions() { for m in ENABLED_MODULES.read().unwrap().iter() { for a in m.actions().into_iter() { - add_hook(a); + add_action(a); } } } diff --git a/pagetop/src/response/page/hook.rs b/pagetop/src/response/page/hook.rs index ac78719b..28d6a779 100644 --- a/pagetop/src/response/page/hook.rs +++ b/pagetop/src/response/page/hook.rs @@ -1,5 +1,5 @@ use super::Page; -use crate::core::hook::{AnyHook, HookTrait}; +use crate::core::hook::{AnyHookAction, HookActionTrait}; pub const HOOK_BEFORE_RENDER_PAGE: &str = "pagetop::hook::before_render_page"; @@ -10,7 +10,7 @@ pub struct BeforeRenderPageHook { weight: isize, } -impl HookTrait for BeforeRenderPageHook { +impl HookActionTrait for BeforeRenderPageHook { fn new() -> Self { BeforeRenderPageHook { hook: None, @@ -26,7 +26,7 @@ impl HookTrait for BeforeRenderPageHook { self.weight } - fn as_ref_any(&self) -> &dyn AnyHook { + fn as_ref_any(&self) -> &dyn AnyHookAction { self } }