🎨 Improve the code for actions

This commit is contained in:
Manuel Cillero 2023-11-04 12:57:14 +01:00
parent 7a68cf9be7
commit c108235613
8 changed files with 30 additions and 52 deletions

View file

@ -21,7 +21,7 @@ impl ModuleTrait for Admin {
actions![ actions![
action::page::BeforePrepareBody::with(before_prepare_body), action::page::BeforePrepareBody::with(before_prepare_body),
action::component::BeforePrepareComponent::<Menu>::with(before_prepare_menu) action::component::BeforePrepareComponent::<Menu>::with(before_prepare_menu)
.filtering_id("admin-menu-test"), .filter_by_referer_id("admin-menu-test"),
] ]
} }

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::FnAction; use super::FnAction;
pub struct AfterPrepareComponent<C: ComponentTrait> { pub struct AfterPrepareComponent<C: ComponentTrait> {
f: Option<FnAction<C>>, f: FnAction<C>,
referer_handle: Option<Handle>, referer_handle: Option<Handle>,
referer_id: OptionId, referer_id: OptionId,
weight: Weight, weight: Weight,
@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
impl<C: ComponentTrait> AfterPrepareComponent<C> { impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self { pub fn with(f: FnAction<C>) -> Self {
AfterPrepareComponent { AfterPrepareComponent {
f: Some(f), f,
referer_handle: Some(C::static_handle()), referer_handle: Some(C::static_handle()),
referer_id: OptionId::default(), referer_id: OptionId::default(),
weight: 0, weight: 0,
} }
} }
pub fn filtering_id(mut self, id: impl Into<String>) -> Self { pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
self.referer_id.alter_value(id); self.referer_id.alter_value(id);
self self
} }
@ -46,14 +46,10 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
} }
#[inline(always)] #[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
dispatch_actions( dispatch_actions(
(ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle()), id), (Self::static_handle(), Some(component.handle()), referer_id),
|action| { |action| (action_ref::<AfterPrepareComponent<C>>(&**action).f)(component, cx),
if let Some(f) = action_ref::<AfterPrepareComponent<C>>(&**action).f {
f(component, cx)
}
},
); );
} }
} }

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::FnAction; use super::FnAction;
pub struct BeforePrepareComponent<C: ComponentTrait> { pub struct BeforePrepareComponent<C: ComponentTrait> {
f: Option<FnAction<C>>, f: FnAction<C>,
referer_handle: Option<Handle>, referer_handle: Option<Handle>,
referer_id: OptionId, referer_id: OptionId,
weight: Weight, weight: Weight,
@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
impl<C: ComponentTrait> BeforePrepareComponent<C> { impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self { pub fn with(f: FnAction<C>) -> Self {
BeforePrepareComponent { BeforePrepareComponent {
f: Some(f), f,
referer_handle: Some(C::static_handle()), referer_handle: Some(C::static_handle()),
referer_id: OptionId::default(), referer_id: OptionId::default(),
weight: 0, weight: 0,
} }
} }
pub fn filtering_id(mut self, id: impl Into<String>) -> Self { pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
self.referer_id.alter_value(id); self.referer_id.alter_value(id);
self self
} }
@ -46,18 +46,10 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
} }
#[inline(always)] #[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
dispatch_actions( dispatch_actions(
( (Self::static_handle(), Some(component.handle()), referer_id),
ACTION_BEFORE_PREPARE_COMPONENT, |action| (action_ref::<BeforePrepareComponent<C>>(&**action).f)(component, cx),
Some(component.handle()),
id,
),
|action| {
if let Some(f) = action_ref::<BeforePrepareComponent<C>>(&**action).f {
f(component, cx)
}
},
); );
} }
} }

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::FnActionPage; use super::FnActionPage;
pub struct AfterPrepareBody { pub struct AfterPrepareBody {
f: Option<FnActionPage>, f: FnActionPage,
weight: Weight, weight: Weight,
} }
@ -17,10 +17,7 @@ impl ActionTrait for AfterPrepareBody {
impl AfterPrepareBody { impl AfterPrepareBody {
pub fn with(f: FnActionPage) -> Self { pub fn with(f: FnActionPage) -> Self {
AfterPrepareBody { AfterPrepareBody { f, weight: 0 }
f: Some(f),
weight: 0,
}
} }
pub fn with_weight(mut self, value: Weight) -> Self { pub fn with_weight(mut self, value: Weight) -> Self {
@ -30,10 +27,8 @@ impl AfterPrepareBody {
#[inline(always)] #[inline(always)]
pub(crate) fn dispatch(page: &mut Page) { pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((ACTION_AFTER_PREPARE_BODY, None, None), |action| { dispatch_actions((Self::static_handle(), None, None), |action| {
if let Some(f) = action_ref::<AfterPrepareBody>(&**action).f { (action_ref::<AfterPrepareBody>(&**action).f)(page)
f(page)
}
}); });
} }
} }

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::FnActionPage; use super::FnActionPage;
pub struct BeforePrepareBody { pub struct BeforePrepareBody {
f: Option<FnActionPage>, f: FnActionPage,
weight: Weight, weight: Weight,
} }
@ -17,10 +17,7 @@ impl ActionTrait for BeforePrepareBody {
impl BeforePrepareBody { impl BeforePrepareBody {
pub fn with(f: FnActionPage) -> Self { pub fn with(f: FnActionPage) -> Self {
BeforePrepareBody { BeforePrepareBody { f, weight: 0 }
f: Some(f),
weight: 0,
}
} }
pub fn with_weight(mut self, value: Weight) -> Self { pub fn with_weight(mut self, value: Weight) -> Self {
@ -30,10 +27,8 @@ impl BeforePrepareBody {
#[inline(always)] #[inline(always)]
pub(crate) fn dispatch(page: &mut Page) { pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((ACTION_BEFORE_PREPARE_BODY, None, None), |action| { dispatch_actions((Self::static_handle(), None, None), |action| {
if let Some(f) = action_ref::<BeforePrepareBody>(&**action).f { (action_ref::<BeforePrepareBody>(&**action).f)(page)
f(page)
}
}); });
} }
} }

View file

@ -7,4 +7,4 @@ use list::ActionsList;
mod all; mod all;
pub(crate) use all::add_action; pub(crate) use all::add_action;
pub use all::dispatch_actions; pub use all::{dispatch_actions, KeyAction};

View file

@ -4,31 +4,31 @@ use crate::{Handle, LazyStatic};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::RwLock; use std::sync::RwLock;
type KeyHandles = (Handle, Option<Handle>, Option<String>); pub type KeyAction = (Handle, Option<Handle>, Option<String>);
// Registered actions. // Registered actions.
static ACTIONS: LazyStatic<RwLock<HashMap<KeyHandles, ActionsList>>> = static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
LazyStatic::new(|| RwLock::new(HashMap::new())); LazyStatic::new(|| RwLock::new(HashMap::new()));
pub fn add_action(action: Action) { pub fn add_action(action: Action) {
let mut actions = ACTIONS.write().unwrap(); let mut actions = ACTIONS.write().unwrap();
let action_handle = ( let key_action = (
action.handle(), action.handle(),
action.referer_handle(), action.referer_handle(),
action.referer_id(), action.referer_id(),
); );
if let Some(list) = actions.get_mut(&action_handle) { if let Some(list) = actions.get_mut(&key_action) {
list.add(action); list.add(action);
} else { } else {
actions.insert(action_handle, ActionsList::with(action)); actions.insert(key_action, ActionsList::with(action));
} }
} }
pub fn dispatch_actions<B, F>(action_handle: (Handle, Option<Handle>, Option<String>), f: F) pub fn dispatch_actions<B, F>(key_action: KeyAction, f: F)
where where
F: FnMut(&Action) -> B, F: FnMut(&Action) -> B,
{ {
if let Some(list) = ACTIONS.read().unwrap().get(&action_handle) { if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
list.iter_map(f) list.iter_map(f)
} }
} }

View file

@ -4,7 +4,7 @@
pub use crate::{concat_string, fn_builder, main, paste, test}; pub use crate::{concat_string, fn_builder, main, paste, test};
// Global. // Global.
pub use crate::{Handle, HashMapResources, LazyStatic, Weight}; pub use crate::{Handle, HasHandle, HashMapResources, LazyStatic, Weight};
// Functions and macro helpers. // Functions and macro helpers.
pub use crate::util; pub use crate::util;