1use crate::collection::{DefaultMarker, Handle, StoreCollectionBuilder};
2use crate::command;
3use crate::error::BoxResult;
4use crate::manager::ManagerExt;
5use serde::de::DeserializeOwned;
6use tauri::plugin::{PluginApi, TauriPlugin};
7use tauri::{AppHandle, RunEvent, Runtime};
8
9pub use crate::collection::StoreCollectionBuilder as Builder;
10
11#[cfg(target_os = "ios")]
12tauri::ios_plugin_binding!(init_plugin_tauri_store);
13
14pub fn init<R: Runtime>() -> TauriPlugin<R> {
16 build(Builder::<R, DefaultMarker>::default())
17}
18
19pub(crate) fn build<R>(builder: Builder<R, DefaultMarker>) -> TauriPlugin<R>
20where
21 R: Runtime,
22{
23 tauri::plugin::Builder::new("tauri-store")
24 .on_event(on_event)
25 .setup(|app, api| setup(app, api, builder))
26 .invoke_handler(tauri::generate_handler![
27 command::allow_save,
28 command::allow_sync,
29 command::clear_autosave,
30 command::deny_save,
31 command::deny_sync,
32 command::destroy,
33 command::get_default_save_strategy,
34 command::get_save_strategy,
35 command::get_store_collection_path,
36 command::get_store_ids,
37 command::get_store_path,
38 command::get_store_state,
39 command::load,
40 command::patch,
41 command::save,
42 command::save_all,
43 command::save_all_now,
44 command::save_now,
45 command::save_some,
46 command::save_some_now,
47 command::set_autosave,
48 command::set_save_strategy,
49 command::set_store_options,
50 command::unload
51 ])
52 .build()
53}
54
55#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
56pub(super) fn setup<R, D>(
57 app: &AppHandle<R>,
58 _api: PluginApi<R, D>,
59 builder: StoreCollectionBuilder<R, DefaultMarker>,
60) -> BoxResult<()>
61where
62 R: Runtime,
63 D: DeserializeOwned,
64{
65 let handle = Handle::new(app.clone());
66 builder.build(handle, env!("CARGO_PKG_NAME"))?;
67 Ok(())
68}
69
70#[cfg(any(target_os = "android", target_os = "ios"))]
71pub(super) fn setup<R, D>(
72 _app: &AppHandle<R>,
73 api: PluginApi<R, D>,
74 builder: StoreCollectionBuilder<R, DefaultMarker>,
75) -> BoxResult<()>
76where
77 R: Runtime,
78 D: DeserializeOwned,
79{
80 #[cfg(target_os = "android")]
81 let handle = api.register_android_plugin("", "TauriStorePlugin")?;
82 #[cfg(target_os = "ios")]
83 let handle = api.register_ios_plugin(init_plugin_tauri_store)?;
84
85 builder.build(Handle::new(handle), env!("CARGO_PKG_NAME"))?;
86
87 Ok(())
88}
89
90fn on_event<R>(app: &AppHandle<R>, event: &RunEvent)
91where
92 R: Runtime,
93{
94 if let RunEvent::Exit = event {
95 let _ = app.store_collection().on_exit();
96 }
97}