tauri_store_utils/
task.rs

1use parking_lot::Mutex;
2use tokio::task::AbortHandle;
3
4/// A dyn compatible trait intended to be used with types
5/// like [`Debounce`](crate::Debounce) and [`Throttle`](crate::Throttle).
6pub trait RemoteCallable<T> {
7  /// Call the function with the provided context.
8  fn call(&self, ctx: &T);
9  /// Abort any pending calls.
10  fn abort(&self);
11}
12
13#[derive(Default)]
14pub(crate) struct OptionalAbortHandle {
15  inner: Mutex<Option<AbortHandle>>,
16}
17
18impl OptionalAbortHandle {
19  pub(crate) fn abort(&self) {
20    let mut lock = self.inner.lock();
21    if let Some(handle) = lock.take() {
22      drop(lock);
23      handle.abort();
24    }
25  }
26
27  pub(crate) fn replace(&self, handle: AbortHandle) {
28    let mut lock = self.inner.lock();
29    if let Some(old) = lock.replace(handle) {
30      old.abort();
31    }
32  }
33}