Skip to content

Commit c108235

Browse files
committed
🎨 Improve the code for actions
1 parent 7a68cf9 commit c108235

File tree

8 files changed

+30
-52
lines changed

8 files changed

+30
-52
lines changed

pagetop-admin/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl ModuleTrait for Admin {
2121
actions![
2222
action::page::BeforePrepareBody::with(before_prepare_body),
2323
action::component::BeforePrepareComponent::<Menu>::with(before_prepare_menu)
24-
.filtering_id("admin-menu-test"),
24+
.filter_by_referer_id("admin-menu-test"),
2525
]
2626
}
2727

pagetop/src/base/action/component/after_prepare_component.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use super::FnAction;
44

55
pub struct AfterPrepareComponent<C: ComponentTrait> {
6-
f: Option<FnAction<C>>,
6+
f: FnAction<C>,
77
referer_handle: Option<Handle>,
88
referer_id: OptionId,
99
weight: Weight,
@@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
2828
impl<C: ComponentTrait> AfterPrepareComponent<C> {
2929
pub fn with(f: FnAction<C>) -> Self {
3030
AfterPrepareComponent {
31-
f: Some(f),
31+
f,
3232
referer_handle: Some(C::static_handle()),
3333
referer_id: OptionId::default(),
3434
weight: 0,
3535
}
3636
}
3737

38-
pub fn filtering_id(mut self, id: impl Into<String>) -> Self {
38+
pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
3939
self.referer_id.alter_value(id);
4040
self
4141
}
@@ -46,14 +46,10 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
4646
}
4747

4848
#[inline(always)]
49-
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) {
49+
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
5050
dispatch_actions(
51-
(ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle()), id),
52-
|action| {
53-
if let Some(f) = action_ref::<AfterPrepareComponent<C>>(&**action).f {
54-
f(component, cx)
55-
}
56-
},
51+
(Self::static_handle(), Some(component.handle()), referer_id),
52+
|action| (action_ref::<AfterPrepareComponent<C>>(&**action).f)(component, cx),
5753
);
5854
}
5955
}

pagetop/src/base/action/component/before_prepare_component.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use super::FnAction;
44

55
pub struct BeforePrepareComponent<C: ComponentTrait> {
6-
f: Option<FnAction<C>>,
6+
f: FnAction<C>,
77
referer_handle: Option<Handle>,
88
referer_id: OptionId,
99
weight: Weight,
@@ -28,14 +28,14 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
2828
impl<C: ComponentTrait> BeforePrepareComponent<C> {
2929
pub fn with(f: FnAction<C>) -> Self {
3030
BeforePrepareComponent {
31-
f: Some(f),
31+
f,
3232
referer_handle: Some(C::static_handle()),
3333
referer_id: OptionId::default(),
3434
weight: 0,
3535
}
3636
}
3737

38-
pub fn filtering_id(mut self, id: impl Into<String>) -> Self {
38+
pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
3939
self.referer_id.alter_value(id);
4040
self
4141
}
@@ -46,18 +46,10 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
4646
}
4747

4848
#[inline(always)]
49-
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) {
49+
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
5050
dispatch_actions(
51-
(
52-
ACTION_BEFORE_PREPARE_COMPONENT,
53-
Some(component.handle()),
54-
id,
55-
),
56-
|action| {
57-
if let Some(f) = action_ref::<BeforePrepareComponent<C>>(&**action).f {
58-
f(component, cx)
59-
}
60-
},
51+
(Self::static_handle(), Some(component.handle()), referer_id),
52+
|action| (action_ref::<BeforePrepareComponent<C>>(&**action).f)(component, cx),
6153
);
6254
}
6355
}

pagetop/src/base/action/page/after_prepare_body.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use super::FnActionPage;
44

55
pub struct AfterPrepareBody {
6-
f: Option<FnActionPage>,
6+
f: FnActionPage,
77
weight: Weight,
88
}
99

@@ -17,10 +17,7 @@ impl ActionTrait for AfterPrepareBody {
1717

1818
impl AfterPrepareBody {
1919
pub fn with(f: FnActionPage) -> Self {
20-
AfterPrepareBody {
21-
f: Some(f),
22-
weight: 0,
23-
}
20+
AfterPrepareBody { f, weight: 0 }
2421
}
2522

2623
pub fn with_weight(mut self, value: Weight) -> Self {
@@ -30,10 +27,8 @@ impl AfterPrepareBody {
3027

3128
#[inline(always)]
3229
pub(crate) fn dispatch(page: &mut Page) {
33-
dispatch_actions((ACTION_AFTER_PREPARE_BODY, None, None), |action| {
34-
if let Some(f) = action_ref::<AfterPrepareBody>(&**action).f {
35-
f(page)
36-
}
30+
dispatch_actions((Self::static_handle(), None, None), |action| {
31+
(action_ref::<AfterPrepareBody>(&**action).f)(page)
3732
});
3833
}
3934
}

pagetop/src/base/action/page/before_prepare_body.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use super::FnActionPage;
44

55
pub struct BeforePrepareBody {
6-
f: Option<FnActionPage>,
6+
f: FnActionPage,
77
weight: Weight,
88
}
99

@@ -17,10 +17,7 @@ impl ActionTrait for BeforePrepareBody {
1717

1818
impl BeforePrepareBody {
1919
pub fn with(f: FnActionPage) -> Self {
20-
BeforePrepareBody {
21-
f: Some(f),
22-
weight: 0,
23-
}
20+
BeforePrepareBody { f, weight: 0 }
2421
}
2522

2623
pub fn with_weight(mut self, value: Weight) -> Self {
@@ -30,10 +27,8 @@ impl BeforePrepareBody {
3027

3128
#[inline(always)]
3229
pub(crate) fn dispatch(page: &mut Page) {
33-
dispatch_actions((ACTION_BEFORE_PREPARE_BODY, None, None), |action| {
34-
if let Some(f) = action_ref::<BeforePrepareBody>(&**action).f {
35-
f(page)
36-
}
30+
dispatch_actions((Self::static_handle(), None, None), |action| {
31+
(action_ref::<BeforePrepareBody>(&**action).f)(page)
3732
});
3833
}
3934
}

pagetop/src/core/action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ use list::ActionsList;
77

88
mod all;
99
pub(crate) use all::add_action;
10-
pub use all::dispatch_actions;
10+
pub use all::{dispatch_actions, KeyAction};

pagetop/src/core/action/all.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ use crate::{Handle, LazyStatic};
44
use std::collections::HashMap;
55
use std::sync::RwLock;
66

7-
type KeyHandles = (Handle, Option<Handle>, Option<String>);
7+
pub type KeyAction = (Handle, Option<Handle>, Option<String>);
88

99
// Registered actions.
10-
static ACTIONS: LazyStatic<RwLock<HashMap<KeyHandles, ActionsList>>> =
10+
static ACTIONS: LazyStatic<RwLock<HashMap<KeyAction, ActionsList>>> =
1111
LazyStatic::new(|| RwLock::new(HashMap::new()));
1212

1313
pub fn add_action(action: Action) {
1414
let mut actions = ACTIONS.write().unwrap();
15-
let action_handle = (
15+
let key_action = (
1616
action.handle(),
1717
action.referer_handle(),
1818
action.referer_id(),
1919
);
20-
if let Some(list) = actions.get_mut(&action_handle) {
20+
if let Some(list) = actions.get_mut(&key_action) {
2121
list.add(action);
2222
} else {
23-
actions.insert(action_handle, ActionsList::with(action));
23+
actions.insert(key_action, ActionsList::with(action));
2424
}
2525
}
2626

27-
pub fn dispatch_actions<B, F>(action_handle: (Handle, Option<Handle>, Option<String>), f: F)
27+
pub fn dispatch_actions<B, F>(key_action: KeyAction, f: F)
2828
where
2929
F: FnMut(&Action) -> B,
3030
{
31-
if let Some(list) = ACTIONS.read().unwrap().get(&action_handle) {
31+
if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
3232
list.iter_map(f)
3333
}
3434
}

pagetop/src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub use crate::{concat_string, fn_builder, main, paste, test};
55

66
// Global.
7-
pub use crate::{Handle, HashMapResources, LazyStatic, Weight};
7+
pub use crate::{Handle, HasHandle, HashMapResources, LazyStatic, Weight};
88

99
// Functions and macro helpers.
1010
pub use crate::util;

0 commit comments

Comments
 (0)