Modifica y simplifica identificación de acciones
This commit is contained in:
parent
fce106af7a
commit
a6d39202d4
10 changed files with 18 additions and 37 deletions
|
|
@ -1,29 +1,30 @@
|
||||||
use crate::Lazy;
|
use crate::Lazy;
|
||||||
|
use crate::api::TypeId;
|
||||||
use super::{ActionItem, ActionsHolder};
|
use super::{ActionItem, ActionsHolder};
|
||||||
|
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// Registered actions.
|
// Registered actions.
|
||||||
static ACTIONS: Lazy<RwLock<HashMap<&str, ActionsHolder>>> = Lazy::new(|| {
|
static ACTIONS: Lazy<RwLock<HashMap<TypeId, ActionsHolder>>> = Lazy::new(|| {
|
||||||
RwLock::new(HashMap::new())
|
RwLock::new(HashMap::new())
|
||||||
});
|
});
|
||||||
|
|
||||||
pub fn add_action(action: ActionItem) {
|
pub fn add_action(action: ActionItem) {
|
||||||
let mut hmap = ACTIONS.write().unwrap();
|
let mut hmap = ACTIONS.write().unwrap();
|
||||||
let action_name = action.machine_name();
|
let action_id = action.type_id();
|
||||||
if let Some(actions) = hmap.get_mut(action_name) {
|
if let Some(actions) = hmap.get_mut(&action_id) {
|
||||||
actions.add(action);
|
actions.add(action);
|
||||||
} else {
|
} 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
|
where
|
||||||
F: FnMut(&ActionItem) -> B
|
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)
|
actions.iter_map(f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,6 @@ pub use std::any::Any as AnyAction;
|
||||||
pub trait ActionTrait: AnyAction + Send + Sync {
|
pub trait ActionTrait: AnyAction + Send + Sync {
|
||||||
fn new() -> Self where Self: Sized;
|
fn new() -> Self where Self: Sized;
|
||||||
|
|
||||||
fn machine_name(&self) -> &'static str {
|
|
||||||
std::any::type_name::<Self>()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn weight(&self) -> isize {
|
fn weight(&self) -> isize {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
use crate::api::action::{ActionTrait, AnyAction};
|
use crate::api::action::{ActionTrait, AnyAction};
|
||||||
use super::{Assets, ComponentTrait};
|
use super::{Assets, ComponentTrait};
|
||||||
|
|
||||||
pub const ACTION_BEFORE_RENDER_COMPONENT: &str = "pagetop::render::before_render_component";
|
|
||||||
|
|
||||||
pub struct ActionBeforeRenderComponent {
|
pub struct ActionBeforeRenderComponent {
|
||||||
action: Option<fn(&mut dyn ComponentTrait, &mut Assets)>,
|
action: Option<fn(&mut dyn ComponentTrait, &mut Assets)>,
|
||||||
weight: isize,
|
weight: isize,
|
||||||
|
|
@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn machine_name(&self) -> &'static str {
|
|
||||||
ACTION_BEFORE_RENDER_COMPONENT
|
|
||||||
}
|
|
||||||
|
|
||||||
fn weight(&self) -> isize {
|
fn weight(&self) -> isize {
|
||||||
self.weight
|
self.weight
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
use crate::html::{Markup, html};
|
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 crate::util;
|
||||||
use super::{ACTION_BEFORE_RENDER_COMPONENT, ActionBeforeRenderComponent};
|
use super::{ActionBeforeRenderComponent, Assets};
|
||||||
use super::Assets;
|
|
||||||
|
|
||||||
pub use std::any::Any as AnyComponent;
|
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.
|
// Acciones de los módulos antes de renderizar el componente.
|
||||||
run_actions(
|
run_actions(
|
||||||
ACTION_BEFORE_RENDER_COMPONENT,
|
TypeId::of::<ActionBeforeRenderComponent>(),
|
||||||
|a| action_ref::<ActionBeforeRenderComponent>(&**a).run(component, assets)
|
|a| action_ref::<ActionBeforeRenderComponent>(&**a).run(component, assets)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
mod action;
|
mod action;
|
||||||
pub use action::{
|
pub use action::ActionBeforeRenderComponent;
|
||||||
ACTION_BEFORE_RENDER_COMPONENT,
|
|
||||||
ActionBeforeRenderComponent,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod assets;
|
mod assets;
|
||||||
pub use assets::{
|
pub use assets::{
|
||||||
|
|
|
||||||
|
|
@ -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 action; // API to define functions that alter the behavior of PageTop core.
|
||||||
pub mod component; // API para crear nuevos componentes.
|
pub mod component; // API para crear nuevos componentes.
|
||||||
pub mod module; // API para añadir módulos con nuevas funcionalidades.
|
pub mod module; // API para añadir módulos con nuevas funcionalidades.
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ pub use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use crate::{action_item, api::{
|
pub use crate::{action_item, api::{
|
||||||
|
TypeId,
|
||||||
action::*,
|
action::*,
|
||||||
component::*,
|
component::*,
|
||||||
module::*,
|
module::*,
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
use crate::api::action::{ActionTrait, AnyAction};
|
use crate::api::action::{ActionTrait, AnyAction};
|
||||||
use super::Page;
|
use super::Page;
|
||||||
|
|
||||||
pub const ACTION_BEFORE_RENDER_PAGE: &str = "pagetop::render::before_render_page";
|
|
||||||
|
|
||||||
pub struct ActionBeforeRenderPage {
|
pub struct ActionBeforeRenderPage {
|
||||||
action: Option<fn(&mut Page)>,
|
action: Option<fn(&mut Page)>,
|
||||||
weight: isize,
|
weight: isize,
|
||||||
|
|
@ -16,10 +14,6 @@ impl ActionTrait for ActionBeforeRenderPage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn machine_name(&self) -> &'static str {
|
|
||||||
ACTION_BEFORE_RENDER_PAGE
|
|
||||||
}
|
|
||||||
|
|
||||||
fn weight(&self) -> isize {
|
fn weight(&self) -> isize {
|
||||||
self.weight
|
self.weight
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
mod action;
|
mod action;
|
||||||
pub use action::{
|
pub use action::ActionBeforeRenderPage;
|
||||||
ACTION_BEFORE_RENDER_PAGE,
|
|
||||||
ActionBeforeRenderPage,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod page;
|
mod page;
|
||||||
pub use page::Page;
|
pub use page::Page;
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use crate::{Lazy, app, trace};
|
use crate::{Lazy, app, trace};
|
||||||
use crate::config::SETTINGS;
|
use crate::config::SETTINGS;
|
||||||
use crate::html::*;
|
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 crate::api::component::*;
|
||||||
use super::{ACTION_BEFORE_RENDER_PAGE, ActionBeforeRenderPage};
|
use super::ActionBeforeRenderPage;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
|
@ -152,7 +152,7 @@ impl<'a> Page<'a> {
|
||||||
pub fn render(&mut self) -> app::Result<Markup> {
|
pub fn render(&mut self) -> app::Result<Markup> {
|
||||||
// Acciones de los módulos antes de renderizar la página.
|
// Acciones de los módulos antes de renderizar la página.
|
||||||
run_actions(
|
run_actions(
|
||||||
ACTION_BEFORE_RENDER_PAGE,
|
TypeId::of::<ActionBeforeRenderPage>(),
|
||||||
|a| action_ref::<ActionBeforeRenderPage>(&**a).run(self)
|
|a| action_ref::<ActionBeforeRenderPage>(&**a).run(self)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue