Retoca denominación y agrupación de las acciones

This commit is contained in:
Manuel Cillero 2022-07-20 18:44:00 +02:00
parent 3eb9cfbff8
commit 8833a2aa7d
8 changed files with 40 additions and 40 deletions

View file

@ -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"))]

View file

@ -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
}
}

View file

@ -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;

View file

@ -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<RwLock<HashMap<&str, HooksHolder>>> =
static ACTIONS: Lazy<RwLock<HashMap<&str, ActionsHolder>>> =
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<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)
if let Some(holder) = ACTIONS.read().unwrap().get(action_handler) {
holder.iter_map(f)
}
}

View file

@ -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<A: 'static>(hook: &dyn HookTrait) -> &A {
hook.as_ref_any().downcast_ref::<A>().unwrap()
pub fn action_ref<A: 'static>(action: &dyn HookActionTrait) -> &A {
action.as_ref_any().downcast_ref::<A>().unwrap()
}

View file

@ -1,8 +1,8 @@
use super::HookTrait;
use super::HookActionTrait;
use std::sync::{Arc, RwLock};
pub type HookAction = Box<dyn HookTrait>;
pub type HookAction = Box<dyn HookActionTrait>;
#[macro_export]
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 {
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<B, F>(&self, f: F)

View file

@ -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);
}
}
}

View file

@ -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
}
}