use crate::prelude::*; use crate::base::action::FnActionWithComponent; /// Ejecuta [`FnActionWithComponent`] antes de renderizar el componente. pub struct BeforeRender { f: FnActionWithComponent, referer_type_id: Option, referer_id: AttrId, weight: Weight, } /// Filtro para despachar [`FnActionWithComponent`] antes de renderizar un componente `C`. impl ActionDispatcher for BeforeRender { /// Devuelve el identificador de tipo ([`UniqueId`]) del componente `C`. fn referer_type_id(&self) -> Option { self.referer_type_id } /// Devuelve el identificador del componente. fn referer_id(&self) -> Option { self.referer_id.get() } /// Devuelve el peso para definir el orden de ejecución. fn weight(&self) -> Weight { self.weight } } impl BeforeRender { /// Permite [registrar](Extension::actions) una nueva acción [`FnActionWithComponent`]. pub fn new(f: FnActionWithComponent) -> Self { BeforeRender { f, referer_type_id: Some(UniqueId::of::()), referer_id: AttrId::default(), weight: 0, } } /// Afina el registro para ejecutar la acción [`FnActionWithComponent`] sólo para el componente /// `C` con identificador `id`. pub fn filter_by_referer_id(mut self, id: impl AsRef) -> Self { self.referer_id.alter_value(id); self } /// Opcional. Acciones con pesos más bajos se aplican antes. Se pueden usar valores negativos. pub fn with_weight(mut self, value: Weight) -> Self { self.weight = value; self } // Despacha las acciones. #[inline] pub(crate) fn dispatch(component: &mut C, cx: &mut Context) { // Primero despacha las acciones para el tipo de componente. dispatch_actions( &ActionKey::new( UniqueId::of::(), None, Some(UniqueId::of::()), None, ), |action: &Self| (action.f)(component, cx), ); // Y luego despacha las aciones para el tipo de componente con un identificador dado. if let Some(id) = component.id() { dispatch_actions( &ActionKey::new( UniqueId::of::(), None, Some(UniqueId::of::()), Some(id), ), |action: &Self| (action.f)(component, cx), ); } } }