freya_sdk/tokio/watch.rs
1use freya_core::prelude::{
2 ReactiveContext,
3 spawn,
4 use_hook,
5};
6use tokio::sync::watch;
7
8/// Subscribe this component to the given [watch::Receiver].
9pub fn use_track_watcher<T: 'static>(watcher: &watch::Receiver<T>) {
10 use_hook(|| {
11 let mut watcher = watcher.clone();
12
13 // No need to make this component rerun if it just got created
14 watcher.mark_unchanged();
15
16 let rc = ReactiveContext::current();
17
18 spawn(async move {
19 while watcher.changed().await.is_ok() {
20 rc.notify();
21 }
22 });
23 })
24}