🎨 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![
action::page::BeforePrepareBody::with(before_prepare_body),
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;
pub struct AfterPrepareComponent<C: ComponentTrait> {
f: Option<FnAction<C>>,
f: FnAction<C>,
referer_handle: Option<Handle>,
referer_id: OptionId,
weight: Weight,
@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self {
AfterPrepareComponent {
f: Some(f),
f,
referer_handle: Some(C::static_handle()),
referer_id: OptionId::default(),
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
}
@ -46,14 +46,10 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
}
#[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(
(ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle()), id),
|action| {
if let Some(f) = action_ref::<AfterPrepareComponent<C>>(&**action).f {
f(component, cx)
}
},
(Self::static_handle(), Some(component.handle()), referer_id),
|action| (action_ref::<AfterPrepareComponent<C>>(&**action).f)(component, cx),
);
}
}

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::FnAction;
pub struct BeforePrepareComponent<C: ComponentTrait> {
f: Option<FnAction<C>>,
f: FnAction<C>,
referer_handle: Option<Handle>,
referer_id: OptionId,
weight: Weight,
@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self {
BeforePrepareComponent {
f: Some(f),
f,
referer_handle: Some(C::static_handle()),
referer_id: OptionId::default(),
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
}
@ -46,18 +46,10 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
}
#[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(
(
ACTION_BEFORE_PREPARE_COMPONENT,
Some(component.handle()),
id,
),
|action| {
if let Some(f) = action_ref::<BeforePrepareComponent<C>>(&**action).f {
f(component, cx)
}
},
(Self::static_handle(), Some(component.handle()), referer_id),
|action| (action_ref::<BeforePrepareComponent<C>>(&**action).f)(component, cx),
);
}
}

View file

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

View file

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

View file

@ -7,4 +7,4 @@ use list::ActionsList;
mod all;
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::sync::RwLock;
type KeyHandles = (Handle, Option<Handle>, Option<String>);
pub type KeyAction = (Handle, Option<Handle>, Option<String>);
// Registered actions.
static ACTIONS: LazyStatic<RwLock<HashMap<KeyHandles, ActionsList>>> =
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
LazyStatic::new(|| RwLock::new(HashMap::new()));
pub fn add_action(action: Action) {
let mut actions = ACTIONS.write().unwrap();
let action_handle = (
let key_action = (
action.handle(),
action.referer_handle(),
action.referer_id(),
);
if let Some(list) = actions.get_mut(&action_handle) {
if let Some(list) = actions.get_mut(&key_action) {
list.add(action);
} 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
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)
}
}

View file

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