Modifica y simplifica identificación de acciones

This commit is contained in:
Manuel Cillero 2022-05-07 15:15:14 +02:00
parent fce106af7a
commit a6d39202d4
10 changed files with 18 additions and 37 deletions

View file

@ -1,29 +1,30 @@
use crate::Lazy;
use crate::api::TypeId;
use super::{ActionItem, ActionsHolder};
use std::sync::RwLock;
use std::collections::HashMap;
// Registered actions.
static ACTIONS: Lazy<RwLock<HashMap<&str, ActionsHolder>>> = Lazy::new(|| {
static ACTIONS: Lazy<RwLock<HashMap<TypeId, ActionsHolder>>> = Lazy::new(|| {
RwLock::new(HashMap::new())
});
pub fn add_action(action: ActionItem) {
let mut hmap = ACTIONS.write().unwrap();
let action_name = action.machine_name();
if let Some(actions) = hmap.get_mut(action_name) {
let action_id = action.type_id();
if let Some(actions) = hmap.get_mut(&action_id) {
actions.add(action);
} else {
hmap.insert(action_name, ActionsHolder::new_with(action));
hmap.insert(action_id, ActionsHolder::new_with(action));
}
}
pub fn run_actions<B, F>(machine_name: &'static str, f: F)
pub fn run_actions<B, F>(action_id: TypeId, f: F)
where
F: FnMut(&ActionItem) -> B
{
if let Some(actions) = ACTIONS.read().unwrap().get(machine_name) {
if let Some(actions) = ACTIONS.read().unwrap().get(&action_id) {
actions.iter_map(f)
}
}

View file

@ -3,10 +3,6 @@ pub use std::any::Any as AnyAction;
pub trait ActionTrait: AnyAction + Send + Sync {
fn new() -> Self where Self: Sized;
fn machine_name(&self) -> &'static str {
std::any::type_name::<Self>()
}
fn weight(&self) -> isize {
0
}

View file

@ -1,8 +1,6 @@
use crate::api::action::{ActionTrait, AnyAction};
use super::{Assets, ComponentTrait};
pub const ACTION_BEFORE_RENDER_COMPONENT: &str = "pagetop::render::before_render_component";
pub struct ActionBeforeRenderComponent {
action: Option<fn(&mut dyn ComponentTrait, &mut Assets)>,
weight: isize,
@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderComponent {
}
}
fn machine_name(&self) -> &'static str {
ACTION_BEFORE_RENDER_COMPONENT
}
fn weight(&self) -> isize {
self.weight
}

View file

@ -1,8 +1,7 @@
use crate::html::{Markup, html};
use crate::api::action::{action_ref, run_actions};
use crate::api::{TypeId, action::{action_ref, run_actions}};
use crate::util;
use super::{ACTION_BEFORE_RENDER_COMPONENT, ActionBeforeRenderComponent};
use super::Assets;
use super::{ActionBeforeRenderComponent, Assets};
pub use std::any::Any as AnyComponent;
@ -76,7 +75,7 @@ pub fn render_component(component: &mut dyn ComponentTrait, assets: &mut Assets)
// Acciones de los módulos antes de renderizar el componente.
run_actions(
ACTION_BEFORE_RENDER_COMPONENT,
TypeId::of::<ActionBeforeRenderComponent>(),
|a| action_ref::<ActionBeforeRenderComponent>(&**a).run(component, assets)
);

View file

@ -1,8 +1,5 @@
mod action;
pub use action::{
ACTION_BEFORE_RENDER_COMPONENT,
ActionBeforeRenderComponent,
};
pub use action::ActionBeforeRenderComponent;
mod assets;
pub use assets::{

View file

@ -1,3 +1,5 @@
pub use std::any::TypeId;
pub mod action; // API to define functions that alter the behavior of PageTop core.
pub mod component; // API para crear nuevos componentes.
pub mod module; // API para añadir módulos con nuevas funcionalidades.

View file

@ -21,6 +21,7 @@ pub use crate::{
};
pub use crate::{action_item, api::{
TypeId,
action::*,
component::*,
module::*,

View file

@ -1,8 +1,6 @@
use crate::api::action::{ActionTrait, AnyAction};
use super::Page;
pub const ACTION_BEFORE_RENDER_PAGE: &str = "pagetop::render::before_render_page";
pub struct ActionBeforeRenderPage {
action: Option<fn(&mut Page)>,
weight: isize,
@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderPage {
}
}
fn machine_name(&self) -> &'static str {
ACTION_BEFORE_RENDER_PAGE
}
fn weight(&self) -> isize {
self.weight
}

View file

@ -1,8 +1,5 @@
mod action;
pub use action::{
ACTION_BEFORE_RENDER_PAGE,
ActionBeforeRenderPage,
};
pub use action::ActionBeforeRenderPage;
mod page;
pub use page::Page;

View file

@ -1,9 +1,9 @@
use crate::{Lazy, app, trace};
use crate::config::SETTINGS;
use crate::html::*;
use crate::api::action::{action_ref, run_actions};
use crate::api::{TypeId, action::{action_ref, run_actions}};
use crate::api::component::*;
use super::{ACTION_BEFORE_RENDER_PAGE, ActionBeforeRenderPage};
use super::ActionBeforeRenderPage;
use std::collections::HashMap;
@ -152,7 +152,7 @@ impl<'a> Page<'a> {
pub fn render(&mut self) -> app::Result<Markup> {
// Acciones de los módulos antes de renderizar la página.
run_actions(
ACTION_BEFORE_RENDER_PAGE,
TypeId::of::<ActionBeforeRenderPage>(),
|a| action_ref::<ActionBeforeRenderPage>(&**a).run(self)
);