Actualiza HookItem por HookAction
This commit is contained in:
parent
068f3d8520
commit
166f209dab
8 changed files with 21 additions and 17 deletions
|
|
@ -28,9 +28,9 @@ impl ModuleTrait for Admin {
|
|||
);
|
||||
}
|
||||
|
||||
fn actions(&self) -> Vec<HookItem> {
|
||||
fn actions(&self) -> Vec<HookAction> {
|
||||
vec![
|
||||
hook_item!(BeforeRenderPageHook => before_render_page)
|
||||
hook_action!(BeforeRenderPageHook => before_render_page)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,9 +26,9 @@ impl ModuleTrait for Node {
|
|||
cfg.route("/node", app::web::get().to(node));
|
||||
}
|
||||
|
||||
fn actions(&self) -> Vec<HookItem> {
|
||||
fn actions(&self) -> Vec<HookAction> {
|
||||
vec![
|
||||
hook_item!(BeforeRenderPageHook => before_render_page, -1)
|
||||
hook_action!(BeforeRenderPageHook => before_render_page, -1)
|
||||
]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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! {}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ pub use definition::{
|
|||
};
|
||||
|
||||
mod holder;
|
||||
pub use holder::HookItem;
|
||||
pub use holder::HookAction;
|
||||
use holder::HooksHolder;
|
||||
|
||||
mod all;
|
||||
|
|
|
|||
|
|
@ -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<RwLock<HashMap<&str, HooksHolder>>> = 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<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) {
|
||||
actions.iter_map(f)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,35 +2,35 @@ use super::HookTrait;
|
|||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
pub type HookItem = Box<dyn HookTrait>;
|
||||
pub type HookAction = Box<dyn HookTrait>;
|
||||
|
||||
#[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<RwLock<Vec<HookItem>>>);
|
||||
pub struct HooksHolder(Arc<RwLock<Vec<HookAction>>>);
|
||||
|
||||
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<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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<HookItem> {
|
||||
fn actions(&self) -> Vec<HookAction> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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::*,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue