tauri_store/manager.rs
1use crate::collection::StoreCollection;
2use crate::error::Result;
3use crate::store::Store;
4use crate::{CollectionMarker, DefaultMarker};
5use tauri::{Manager, Runtime, State};
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 default store collection.
12 ///
13 /// # Panics
14 ///
15 /// Panics if the [store collection] is not yet being managed by Tauri.
16 ///
17 /// This likely indicates it was called before the plugin was properly initialized.
18 ///
19 /// [store collection]: https://docs.rs/tauri-store/latest/tauri_store/struct.StoreCollection.html
20 fn store_collection(&self) -> State<'_, StoreCollection<R, DefaultMarker>> {
21 self.store_collection_with_marker::<DefaultMarker>()
22 }
23
24 /// Returns a handle to the store collection for the specified marker.
25 ///
26 /// # Panics
27 ///
28 /// Panics if the [store collection] is not yet being managed by Tauri.
29 ///
30 /// This likely indicates it was called before the plugin was properly initialized.
31 ///
32 /// [store collection]: https://docs.rs/tauri-store/latest/tauri_store/struct.StoreCollection.html
33 fn store_collection_with_marker<C>(&self) -> State<'_, StoreCollection<R, C>>
34 where
35 C: CollectionMarker,
36 {
37 self.app_handle().state::<StoreCollection<R, C>>()
38 }
39
40 /// Calls a closure with a mutable reference to the store with the given id.
41 fn with_store<F, T>(&self, id: impl AsRef<str>, f: F) -> Result<T>
42 where
43 F: FnOnce(&mut Store<R, DefaultMarker>) -> T,
44 {
45 self.store_collection().with_store(id, f)
46 }
47}
48
49impl<R: Runtime, T: Manager<R>> ManagerExt<R> for T {}