Actualiza HookItem por HookAction

This commit is contained in:
Manuel Cillero 2022-07-16 06:45:32 +02:00
parent 068f3d8520
commit 166f209dab
8 changed files with 21 additions and 17 deletions

View file

@ -28,9 +28,9 @@ impl ModuleTrait for Admin {
); );
} }
fn actions(&self) -> Vec<HookItem> { fn actions(&self) -> Vec<HookAction> {
vec![ vec![
hook_item!(BeforeRenderPageHook => before_render_page) hook_action!(BeforeRenderPageHook => before_render_page)
] ]
} }
} }

View file

@ -26,9 +26,9 @@ impl ModuleTrait for Node {
cfg.route("/node", app::web::get().to(node)); cfg.route("/node", app::web::get().to(node));
} }
fn actions(&self) -> Vec<HookItem> { fn actions(&self) -> Vec<HookAction> {
vec![ vec![
hook_item!(BeforeRenderPageHook => before_render_page, -1) hook_action!(BeforeRenderPageHook => before_render_page, -1)
] ]
} }

View file

@ -27,6 +27,10 @@ pub trait ComponentTrait: AnyComponent + Send + Sync {
true true
} }
#[allow(unused_variables)]
fn before_render(&mut self, context: &mut InContext) {
}
#[allow(unused_variables)] #[allow(unused_variables)]
fn default_render(&self, context: &mut InContext) -> Markup { fn default_render(&self, context: &mut InContext) -> Markup {
html! {} html! {}

View file

@ -6,7 +6,7 @@ pub use definition::{
}; };
mod holder; mod holder;
pub use holder::HookItem; pub use holder::HookAction;
use holder::HooksHolder; use holder::HooksHolder;
mod all; mod all;

View file

@ -1,5 +1,5 @@
use crate::Lazy; use crate::Lazy;
use super::{HookItem, HooksHolder}; use super::{HookAction, HooksHolder};
use std::sync::RwLock; use std::sync::RwLock;
use std::collections::HashMap; use std::collections::HashMap;
@ -9,7 +9,7 @@ static ACTIONS: Lazy<RwLock<HashMap<&str, HooksHolder>>> = Lazy::new(|| {
RwLock::new(HashMap::new()) RwLock::new(HashMap::new())
}); });
pub fn add_hook(hook: HookItem) { pub fn add_hook(hook: HookAction) {
let mut hmap = ACTIONS.write().unwrap(); let mut hmap = ACTIONS.write().unwrap();
let action_handler = hook.handler(); let action_handler = hook.handler();
if let Some(actions) = hmap.get_mut(action_handler) { if let Some(actions) = hmap.get_mut(action_handler) {
@ -19,7 +19,7 @@ pub fn add_hook(hook: HookItem) {
} }
} }
pub fn run_hooks<B, F>(action_handler: &str, f: F) where F: FnMut(&HookItem) -> B { pub fn run_hooks<B, F>(action_handler: &str, f: F) where F: FnMut(&HookAction) -> B {
if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) { if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) {
actions.iter_map(f) actions.iter_map(f)
} }

View file

@ -2,35 +2,35 @@ use super::HookTrait;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
pub type HookItem = Box<dyn HookTrait>; pub type HookAction = Box<dyn HookTrait>;
#[macro_export] #[macro_export]
macro_rules! hook_item { macro_rules! hook_action {
( $hook:ident => $f:ident $(, $weight:expr)? ) => {{ ( $hook:ident => $f:ident $(, $weight:expr)? ) => {{
Box::new($hook::new().with_hook($f)$(.with_weight($weight))?) Box::new($hook::new().with_hook($f)$(.with_weight($weight))?)
}}; }};
} }
pub struct HooksHolder(Arc<RwLock<Vec<HookItem>>>); pub struct HooksHolder(Arc<RwLock<Vec<HookAction>>>);
impl HooksHolder { impl HooksHolder {
pub fn new() -> Self { pub fn new() -> Self {
HooksHolder(Arc::new(RwLock::new(Vec::new()))) 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(); let mut container = HooksHolder::new();
container.add(hook); container.add(hook);
container container
} }
pub fn add(&mut self, hook: HookItem) { pub fn add(&mut self, hook: HookAction) {
let mut actions = self.0.write().unwrap(); let mut actions = self.0.write().unwrap();
actions.push(hook); actions.push(hook);
actions.sort_by_key(|a| a.weight()); actions.sort_by_key(|a| a.weight());
} }
pub fn iter_map<B, F>(&self, f: F) where Self: Sized, F: FnMut(&HookItem) -> B { pub fn iter_map<B, F>(&self, f: F) where Self: Sized, F: FnMut(&HookAction) -> B {
let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect(); let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect();
} }
} }

View file

@ -1,5 +1,5 @@
use crate::{app, util}; use crate::{app, util};
use crate::core::hook::HookItem; use crate::core::hook::HookAction;
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))] #[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
use crate::db::MigrationItem; use crate::db::MigrationItem;
@ -28,7 +28,7 @@ pub trait ModuleTrait: BaseModule + Send + Sync {
fn configure_service(&self, cfg: &mut app::web::ServiceConfig) { fn configure_service(&self, cfg: &mut app::web::ServiceConfig) {
} }
fn actions(&self) -> Vec<HookItem> { fn actions(&self) -> Vec<HookAction> {
vec![] vec![]
} }

View file

@ -25,7 +25,7 @@ pub use crate::app;
pub use crate::app::AppTrait; pub use crate::app::AppTrait;
pub use crate::app::application::Application; pub use crate::app::application::Application;
pub use crate::{hook_item, core::{ pub use crate::{hook_action, core::{
component::*, component::*,
hook::*, hook::*,
module::*, module::*,