🎨 Passes action keys by reference in dispatch

This commit is contained in:
Manuel Cillero 2024-07-27 23:05:41 +02:00
parent b481d84cba
commit 5f9c1e4b15
9 changed files with 41 additions and 31 deletions

View file

@ -44,18 +44,19 @@ impl<C: ComponentTrait> AfterPrepare<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions( dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None), &ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx), |action: &Self| (action.f)(component, cx),
); );
if component.id().is_some() { if let Some(id) = component.id() {
dispatch_actions( dispatch_actions(
ActionKey::new( &ActionKey::new(
TypeId::of::<Self>(), TypeId::of::<Self>(),
None, None,
Some(TypeId::of::<C>()), Some(TypeId::of::<C>()),
component.id(), Some(id),
), ),
|action: &Self| (action.f)(component, cx), |action: &Self| (action.f)(component, cx),
); );

View file

@ -44,18 +44,19 @@ impl<C: ComponentTrait> BeforePrepare<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions( dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None), &ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx), |action: &Self| (action.f)(component, cx),
); );
if component.id().is_some() { if let Some(id) = component.id() {
dispatch_actions( dispatch_actions(
ActionKey::new( &ActionKey::new(
TypeId::of::<Self>(), TypeId::of::<Self>(),
None, None,
Some(TypeId::of::<C>()), Some(TypeId::of::<C>()),
component.id(), Some(id),
), ),
|action: &Self| (action.f)(component, cx), |action: &Self| (action.f)(component, cx),
); );

View file

@ -44,30 +44,33 @@ impl<C: ComponentTrait> IsRenderable<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &C, cx: &mut Context) -> bool { pub(crate) fn dispatch(component: &C, cx: &mut Context) -> bool {
let mut renderable = true; let mut renderable = true;
dispatch_actions( dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None), &ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| { |action: &Self| {
if renderable && !(action.f)(component, cx) { if renderable && !(action.f)(component, cx) {
renderable = false; renderable = false;
} }
}, },
); );
if renderable && component.id().is_some() { if renderable {
dispatch_actions( if let Some(id) = component.id() {
ActionKey::new( dispatch_actions(
TypeId::of::<Self>(), &ActionKey::new(
None, TypeId::of::<Self>(),
Some(TypeId::of::<C>()), None,
component.id(), Some(TypeId::of::<C>()),
), Some(id),
|action: &Self| { ),
if renderable && !(action.f)(component, cx) { |action: &Self| {
renderable = false; if renderable && !(action.f)(component, cx) {
} renderable = false;
}, }
); },
);
}
} }
renderable renderable
} }

View file

@ -24,9 +24,10 @@ impl AfterPrepareBody {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(page: &mut Page) { pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions( dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None), &ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page), |action: &Self| (action.f)(page),
); );
} }

View file

@ -24,9 +24,10 @@ impl BeforePrepareBody {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(page: &mut Page) { pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions( dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None), &ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page), |action: &Self| (action.f)(page),
); );
} }

View file

@ -28,9 +28,10 @@ impl<C: ComponentTrait> AfterPrepare<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions( dispatch_actions(
ActionKey::new( &ActionKey::new(
TypeId::of::<Self>(), TypeId::of::<Self>(),
Some(cx.theme().type_id()), Some(cx.theme().type_id()),
Some(TypeId::of::<C>()), Some(TypeId::of::<C>()),

View file

@ -28,9 +28,10 @@ impl<C: ComponentTrait> BeforePrepare<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) { pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions( dispatch_actions(
ActionKey::new( &ActionKey::new(
TypeId::of::<Self>(), TypeId::of::<Self>(),
Some(cx.theme().type_id()), Some(cx.theme().type_id()),
Some(TypeId::of::<C>()), Some(TypeId::of::<C>()),

View file

@ -28,10 +28,11 @@ impl<C: ComponentTrait> RenderComponent<C> {
} }
#[inline(always)] #[inline(always)]
#[allow(clippy::inline_always)]
pub(crate) fn dispatch(component: &C, cx: &mut Context) -> Option<Markup> { pub(crate) fn dispatch(component: &C, cx: &mut Context) -> Option<Markup> {
let mut render_component: Option<Markup> = None; let mut render_component: Option<Markup> = None;
dispatch_actions( dispatch_actions(
ActionKey::new( &ActionKey::new(
TypeId::of::<Self>(), TypeId::of::<Self>(),
Some(cx.theme().type_id()), Some(cx.theme().type_id()),
Some(TypeId::of::<C>()), Some(TypeId::of::<C>()),

View file

@ -19,12 +19,12 @@ pub fn add_action(action: ActionBox) {
} }
} }
pub fn dispatch_actions<A, B, F>(key: ActionKey, f: F) pub fn dispatch_actions<A, B, F>(key: &ActionKey, f: F)
where where
A: ActionTrait, A: ActionTrait,
F: FnMut(&A) -> B, F: FnMut(&A) -> B,
{ {
if let Some(list) = ACTIONS.read().unwrap().get(&key) { if let Some(list) = ACTIONS.read().unwrap().get(key) {
list.iter_map(f) list.iter_map(f);
} }
} }