tauri_store/store/
watch.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::error::Result;
use std::fmt;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use tauri::{AppHandle, Runtime};

#[cfg(feature = "unstable-async")]
use futures::future::BoxFuture;

static WATCHER_ID: AtomicU32 = AtomicU32::new(0);

#[cfg(not(feature = "unstable-async"))]
pub type WatcherResult = Result<()>;
#[cfg(feature = "unstable-async")]
pub type WatcherResult = BoxFuture<'static, Result<()>>;

type WatcherFn<R> = dyn Fn(AppHandle<R>) -> WatcherResult + Send + Sync;

pub(crate) struct Watcher<R: Runtime> {
  pub(crate) id: u32,
  inner: Arc<WatcherFn<R>>,
}

impl<R: Runtime> Watcher<R> {
  pub fn new<F>(f: F) -> Self
  where
    F: Fn(AppHandle<R>) -> WatcherResult + Send + Sync + 'static,
  {
    Self {
      id: WATCHER_ID.fetch_add(1, Ordering::Relaxed),
      inner: Arc::new(f),
    }
  }

  #[cfg(not(feature = "unstable-async"))]
  pub fn call(&self, app: AppHandle<R>) {
    let _ = (self.inner)(app);
  }

  #[cfg(feature = "unstable-async")]
  pub async fn call(&self, app: AppHandle<R>) {
    let _ = (self.inner)(app).await;
  }
}

impl<R: Runtime> Clone for Watcher<R> {
  fn clone(&self) -> Self {
    Self {
      id: self.id,
      inner: Arc::clone(&self.inner),
    }
  }
}

impl<R: Runtime> fmt::Debug for Watcher<R> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("Listener")
      .field("id", &self.id)
      .finish_non_exhaustive()
  }
}