tauri_store/
plugin.rs

1use crate::command;
2use crate::error::BoxResult;
3use crate::manager::ManagerExt;
4use tauri::plugin::TauriPlugin;
5use tauri::{AppHandle, RunEvent, Runtime};
6
7pub use crate::collection::StoreCollectionBuilder as Builder;
8
9/// Initializes the store plugin.
10pub fn init<R: Runtime>() -> TauriPlugin<R> {
11  build(Builder::default())
12}
13
14pub(crate) fn build<R: Runtime>(builder: Builder<R>) -> TauriPlugin<R> {
15  tauri::plugin::Builder::new("tauri-store")
16    .on_event(on_event)
17    .setup(|app, _| setup(app, builder))
18    .invoke_handler(tauri::generate_handler![
19      command::clear_autosave,
20      command::get_default_save_strategy,
21      command::get_save_strategy,
22      command::get_store_collection_path,
23      command::get_store_ids,
24      command::get_store_path,
25      command::get_store_state,
26      command::load,
27      command::patch,
28      command::save,
29      command::save_all,
30      command::save_all_now,
31      command::save_now,
32      command::save_some,
33      command::save_some_now,
34      command::set_autosave,
35      command::set_save_strategy,
36      command::set_store_collection_path,
37      command::set_store_options,
38      command::unload
39    ])
40    .build()
41}
42
43fn setup<R>(app: &AppHandle<R>, builder: Builder<R>) -> BoxResult<()>
44where
45  R: Runtime,
46{
47  builder.build(app, env!("CARGO_PKG_NAME"))?;
48  Ok(())
49}
50
51fn on_event<R>(app: &AppHandle<R>, event: &RunEvent)
52where
53  R: Runtime,
54{
55  if let RunEvent::Exit = event {
56    let _ = app.store_collection().on_exit();
57  }
58}