Attribute Macro medea_macro::watchers [−][src]
#[watchers]
Generates ComponentState
implementation on provided impl
.
Usage
ⓘ
use std::rc::Rc; use medea_jason::utils::Component; use medea_macro::{watchers, watch}; struct SenderState { muted: ObservableCell<bool>, enabled: ObservableCell<bool>, } struct Sender; type SenderComponent = Component<SenderState, Sender>; #[watchers] impl SenderComponent { #[watch(self.muted.subscribe())] async fn muted_change_watcher( ctx: Rc<Sender>, state: Rc<SenderState>, new_muted_val: bool ) -> Result<(), ()> { Ok(()) } #[watch(self.enabled.subscribe())] async fn enabled_change_watcher( ctx: Rc<Sender>, state: Rc<SenderState>, new_enabled_val: bool, ) -> Result<(), ()> { Ok(()) } }
SenderComponent
implementation after macro expansion
ⓘ
impl SenderComponent { async fn muted_change_watcher( sender: Rc<Sender>, state: Rc<SenderState>, new_muted_val: bool ) -> Result<(), ()> { Ok(()) } async fn enabled_change_watcher( sender: Rc<Sender>, state: Rc<SenderState>, new_enabled_val: bool, ) -> Result<(), ()> { Ok(()) } } impl ComponentState<Sender> for SenderState { fn spawn_watchers(&self, s: &mut WatchersSpawner<SenderState, Sender>) { s.spawn( self.muted.subscribe(), SenderComponent::muted_change_watcher, ); s.spawn( self.enabled.subscribe(), SenderComponent::enabled_change_watcher, ); } }
Note, that ComponentState
implementation is simplified in this example
for better readability.
In reality object and state types will be obtained by casting
SenderComponent
to the ComponentTypes
trait and getting types from it.