relm4/component/sync/
state_watcher.rs1use crate::{Component, ComponentParts, ShutdownOnDrop};
2
3use std::cell::{Ref, RefCell, RefMut};
4use std::fmt::{self, Debug};
5use std::rc::Rc;
6
7pub struct StateWatcher<C: Component> {
11 pub(super) state: Rc<RefCell<ComponentParts<C>>>,
13 pub(super) notifier: crate::Sender<()>,
14 pub(super) shutdown_on_drop: ShutdownOnDrop,
15}
16
17impl<C: Component> StateWatcher<C> {
18 #[must_use]
20 pub fn get(&self) -> Ref<'_, ComponentParts<C>> {
21 self.state.borrow()
22 }
23
24 #[must_use]
26 pub fn get_mut(&self) -> RefMut<'_, ComponentParts<C>> {
27 self.notifier.send(()).unwrap();
28 self.state.borrow_mut()
29 }
30
31 pub(super) fn detach_runtime(&mut self) {
32 self.shutdown_on_drop.deactivate()
33 }
34}
35
36impl<C> Debug for StateWatcher<C>
37where
38 C: Component + Debug,
39 C::Widgets: Debug,
40{
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 f.debug_struct("StateWatcher")
43 .field("state", &self.state)
44 .field("notifier", &self.notifier)
45 .field("shutdown_on_drop", &self.shutdown_on_drop)
46 .finish()
47 }
48}