From 166f209dab5f72f6e2a6e0607188580c3a6a0c27 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 16 Jul 2022 06:45:32 +0200 Subject: [PATCH] Actualiza HookItem por HookAction --- pagetop-admin/src/lib.rs | 4 ++-- pagetop-node/src/lib.rs | 4 ++-- pagetop/src/core/component/definition.rs | 4 ++++ pagetop/src/core/hook.rs | 2 +- pagetop/src/core/hook/all.rs | 6 +++--- pagetop/src/core/hook/holder.rs | 12 ++++++------ pagetop/src/core/module/definition.rs | 4 ++-- pagetop/src/prelude.rs | 2 +- 8 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pagetop-admin/src/lib.rs b/pagetop-admin/src/lib.rs index c12421c3..5bd6dfb6 100644 --- a/pagetop-admin/src/lib.rs +++ b/pagetop-admin/src/lib.rs @@ -28,9 +28,9 @@ impl ModuleTrait for Admin { ); } - fn actions(&self) -> Vec { + fn actions(&self) -> Vec { vec![ - hook_item!(BeforeRenderPageHook => before_render_page) + hook_action!(BeforeRenderPageHook => before_render_page) ] } } diff --git a/pagetop-node/src/lib.rs b/pagetop-node/src/lib.rs index bed7f9db..98b06c66 100644 --- a/pagetop-node/src/lib.rs +++ b/pagetop-node/src/lib.rs @@ -26,9 +26,9 @@ impl ModuleTrait for Node { cfg.route("/node", app::web::get().to(node)); } - fn actions(&self) -> Vec { + fn actions(&self) -> Vec { vec![ - hook_item!(BeforeRenderPageHook => before_render_page, -1) + hook_action!(BeforeRenderPageHook => before_render_page, -1) ] } diff --git a/pagetop/src/core/component/definition.rs b/pagetop/src/core/component/definition.rs index 787561b3..d8eb920e 100644 --- a/pagetop/src/core/component/definition.rs +++ b/pagetop/src/core/component/definition.rs @@ -27,6 +27,10 @@ pub trait ComponentTrait: AnyComponent + Send + Sync { true } + #[allow(unused_variables)] + fn before_render(&mut self, context: &mut InContext) { + } + #[allow(unused_variables)] fn default_render(&self, context: &mut InContext) -> Markup { html! {} diff --git a/pagetop/src/core/hook.rs b/pagetop/src/core/hook.rs index 8b83a58d..00b6d4ea 100644 --- a/pagetop/src/core/hook.rs +++ b/pagetop/src/core/hook.rs @@ -6,7 +6,7 @@ pub use definition::{ }; mod holder; -pub use holder::HookItem; +pub use holder::HookAction; use holder::HooksHolder; mod all; diff --git a/pagetop/src/core/hook/all.rs b/pagetop/src/core/hook/all.rs index 5c6f28d9..6321dfbc 100644 --- a/pagetop/src/core/hook/all.rs +++ b/pagetop/src/core/hook/all.rs @@ -1,5 +1,5 @@ use crate::Lazy; -use super::{HookItem, HooksHolder}; +use super::{HookAction, HooksHolder}; use std::sync::RwLock; use std::collections::HashMap; @@ -9,7 +9,7 @@ static ACTIONS: Lazy>> = Lazy::new(|| { RwLock::new(HashMap::new()) }); -pub fn add_hook(hook: HookItem) { +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) { @@ -19,7 +19,7 @@ pub fn add_hook(hook: HookItem) { } } -pub fn run_hooks(action_handler: &str, f: F) where F: FnMut(&HookItem) -> B { +pub fn run_hooks(action_handler: &str, f: F) where F: FnMut(&HookAction) -> B { if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) { actions.iter_map(f) } diff --git a/pagetop/src/core/hook/holder.rs b/pagetop/src/core/hook/holder.rs index cd138c67..59ce3b81 100644 --- a/pagetop/src/core/hook/holder.rs +++ b/pagetop/src/core/hook/holder.rs @@ -2,35 +2,35 @@ use super::HookTrait; use std::sync::{Arc, RwLock}; -pub type HookItem = Box; +pub type HookAction = Box; #[macro_export] -macro_rules! hook_item { +macro_rules! hook_action { ( $hook:ident => $f:ident $(, $weight:expr)? ) => {{ Box::new($hook::new().with_hook($f)$(.with_weight($weight))?) }}; } -pub struct HooksHolder(Arc>>); +pub struct HooksHolder(Arc>>); impl HooksHolder { pub fn new() -> Self { HooksHolder(Arc::new(RwLock::new(Vec::new()))) } - pub fn new_with(hook: HookItem) -> Self { + pub fn new_with(hook: HookAction) -> Self { let mut container = HooksHolder::new(); container.add(hook); container } - pub fn add(&mut self, hook: HookItem) { + 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 iter_map(&self, f: F) where Self: Sized, F: FnMut(&HookItem) -> B { + pub fn iter_map(&self, f: F) where Self: Sized, F: FnMut(&HookAction) -> B { let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect(); } } diff --git a/pagetop/src/core/module/definition.rs b/pagetop/src/core/module/definition.rs index 38621d82..5fe9e84e 100644 --- a/pagetop/src/core/module/definition.rs +++ b/pagetop/src/core/module/definition.rs @@ -1,5 +1,5 @@ use crate::{app, util}; -use crate::core::hook::HookItem; +use crate::core::hook::HookAction; #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] use crate::db::MigrationItem; @@ -28,7 +28,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync { fn configure_service(&self, cfg: &mut app::web::ServiceConfig) { } - fn actions(&self) -> Vec { + fn actions(&self) -> Vec { vec![] } diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index c772c91d..9c0f552a 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -25,7 +25,7 @@ pub use crate::app; pub use crate::app::AppTrait; pub use crate::app::application::Application; -pub use crate::{hook_item, core::{ +pub use crate::{hook_action, core::{ component::*, hook::*, module::*,