Retoca denominación y agrupación de las acciones
This commit is contained in:
parent
3eb9cfbff8
commit
8833a2aa7d
8 changed files with 40 additions and 40 deletions
|
|
@ -41,7 +41,7 @@ impl Application {
|
||||||
theme::all::register_themes(app.themes());
|
theme::all::register_themes(app.themes());
|
||||||
|
|
||||||
// Registra las acciones de todos los módulos.
|
// 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).
|
// Ejecuta actualizaciones pendientes de la base de datos (opcional).
|
||||||
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
|
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ macro_rules! hook_before_render_component {
|
||||||
weight: isize,
|
weight: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HookTrait for [< BeforeRender $Component >] {
|
impl HookActionTrait for [< BeforeRender $Component >] {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
[< BeforeRender $Component >] {
|
[< BeforeRender $Component >] {
|
||||||
action: None,
|
action: None,
|
||||||
|
|
@ -100,7 +100,7 @@ macro_rules! hook_before_render_component {
|
||||||
self.weight
|
self.weight
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_ref_any(&self) -> &dyn AnyHook {
|
fn as_ref_any(&self) -> &dyn AnyHookAction {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
mod definition;
|
mod definition;
|
||||||
pub use definition::{action_ref, AnyHook, HookTrait};
|
pub use definition::{action_ref, AnyHookAction, HookActionTrait};
|
||||||
|
|
||||||
mod holder;
|
mod holder;
|
||||||
pub use holder::HookAction;
|
pub use holder::HookAction;
|
||||||
use holder::HooksHolder;
|
use holder::ActionsHolder;
|
||||||
|
|
||||||
mod all;
|
mod all;
|
||||||
pub(crate) use all::add_hook;
|
pub(crate) use all::add_action;
|
||||||
pub use all::run_actions;
|
pub use all::run_actions;
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,20 @@
|
||||||
use super::{HookAction, HooksHolder};
|
use super::{HookAction, ActionsHolder};
|
||||||
use crate::Lazy;
|
use crate::Lazy;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
// Registered actions.
|
// Registered actions.
|
||||||
static ACTIONS: Lazy<RwLock<HashMap<&str, HooksHolder>>> =
|
static ACTIONS: Lazy<RwLock<HashMap<&str, ActionsHolder>>> =
|
||||||
Lazy::new(|| RwLock::new(HashMap::new()));
|
Lazy::new(|| RwLock::new(HashMap::new()));
|
||||||
|
|
||||||
pub fn add_hook(hook: HookAction) {
|
pub fn add_action(action: HookAction) {
|
||||||
let mut hmap = ACTIONS.write().unwrap();
|
let mut actions = ACTIONS.write().unwrap();
|
||||||
let action_handler = hook.handler();
|
let action_handler = action.handler();
|
||||||
if let Some(actions) = hmap.get_mut(action_handler) {
|
if let Some(holder) = actions.get_mut(action_handler) {
|
||||||
actions.add(hook);
|
holder.add(action);
|
||||||
} else {
|
} 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<B, F>(action_handler: &str, f: F)
|
||||||
where
|
where
|
||||||
F: FnMut(&HookAction) -> B,
|
F: FnMut(&HookAction) -> B,
|
||||||
{
|
{
|
||||||
if let Some(actions) = ACTIONS.read().unwrap().get(action_handler) {
|
if let Some(holder) = ACTIONS.read().unwrap().get(action_handler) {
|
||||||
actions.iter_map(f)
|
holder.iter_map(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
fn new() -> Self
|
||||||
where
|
where
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
@ -11,9 +11,9 @@ pub trait HookTrait: AnyHook + Send + Sync {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_ref_any(&self) -> &dyn AnyHook;
|
fn as_ref_any(&self) -> &dyn AnyHookAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn action_ref<A: 'static>(hook: &dyn HookTrait) -> &A {
|
pub fn action_ref<A: 'static>(action: &dyn HookActionTrait) -> &A {
|
||||||
hook.as_ref_any().downcast_ref::<A>().unwrap()
|
action.as_ref_any().downcast_ref::<A>().unwrap()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use super::HookTrait;
|
use super::HookActionTrait;
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
pub type HookAction = Box<dyn HookTrait>;
|
pub type HookAction = Box<dyn HookActionTrait>;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! hook_action {
|
macro_rules! hook_action {
|
||||||
|
|
@ -11,23 +11,23 @@ macro_rules! hook_action {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct HooksHolder(Arc<RwLock<Vec<HookAction>>>);
|
pub struct ActionsHolder(Arc<RwLock<Vec<HookAction>>>);
|
||||||
|
|
||||||
impl HooksHolder {
|
impl ActionsHolder {
|
||||||
pub fn new() -> Self {
|
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 {
|
pub fn new_with(action: HookAction) -> Self {
|
||||||
let mut container = HooksHolder::new();
|
let mut holder = ActionsHolder::new();
|
||||||
container.add(hook);
|
holder.add(action);
|
||||||
container
|
holder
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, hook: HookAction) {
|
pub fn add(&mut self, action: HookAction) {
|
||||||
let mut actions = self.0.write().unwrap();
|
let mut holder = self.0.write().unwrap();
|
||||||
actions.push(hook);
|
holder.push(action);
|
||||||
actions.sort_by_key(|a| a.weight());
|
holder.sort_by_key(|a| a.weight());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_map<B, F>(&self, f: F)
|
pub fn iter_map<B, F>(&self, f: F)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::ModuleTrait;
|
use super::ModuleTrait;
|
||||||
use crate::core::hook::add_hook;
|
use crate::core::hook::add_action;
|
||||||
use crate::{app, trace, Lazy};
|
use crate::{app, trace, Lazy};
|
||||||
|
|
||||||
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
|
#[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 m in ENABLED_MODULES.read().unwrap().iter() {
|
||||||
for a in m.actions().into_iter() {
|
for a in m.actions().into_iter() {
|
||||||
add_hook(a);
|
add_action(a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use super::Page;
|
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";
|
pub const HOOK_BEFORE_RENDER_PAGE: &str = "pagetop::hook::before_render_page";
|
||||||
|
|
||||||
|
|
@ -10,7 +10,7 @@ pub struct BeforeRenderPageHook {
|
||||||
weight: isize,
|
weight: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HookTrait for BeforeRenderPageHook {
|
impl HookActionTrait for BeforeRenderPageHook {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
BeforeRenderPageHook {
|
BeforeRenderPageHook {
|
||||||
hook: None,
|
hook: None,
|
||||||
|
|
@ -26,7 +26,7 @@ impl HookTrait for BeforeRenderPageHook {
|
||||||
self.weight
|
self.weight
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_ref_any(&self) -> &dyn AnyHook {
|
fn as_ref_any(&self) -> &dyn AnyHookAction {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue