tauri_store/
manager.rs

1use crate::collection::{StoreCollection, RESOURCE_ID};
2use crate::error::Result;
3use crate::store::Store;
4use std::sync::Arc;
5use tauri::{AppHandle, Manager, Runtime, WebviewWindow, Window};
6
7/// Extension for the [`Manager`] trait providing access to the store collection.
8///
9/// [`Manager`]: https://docs.rs/tauri/latest/tauri/trait.Manager.html
10pub trait ManagerExt<R: Runtime>: Manager<R> {
11  /// Returns a handle to the store collection.
12  ///
13  /// # Panics
14  ///
15  /// Panics if the [store collection](crate::collection::StoreCollection) is not in the [resources table].
16  ///
17  /// This likely indicates that the method was called before the plugin was properly initialized.
18  ///
19  /// [resources table]: https://docs.rs/tauri/latest/tauri/struct.ResourceTable.html
20  fn store_collection(&self) -> Arc<StoreCollection<R>> {
21    let rid = RESOURCE_ID
22      .get()
23      .expect("missing store collection resource id");
24
25    self
26      .resources_table()
27      .get::<StoreCollection<R>>(*rid)
28      .expect("store collection is not in the resources table")
29  }
30
31  /// Calls a closure with a mutable reference to the store with the given id.
32  fn with_store<F, T>(&self, id: impl AsRef<str>, f: F) -> Result<T>
33  where
34    F: FnOnce(&mut Store<R>) -> T,
35  {
36    self.store_collection().with_store(id, f)
37  }
38}
39
40impl<R: Runtime> ManagerExt<R> for AppHandle<R> {}
41impl<R: Runtime> ManagerExt<R> for WebviewWindow<R> {}
42impl<R: Runtime> ManagerExt<R> for Window<R> {}