Skip to content

Commit 23dc58c

Browse files
committed
🧑‍💻 Simplify action creation and management
1 parent 81b1877 commit 23dc58c

File tree

10 files changed

+106
-118
lines changed

10 files changed

+106
-118
lines changed

pagetop/src/base/action/component.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::prelude::*;
22

3-
pub type FnAction<C> = fn(component: &C, cx: &mut Context);
3+
pub type FnAction<C> = fn(component: &mut C, cx: &mut Context);
44

55
mod before_prepare_component;
66
pub use before_prepare_component::*;

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@ use crate::prelude::*;
33
use super::FnAction;
44

55
pub struct AfterPrepareComponent<C: ComponentTrait> {
6-
action: Option<FnAction<C>>,
7-
referer: Option<Handle>,
6+
f: Option<FnAction<C>>,
7+
referer_handle: Option<Handle>,
8+
referer_id: OptionId,
89
weight: Weight,
910
}
1011

1112
impl_handle!(ACTION_AFTER_PREPARE_COMPONENT for AfterPrepareComponent<ComponentTrait>);
1213

1314
impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
14-
fn new() -> Self {
15-
AfterPrepareComponent {
16-
action: None,
17-
referer: Some(C::static_handle()),
18-
weight: 0,
19-
}
15+
fn referer_handle(&self) -> Option<Handle> {
16+
self.referer_handle
2017
}
2118

22-
fn referer_handle(&self) -> Option<Handle> {
23-
self.referer
19+
fn referer_id(&self) -> Option<String> {
20+
self.referer_id.get()
2421
}
2522

2623
fn weight(&self) -> Weight {
@@ -29,33 +26,34 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
2926
}
3027

3128
impl<C: ComponentTrait> AfterPrepareComponent<C> {
32-
pub fn with(action: FnAction<C>) -> Self {
29+
pub fn with(f: FnAction<C>) -> Self {
3330
AfterPrepareComponent {
34-
action: Some(action),
35-
referer: Some(C::static_handle()),
31+
f: Some(f),
32+
referer_handle: Some(C::static_handle()),
33+
referer_id: OptionId::default(),
3634
weight: 0,
3735
}
3836
}
3937

38+
pub fn filtering_id(mut self, id: impl Into<String>) -> Self {
39+
self.referer_id.alter_value(id);
40+
self
41+
}
42+
4043
pub fn with_weight(mut self, value: Weight) -> Self {
4144
self.weight = value;
4245
self
4346
}
4447

45-
pub(crate) fn run(&self, component: &mut C, cx: &mut Context) {
46-
if let Some(action) = self.action {
47-
action(component, cx)
48-
}
48+
#[inline(always)]
49+
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) {
50+
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+
},
57+
);
4958
}
5059
}
51-
52-
#[inline(always)]
53-
pub(crate) fn run_actions_after_prepare_component<C: ComponentTrait>(
54-
component: &mut C,
55-
cx: &mut Context,
56-
) {
57-
run_actions(
58-
(ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle())),
59-
|action| action_ref::<AfterPrepareComponent<C>>(&**action).run(component, cx),
60-
);
61-
}

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

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@ use crate::prelude::*;
33
use super::FnAction;
44

55
pub struct BeforePrepareComponent<C: ComponentTrait> {
6-
action: Option<FnAction<C>>,
7-
referer: Option<Handle>,
6+
f: Option<FnAction<C>>,
7+
referer_handle: Option<Handle>,
8+
referer_id: OptionId,
89
weight: Weight,
910
}
1011

1112
impl_handle!(ACTION_BEFORE_PREPARE_COMPONENT for BeforePrepareComponent<ComponentTrait>);
1213

1314
impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
14-
fn new() -> Self {
15-
BeforePrepareComponent {
16-
action: None,
17-
referer: Some(C::static_handle()),
18-
weight: 0,
19-
}
15+
fn referer_handle(&self) -> Option<Handle> {
16+
self.referer_handle
2017
}
2118

22-
fn referer_handle(&self) -> Option<Handle> {
23-
self.referer
19+
fn referer_id(&self) -> Option<String> {
20+
self.referer_id.get()
2421
}
2522

2623
fn weight(&self) -> Weight {
@@ -29,33 +26,38 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
2926
}
3027

3128
impl<C: ComponentTrait> BeforePrepareComponent<C> {
32-
pub fn with(action: FnAction<C>) -> Self {
29+
pub fn with(f: FnAction<C>) -> Self {
3330
BeforePrepareComponent {
34-
action: Some(action),
35-
referer: Some(C::static_handle()),
31+
f: Some(f),
32+
referer_handle: Some(C::static_handle()),
33+
referer_id: OptionId::default(),
3634
weight: 0,
3735
}
3836
}
3937

38+
pub fn filtering_id(mut self, id: impl Into<String>) -> Self {
39+
self.referer_id.alter_value(id);
40+
self
41+
}
42+
4043
pub fn with_weight(mut self, value: Weight) -> Self {
4144
self.weight = value;
4245
self
4346
}
4447

45-
pub(crate) fn run(&self, component: &mut C, cx: &mut Context) {
46-
if let Some(action) = self.action {
47-
action(component, cx)
48-
}
48+
#[inline(always)]
49+
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, id: Option<String>) {
50+
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+
},
61+
);
4962
}
5063
}
51-
52-
#[inline(always)]
53-
pub(crate) fn run_actions_before_prepare_component<C: ComponentTrait>(
54-
component: &mut C,
55-
cx: &mut Context,
56-
) {
57-
run_actions(
58-
(ACTION_BEFORE_PREPARE_COMPONENT, Some(component.handle())),
59-
|action| action_ref::<BeforePrepareComponent<C>>(&**action).run(component, cx),
60-
);
61-
}

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,22 @@ use crate::prelude::*;
33
use super::FnActionPage;
44

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

1010
impl_handle!(ACTION_AFTER_PREPARE_BODY for AfterPrepareBody);
1111

1212
impl ActionTrait for AfterPrepareBody {
13-
fn new() -> Self {
14-
AfterPrepareBody {
15-
action: None,
16-
weight: 0,
17-
}
18-
}
19-
2013
fn weight(&self) -> Weight {
2114
self.weight
2215
}
2316
}
2417

2518
impl AfterPrepareBody {
26-
pub fn with(action: FnActionPage) -> Self {
19+
pub fn with(f: FnActionPage) -> Self {
2720
AfterPrepareBody {
28-
action: Some(action),
21+
f: Some(f),
2922
weight: 0,
3023
}
3124
}
@@ -35,16 +28,12 @@ impl AfterPrepareBody {
3528
self
3629
}
3730

38-
pub(crate) fn run(&self, page: &mut Page) {
39-
if let Some(action) = self.action {
40-
action(page)
41-
}
31+
#[inline(always)]
32+
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+
}
37+
});
4238
}
4339
}
44-
45-
#[inline(always)]
46-
pub(crate) fn run_actions_after_prepare_body(page: &mut Page) {
47-
run_actions((ACTION_AFTER_PREPARE_BODY, None), |action| {
48-
action_ref::<AfterPrepareBody>(&**action).run(page)
49-
});
50-
}

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,22 @@ use crate::prelude::*;
33
use super::FnActionPage;
44

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

1010
impl_handle!(ACTION_BEFORE_PREPARE_BODY for BeforePrepareBody);
1111

1212
impl ActionTrait for BeforePrepareBody {
13-
fn new() -> Self {
14-
BeforePrepareBody {
15-
action: None,
16-
weight: 0,
17-
}
18-
}
19-
2013
fn weight(&self) -> Weight {
2114
self.weight
2215
}
2316
}
2417

2518
impl BeforePrepareBody {
26-
pub fn with(action: FnActionPage) -> Self {
19+
pub fn with(f: FnActionPage) -> Self {
2720
BeforePrepareBody {
28-
action: Some(action),
21+
f: Some(f),
2922
weight: 0,
3023
}
3124
}
@@ -35,16 +28,12 @@ impl BeforePrepareBody {
3528
self
3629
}
3730

38-
pub(crate) fn run(&self, page: &mut Page) {
39-
if let Some(action) = self.action {
40-
action(page)
41-
}
31+
#[inline(always)]
32+
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+
}
37+
});
4238
}
4339
}
44-
45-
#[inline(always)]
46-
pub(crate) fn run_actions_before_prepare_body(page: &mut Page) {
47-
run_actions((ACTION_BEFORE_PREPARE_BODY, None), |action| {
48-
action_ref::<BeforePrepareBody>(&**action).run(page)
49-
});
50-
}

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::run_actions;
10+
pub use all::dispatch_actions;

pagetop/src/core/action/all.rs

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

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

99
// Registered actions.
1010
static ACTIONS: LazyStatic<RwLock<HashMap<KeyHandles, 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 = (action.handle(), action.referer_handle());
15+
let action_handle = (
16+
action.handle(),
17+
action.referer_handle(),
18+
action.referer_id(),
19+
);
1620
if let Some(list) = actions.get_mut(&action_handle) {
1721
list.add(action);
1822
} else {
1923
actions.insert(action_handle, ActionsList::with(action));
2024
}
2125
}
2226

23-
pub fn run_actions<B, F>(action_handle: (Handle, Option<Handle>), f: F)
27+
pub fn dispatch_actions<B, F>(action_handle: (Handle, Option<Handle>, Option<String>), f: F)
2428
where
2529
F: FnMut(&Action) -> B,
2630
{

pagetop/src/core/action/definition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ pub trait ActionBase: Any {
77
}
88

99
pub trait ActionTrait: ActionBase + HasHandle + Send + Sync {
10-
fn new() -> Self
11-
where
12-
Self: Sized;
13-
1410
fn referer_handle(&self) -> Option<Handle> {
1511
None
1612
}
1713

14+
fn referer_id(&self) -> Option<String> {
15+
None
16+
}
17+
1818
fn weight(&self) -> Weight {
1919
0
2020
}

pagetop/src/core/component/definition.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::base::action::component::run_actions_after_prepare_component;
2-
use crate::base::action::component::run_actions_before_prepare_component;
1+
use crate::base::action;
32
use crate::core::component::Context;
43
use crate::html::{html, Markup, PrepareMarkup};
54
use crate::{util, HasHandle, Weight};
@@ -62,8 +61,12 @@ impl<C: ComponentTrait> ComponentBase for C {
6261
cx.theme().before_prepare_component(self, cx);
6362

6463
// Acciones de los módulos antes de preparar el componente.
65-
run_actions_before_prepare_component(self, cx);
64+
action::component::BeforePrepareComponent::dispatch(self, cx, None);
65+
if let Some(id) = self.id() {
66+
action::component::BeforePrepareComponent::dispatch(self, cx, Some(id));
67+
}
6668

69+
// Renderiza el componente.
6770
let markup = match cx.theme().render_component(self, cx) {
6871
Some(html) => html,
6972
None => match self.prepare_component(cx) {
@@ -80,7 +83,10 @@ impl<C: ComponentTrait> ComponentBase for C {
8083
cx.theme().after_prepare_component(self, cx);
8184

8285
// Acciones de los módulos después de preparar el componente.
83-
run_actions_after_prepare_component(self, cx);
86+
action::component::AfterPrepareComponent::dispatch(self, cx, None);
87+
if let Some(id) = self.id() {
88+
action::component::AfterPrepareComponent::dispatch(self, cx, Some(id));
89+
}
8490

8591
markup
8692
} else {

0 commit comments

Comments
 (0)