🧑‍💻 Apply Handle changes to improve actions

This commit is contained in:
Manuel Cillero 2023-11-03 22:45:12 +01:00
parent 5b8c249785
commit 68c551bfb3
8 changed files with 150 additions and 150 deletions

View file

@ -1,133 +1,9 @@
#[macro_export]
macro_rules! actions_for_component {
( $Component:ty ) => {
$crate::paste! {
#[allow(unused_imports)]
use $crate::prelude::*;
use crate::prelude::*;
pub type [<FnAction $Component>] = fn(component: &$Component, cx: &mut Context);
pub type FnAction<C> = fn(component: &C, cx: &mut Context);
// *************************************************************************************
// ACTION BEFORE PREPARE COMPONENT
// *************************************************************************************
mod before_prepare_component;
pub use before_prepare_component::*;
$crate::new_handle!([<ACTION_BEFORE_PREPARE_ $Component:upper>] for Crate);
pub struct [<BeforePrepare $Component>] {
action: Option<[<FnAction $Component>]>,
weight: Weight,
}
impl ActionTrait for [<BeforePrepare $Component>] {
fn new() -> Self {
[<BeforePrepare $Component>] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
[<ACTION_BEFORE_PREPARE_ $Component:upper>]
}
fn weight(&self) -> Weight {
self.weight
}
}
impl [<BeforePrepare $Component>] {
#[allow(dead_code)]
pub fn with(action: [<FnAction $Component>]) -> Self {
[<BeforePrepare $Component>] {
action: Some(action),
weight: 0,
}
}
#[allow(dead_code)]
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) {
if let Some(action) = self.action {
action(component, cx)
}
}
}
#[inline(always)]
pub(crate) fn [<run_actions_before_prepare_ $Component:lower>](
component: &mut $Component,
cx: &mut Context
) {
run_actions([<ACTION_BEFORE_PREPARE_ $Component:upper>], |action|
action_ref::<[<BeforePrepare $Component>]>(&**action)
.run(component, cx)
);
}
// *************************************************************************************
// ACTION AFTER PREPARE COMPONENT
// *************************************************************************************
$crate::new_handle!([<ACTION_AFTER_PREPARE_ $Component:upper>] for Crate);
pub struct [<AfterPrepare $Component>] {
action: Option<[<FnAction $Component>]>,
weight: Weight,
}
impl ActionTrait for [<AfterPrepare $Component>] {
fn new() -> Self {
[<AfterPrepare $Component>] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
[<ACTION_AFTER_PREPARE_ $Component:upper>]
}
fn weight(&self) -> Weight {
self.weight
}
}
impl [<AfterPrepare $Component>] {
#[allow(dead_code)]
pub fn with(action: [<FnAction $Component>]) -> Self {
[<AfterPrepare $Component>] {
action: Some(action),
weight: 0,
}
}
#[allow(dead_code)]
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
pub(crate) fn run(&self, component: &mut $Component, cx: &mut Context) {
if let Some(action) = self.action {
action(component, cx)
}
}
}
#[inline(always)]
pub(crate) fn [<run_actions_after_prepare_ $Component:lower>](
component: &mut $Component,
cx: &mut Context
) {
run_actions([<ACTION_AFTER_PREPARE_ $Component:upper>], |action|
action_ref::<[<AfterPrepare $Component>]>(&**action)
.run(component, cx)
);
}
}
};
}
mod after_prepare_component;
pub use after_prepare_component::*;

View file

@ -0,0 +1,61 @@
use crate::prelude::*;
use super::FnAction;
pub struct AfterPrepareComponent<C: ComponentTrait> {
action: Option<FnAction<C>>,
referer: Option<Handle>,
weight: Weight,
}
impl_handle!(ACTION_AFTER_PREPARE_COMPONENT for AfterPrepareComponent<ComponentTrait>);
impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
fn new() -> Self {
AfterPrepareComponent {
action: None,
referer: Some(C::static_handle()),
weight: 0,
}
}
fn referer_handle(&self) -> Option<Handle> {
self.referer
}
fn weight(&self) -> Weight {
self.weight
}
}
impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn with(action: FnAction<C>) -> Self {
AfterPrepareComponent {
action: Some(action),
referer: Some(C::static_handle()),
weight: 0,
}
}
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
pub(crate) fn run(&self, component: &mut C, cx: &mut Context) {
if let Some(action) = self.action {
action(component, cx)
}
}
}
#[inline(always)]
pub(crate) fn run_actions_after_prepare_component<C: ComponentTrait>(
component: &mut C,
cx: &mut Context,
) {
run_actions(
(ACTION_AFTER_PREPARE_COMPONENT, Some(component.handle())),
|action| action_ref::<AfterPrepareComponent<C>>(&**action).run(component, cx),
);
}

View file

@ -0,0 +1,61 @@
use crate::prelude::*;
use super::FnAction;
pub struct BeforePrepareComponent<C: ComponentTrait> {
action: Option<FnAction<C>>,
referer: Option<Handle>,
weight: Weight,
}
impl_handle!(ACTION_BEFORE_PREPARE_COMPONENT for BeforePrepareComponent<ComponentTrait>);
impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
fn new() -> Self {
BeforePrepareComponent {
action: None,
referer: Some(C::static_handle()),
weight: 0,
}
}
fn referer_handle(&self) -> Option<Handle> {
self.referer
}
fn weight(&self) -> Weight {
self.weight
}
}
impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn with(action: FnAction<C>) -> Self {
BeforePrepareComponent {
action: Some(action),
referer: Some(C::static_handle()),
weight: 0,
}
}
pub fn with_weight(mut self, value: Weight) -> Self {
self.weight = value;
self
}
pub(crate) fn run(&self, component: &mut C, cx: &mut Context) {
if let Some(action) = self.action {
action(component, cx)
}
}
}
#[inline(always)]
pub(crate) fn run_actions_before_prepare_component<C: ComponentTrait>(
component: &mut C,
cx: &mut Context,
) {
run_actions(
(ACTION_BEFORE_PREPARE_COMPONENT, Some(component.handle())),
|action| action_ref::<BeforePrepareComponent<C>>(&**action).run(component, cx),
);
}

View file

@ -2,13 +2,13 @@ use crate::prelude::*;
use super::FnActionPage;
new_handle!(ACTION_AFTER_PREPARE_BODY for Crate);
pub struct AfterPrepareBody {
action: Option<FnActionPage>,
weight: Weight,
}
impl_handle!(ACTION_AFTER_PREPARE_BODY for AfterPrepareBody);
impl ActionTrait for AfterPrepareBody {
fn new() -> Self {
AfterPrepareBody {
@ -17,10 +17,6 @@ impl ActionTrait for AfterPrepareBody {
}
}
fn handle(&self) -> Handle {
ACTION_AFTER_PREPARE_BODY
}
fn weight(&self) -> Weight {
self.weight
}
@ -48,7 +44,7 @@ impl AfterPrepareBody {
#[inline(always)]
pub(crate) fn run_actions_after_prepare_body(page: &mut Page) {
run_actions(ACTION_AFTER_PREPARE_BODY, |action| {
run_actions((ACTION_AFTER_PREPARE_BODY, None), |action| {
action_ref::<AfterPrepareBody>(&**action).run(page)
});
}

View file

@ -2,13 +2,13 @@ use crate::prelude::*;
use super::FnActionPage;
new_handle!(ACTION_BEFORE_PREPARE_BODY for Crate);
pub struct BeforePrepareBody {
action: Option<FnActionPage>,
weight: Weight,
}
impl_handle!(ACTION_BEFORE_PREPARE_BODY for BeforePrepareBody);
impl ActionTrait for BeforePrepareBody {
fn new() -> Self {
BeforePrepareBody {
@ -17,10 +17,6 @@ impl ActionTrait for BeforePrepareBody {
}
}
fn handle(&self) -> Handle {
ACTION_BEFORE_PREPARE_BODY
}
fn weight(&self) -> Weight {
self.weight
}
@ -48,7 +44,7 @@ impl BeforePrepareBody {
#[inline(always)]
pub(crate) fn run_actions_before_prepare_body(page: &mut Page) {
run_actions(ACTION_BEFORE_PREPARE_BODY, |action| {
run_actions((ACTION_BEFORE_PREPARE_BODY, None), |action| {
action_ref::<BeforePrepareBody>(&**action).run(page)
});
}

View file

@ -5,12 +5,12 @@ use std::collections::HashMap;
use std::sync::RwLock;
// Registered actions.
static ACTIONS: LazyStatic<RwLock<HashMap<Handle, ActionsList>>> =
static ACTIONS: LazyStatic<RwLock<HashMap<(Handle, Option<Handle>), ActionsList>>> =
LazyStatic::new(|| RwLock::new(HashMap::new()));
pub fn add_action(action: Action) {
let mut actions = ACTIONS.write().unwrap();
let action_handle = action.handle();
let action_handle = (action.handle(), action.referer_handle());
if let Some(list) = actions.get_mut(&action_handle) {
list.add(action);
} else {
@ -18,7 +18,7 @@ pub fn add_action(action: Action) {
}
}
pub fn run_actions<B, F>(action_handle: Handle, f: F)
pub fn run_actions<B, F>(action_handle: (Handle, Option<Handle>), f: F)
where
F: FnMut(&Action) -> B,
{

View file

@ -1,4 +1,4 @@
use crate::{Handle, Weight};
use crate::{Handle, HasHandle, Weight};
use std::any::Any;
@ -6,12 +6,14 @@ pub trait ActionBase: Any {
fn as_ref_any(&self) -> &dyn Any;
}
pub trait ActionTrait: ActionBase + Send + Sync {
pub trait ActionTrait: ActionBase + HasHandle + Send + Sync {
fn new() -> Self
where
Self: Sized;
fn handle(&self) -> Handle;
fn referer_handle(&self) -> Option<Handle> {
None
}
fn weight(&self) -> Weight {
0

View file

@ -1,3 +1,5 @@
use crate::base::action::component::run_actions_after_prepare_component;
use crate::base::action::component::run_actions_before_prepare_component;
use crate::core::component::Context;
use crate::html::{html, Markup, PrepareMarkup};
use crate::{util, HasHandle, Weight};
@ -59,6 +61,9 @@ impl<C: ComponentTrait> ComponentBase for C {
// Acciones del tema antes de preparar el componente.
cx.theme().before_prepare_component(self, cx);
// Acciones de los módulos antes de preparar el componente.
run_actions_before_prepare_component(self, cx);
let markup = match cx.theme().render_component(self, cx) {
Some(html) => html,
None => match self.prepare_component(cx) {
@ -74,6 +79,9 @@ impl<C: ComponentTrait> ComponentBase for C {
// Acciones del tema después de preparar el componente.
cx.theme().after_prepare_component(self, cx);
// Acciones de los módulos después de preparar el componente.
run_actions_after_prepare_component(self, cx);
markup
} else {
html! {}