tauri_store_utils/time/
mod.rs

1mod debounce;
2mod throttle;
3
4use crate::manager::ManagerExt;
5pub use debounce::Debounce;
6use std::future::Future;
7use std::time::Duration;
8use tauri::{AppHandle, Runtime};
9pub use throttle::Throttle;
10use tokio::task::AbortHandle;
11use tokio::time::{self, MissedTickBehavior};
12
13/// Calls the given function at regular intervals.
14pub fn set_interval<R, F, Fut>(app: &AppHandle<R>, duration: Duration, f: F) -> AbortHandle
15where
16  R: Runtime,
17  F: Fn(AppHandle<R>) -> Fut + Send + 'static,
18  Fut: Future<Output = ()> + Send + 'static,
19{
20  app.spawn(move |app| {
21    async move {
22      let mut interval = time::interval(duration);
23      interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
24
25      // The first tick completes immediately.
26      interval.tick().await;
27
28      loop {
29        interval.tick().await;
30        f(app.clone()).await;
31      }
32    }
33  })
34}