tauri_store/
collection.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::error::Result;
use crate::event::{emit_all, STORE_UNLOADED_EVENT};
use crate::io_err;
use crate::store::{Store, StoreState};
use serde::de::DeserializeOwned;
use serde_json::json;
use serde_json::Value as Json;
use std::fmt;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex as StdMutex, OnceLock};
use tauri::{AppHandle, Manager, Resource, ResourceId, Runtime};

#[cfg(feature = "unstable-async")]
use {
  crate::manager::ManagerExt,
  crate::{BoxFuture, FutureExt},
  std::time::Duration,
  tokio::sync::Mutex as TokioMutex,
  tokio::task::AbortHandle,
};

#[cfg(feature = "ahash")]
use ahash::{HashMap, HashMapExt, HashSet};
#[cfg(not(feature = "ahash"))]
use std::collections::{HashMap, HashSet};

pub(crate) static RESOURCE_ID: OnceLock<ResourceId> = OnceLock::new();

pub struct StoreCollection<R: Runtime> {
  pub(crate) app: AppHandle<R>,
  pub(crate) path: PathBuf,
  pub(crate) sync_denylist: StdMutex<HashSet<String>>,

  #[cfg(not(feature = "unstable-async"))]
  pub(crate) stores: StdMutex<HashMap<String, Store<R>>>,
  #[cfg(feature = "unstable-async")]
  pub(crate) stores: TokioMutex<HashMap<String, Store<R>>>,

  #[cfg(feature = "unstable-async")]
  pub(crate) autosave: StdMutex<Option<AbortHandle>>,
}

macro_rules! get_store {
  ($stores:expr, $id:expr) => {{
    let id = $id.as_ref();
    match $stores.get(id) {
      Some(store) => Ok(store),
      None => $crate::io_err!(NotFound, "store not found: {id}"),
    }
  }};
}

impl<R: Runtime> StoreCollection<R> {
  pub fn builder() -> StoreCollectionBuilder {
    StoreCollectionBuilder::new()
  }

  /// Directory where the stores are saved.
  pub fn path(&self) -> &Path {
    &self.path
  }

  /// Calls a closure with a mutable reference to the store with the given id.
  #[cfg(not(feature = "unstable-async"))]
  pub fn with_store<F, T>(&self, id: impl AsRef<str>, f: F) -> Result<T>
  where
    F: FnOnce(&mut Store<R>) -> Result<T>,
  {
    let id = id.as_ref();
    let mut stores = self.stores.lock().unwrap();
    if !stores.contains_key(id) {
      let app = self.app.clone();
      let store = Store::load(app, id)?;
      stores.insert(id.to_owned(), store);
    }

    f(stores.get_mut(id).expect("store should exist"))
  }

  /// Calls a closure with a mutable reference to the store with the given id.
  #[cfg(feature = "unstable-async")]
  pub fn with_store<F, T>(&self, id: impl AsRef<str>, f: F) -> BoxFuture<Result<T>>
  where
    F: FnOnce(&mut Store<R>) -> BoxFuture<Result<T>> + Send + 'static,
    T: Send + 'static,
  {
    let id = id.as_ref().to_owned();
    let app = self.app.clone();
    Box::pin(async move {
      let mut stores = self.stores.lock().await;
      if !stores.contains_key(&id) {
        let store = Store::load(app, &id).unwrap();
        stores.insert(id.clone(), store);
      }

      f(stores.get_mut(&id).expect("store should exist")).await
    })
  }

  /// Saves a store to the disk.
  #[cfg(not(feature = "unstable-async"))]
  pub fn save(&self, id: impl AsRef<str>) -> Result<()> {
    let stores = self.stores.lock().unwrap();
    get_store!(stores, id)?.save()
  }

  /// Saves a store to the disk.
  #[cfg(feature = "unstable-async")]
  pub async fn save(&self, id: impl AsRef<str>) -> Result<()> {
    let stores = self.stores.lock().await;
    get_store!(stores, id)?.save().await
  }

  /// Saves some stores to the disk.
  #[cfg(not(feature = "unstable-async"))]
  pub fn save_some(&self, ids: &[impl AsRef<str>]) -> Result<()> {
    let stores = self.stores.lock().unwrap();
    for id in ids {
      get_store!(stores, id)?.save()?;
    }

    Ok(())
  }

  /// Saves some stores to the disk.
  #[cfg(feature = "unstable-async")]
  pub async fn save_some(&self, ids: &[impl AsRef<str>]) -> Result<()> {
    let stores = self.stores.lock().await;
    for id in ids {
      get_store!(stores, id)?.save().await?;
    }

    Ok(())
  }

  /// Saves all the stores to the disk.
  #[cfg(not(feature = "unstable-async"))]
  pub fn save_all(&self) -> Result<()> {
    let stores = self.stores.lock().unwrap();
    stores.values().try_for_each(Store::save)
  }

  /// Saves all the stores to the disk.
  #[cfg(feature = "unstable-async")]
  pub async fn save_all(&self) -> Result<()> {
    let stores = self.stores.lock().await;
    for store in stores.values() {
      store.save().await?;
    }

    Ok(())
  }

  /// Lists all the store ids.
  #[cfg(not(feature = "unstable-async"))]
  pub fn ids(&self) -> Vec<String> {
    let stores = self.stores.lock().unwrap();
    stores.keys().cloned().collect()
  }

  /// Lists all the store ids.
  #[cfg(feature = "unstable-async")]
  pub async fn ids(&self) -> Vec<String> {
    let stores = self.stores.lock().await;
    stores.keys().cloned().collect()
  }

  /// Gets a clone of the store state if it exists.
  ///
  /// **WARNING:** Changes to the returned state will not be reflected in the store.
  #[cfg(not(feature = "unstable-async"))]
  pub fn store_state(&self, store_id: impl AsRef<str>) -> Option<StoreState> {
    let stores = self.stores.lock().unwrap();
    stores.get(store_id.as_ref()).map(Store::state)
  }

  /// Gets a clone of the store state if it exists.
  ///
  /// **WARNING:** Changes to the returned state will not be reflected in the store.
  #[cfg(feature = "unstable-async")]
  pub async fn store_state(&self, store_id: impl AsRef<str>) -> Option<StoreState> {
    let stores = self.stores.lock().await;
    stores.get(store_id.as_ref()).map(Store::state)
  }

  /// Gets the store state if it exists, then tries to deserialize it as an instance of type `T`.
  #[cfg(not(feature = "unstable-async"))]
  pub fn try_store_state<T>(&self, store_id: impl AsRef<str>) -> Result<T>
  where
    T: DeserializeOwned,
  {
    let stores = self.stores.lock().unwrap();
    let store = get_store!(stores, store_id)?;

    let state = json!(store.state());
    serde_json::from_value(state).map_err(Into::into)
  }

  /// Gets the store state if it exists, then tries to deserialize it as an instance of type `T`.
  #[cfg(feature = "unstable-async")]
  pub async fn try_store_state<T>(&self, store_id: impl AsRef<str>) -> Result<T>
  where
    T: DeserializeOwned,
  {
    let stores = self.stores.lock().await;
    let store = get_store!(stores, store_id)?;

    let state = json!(store.state());
    serde_json::from_value(state).map_err(Into::into)
  }

  /// Gets a value from a store.
  #[cfg(not(feature = "unstable-async"))]
  pub fn get(&self, store_id: impl AsRef<str>, key: impl AsRef<str>) -> Option<Json> {
    let stores = self.stores.lock().unwrap();
    stores
      .get(store_id.as_ref())
      .and_then(|store| store.get_owned(key))
  }

  /// Gets a value from a store.
  #[cfg(feature = "unstable-async")]
  pub async fn get(&self, store_id: impl AsRef<str>, key: impl AsRef<str>) -> Option<Json> {
    let stores = self.stores.lock().await;
    stores
      .get(store_id.as_ref())
      .and_then(|store| store.get_owned(key))
  }

  #[cfg(not(feature = "unstable-async"))]
  /// Gets a value from a store and tries to interpret it as an instance of type `T`.
  pub fn try_get<T>(&self, store_id: impl AsRef<str>, key: impl AsRef<str>) -> Result<T>
  where
    T: DeserializeOwned,
  {
    let key = key.as_ref();
    let Some(value) = self.get(store_id, key) else {
      return io_err!(NotFound, "key not found: {key}");
    };

    serde_json::from_value(value).map_err(Into::into)
  }

  #[cfg(feature = "unstable-async")]
  /// Gets a value from a store and tries to interpret it as an instance of type `T`.
  pub async fn try_get<T>(&self, store_id: impl AsRef<str>, key: impl AsRef<str>) -> Result<T>
  where
    T: DeserializeOwned,
  {
    let key = key.as_ref();
    let Some(value) = self.get(store_id, key).await else {
      return io_err!(NotFound, "key not found: {key}");
    };

    serde_json::from_value(value).map_err(Into::into)
  }

  /// Sets a key-value pair in a store.
  #[cfg(not(feature = "unstable-async"))]
  pub fn set(&self, store_id: impl AsRef<str>, key: impl AsRef<str>, value: Json) -> Result<()> {
    self.with_store(store_id, |store| store.set(key, value))
  }

  /// Sets a key-value pair in a store.
  #[cfg(feature = "unstable-async")]
  pub async fn set(
    &self,
    store_id: impl AsRef<str>,
    key: impl AsRef<str>,
    value: Json,
  ) -> Result<()> {
    let key = key.as_ref().to_owned();
    self
      .with_store(store_id, |store| async { store.set(key, value) }.boxed())
      .await
  }

  /// Patches a store state.
  #[cfg(not(feature = "unstable-async"))]
  pub fn patch(&self, store_id: impl AsRef<str>, state: StoreState) -> Result<()> {
    self.with_store(store_id, |store| store.patch(state))
  }

  /// Patches a store state.
  #[cfg(feature = "unstable-async")]
  pub async fn patch(&self, store_id: impl AsRef<str>, state: StoreState) -> Result<()> {
    self
      .with_store(store_id, |store| async { store.patch(state) }.boxed())
      .await
  }

  /// Remove a store from the sync denylist.
  pub fn enable_sync(&self, store_id: impl AsRef<str>) {
    self
      .sync_denylist
      .lock()
      .expect("sync denylist mutex is poisoned")
      .remove(store_id.as_ref());
  }

  /// Add a store to the sync denylist.
  pub fn disable_sync(&self, store_id: impl AsRef<str>) {
    self
      .sync_denylist
      .lock()
      .expect("sync denylist mutex is poisoned")
      .insert(store_id.as_ref().to_owned());
  }

  /// Saves the stores periodically.
  #[cfg(feature = "unstable-async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "unstable-async")))]
  pub fn set_autosave(&self, duration: Duration) {
    use tauri::async_runtime::{self, RuntimeHandle};
    use tokio::time::{self, MissedTickBehavior};

    self.clear_autosave();

    let app = self.app.clone();
    let RuntimeHandle::Tokio(runtime) = async_runtime::handle();
    let task = runtime.spawn(async move {
      let mut interval = time::interval(duration);
      interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
      loop {
        interval.tick().await;
        let _ = app.store_collection().save_all().await;
      }
    });

    let mut guard = self.autosave.lock().unwrap();
    *guard = Some(task.abort_handle());
  }

  /// Stops the autosave.
  #[cfg(feature = "unstable-async")]
  #[cfg_attr(docsrs, doc(cfg(feature = "unstable-async")))]
  pub fn clear_autosave(&self) {
    let mut guard = self.autosave.lock().unwrap();
    if let Some(autosave) = guard.take() {
      drop(guard);
      autosave.abort();
    }
  }

  #[cfg(not(feature = "unstable-async"))]
  pub fn unload_store(&self, id: &str) -> Result<()> {
    let mut stores = self.stores.lock().unwrap();
    if let Some(store) = stores.remove(id) {
      drop(stores);
      store.save()?;
      emit_all(&self.app, STORE_UNLOADED_EVENT, id)?;
    }

    Ok(())
  }

  #[cfg(feature = "unstable-async")]
  pub async fn unload_store(&self, id: &str) -> Result<()> {
    let mut stores = self.stores.lock().await;
    if let Some(store) = stores.remove(id) {
      drop(stores);
      store.save().await?;
      emit_all(&self.app, STORE_UNLOADED_EVENT, id)?;
    }

    Ok(())
  }
}

impl<R: Runtime> Resource for StoreCollection<R> {}

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

#[cfg(feature = "unstable-async")]
impl<R: Runtime> Drop for StoreCollection<R> {
  fn drop(&mut self) {
    self.clear_autosave();
  }
}

#[derive(Debug, Default)]
pub struct StoreCollectionBuilder {
  path: Option<PathBuf>,
  sync_denylist: Option<HashSet<String>>,
}

impl StoreCollectionBuilder {
  pub fn new() -> Self {
    Self { path: None, sync_denylist: None }
  }

  #[must_use]
  pub fn path(mut self, path: impl AsRef<Path>) -> Self {
    self.path = Some(path.as_ref().to_path_buf());
    self
  }

  #[must_use]
  pub fn sync_denylist(mut self, sync_denylist: HashSet<String>) -> Self {
    self.sync_denylist = Some(sync_denylist);
    self
  }

  pub fn build<R: Runtime>(mut self, app: &AppHandle<R>) -> Arc<StoreCollection<R>> {
    let path = self.path.take().unwrap_or_else(|| {
      app
        .path()
        .app_data_dir()
        .expect("failed to resolve app data dir")
        .join("tauri-store")
    });

    let sync_denylist = self.sync_denylist.take().unwrap_or_default();

    let collection = Arc::new(StoreCollection::<R> {
      app: app.clone(),
      path,
      sync_denylist: StdMutex::new(sync_denylist),

      #[cfg(not(feature = "unstable-async"))]
      stores: StdMutex::new(HashMap::new()),
      #[cfg(feature = "unstable-async")]
      stores: TokioMutex::new(HashMap::new()),

      #[cfg(feature = "unstable-async")]
      autosave: StdMutex::new(None),
    });

    let rid = app
      .resources_table()
      .add_arc(Arc::clone(&collection));

    let _ = RESOURCE_ID.set(rid);

    collection
  }
}