tauri_plugin_svelte/
lib.rs

1// This file was autogenerated and should not be edited manually.
2// Check the `codegen` command in the `tauri-store-cli` crate.
3
4#![forbid(unsafe_code)]
5#![cfg_attr(docsrs, feature(doc_auto_cfg))]
6#![doc = include_str!("../README.md")]
7#![doc(html_favicon_url = "https://tb.dev.br/tauri-store/favicon.ico")]
8
9mod command;
10mod manager;
11mod svelte;
12
13use std::collections::HashSet;
14use std::path::PathBuf;
15use std::time::Duration;
16use tauri::plugin::TauriPlugin;
17use tauri::{AppHandle, Manager, RunEvent, Runtime};
18use tauri_store::CollectionBuilder;
19
20#[cfg(feature = "unstable-migration")]
21use tauri_store::Migrator;
22
23pub use manager::ManagerExt;
24pub use svelte::Svelte;
25pub use tauri_store::prelude::*;
26
27#[cfg(feature = "unstable-migration")]
28pub use tauri_store::{Migration, MigrationContext};
29
30/// Builder for the Svelte plugin.
31#[derive(CollectionBuilder)]
32pub struct Builder<R: Runtime> {
33  path: Option<PathBuf>,
34  default_save_strategy: SaveStrategy,
35  autosave: Option<Duration>,
36  on_load: Option<Box<OnLoadFn<R>>>,
37  pretty: bool,
38  save_denylist: HashSet<StoreId>,
39  sync_denylist: HashSet<StoreId>,
40
41  #[cfg(feature = "unstable-migration")]
42  migrator: Migrator,
43}
44
45impl<R: Runtime> Builder<R> {
46  /// Builds the Svelte plugin.
47  pub fn build(self) -> TauriPlugin<R> {
48    tauri::plugin::Builder::new("svelte")
49      .setup(|app, _| setup(app, self))
50      .on_event(on_event)
51      .invoke_handler(tauri::generate_handler![
52        command::clear_autosave,
53        command::get_default_save_strategy,
54        command::get_store_collection_path,
55        command::get_save_strategy,
56        command::get_store_ids,
57        command::get_store_path,
58        command::get_store_state,
59        command::load,
60        command::patch,
61        command::save,
62        command::save_all,
63        command::save_all_now,
64        command::save_now,
65        command::save_some,
66        command::save_some_now,
67        command::set_autosave,
68        command::set_save_strategy,
69        command::set_store_collection_path,
70        command::set_store_options,
71        command::unload
72      ])
73      .build()
74  }
75}
76
77fn setup<R>(app: &AppHandle<R>, builder: Builder<R>) -> BoxResult<()>
78where
79  R: Runtime,
80{
81  let collection = builder.build_collection(app)?;
82  app.manage(Svelte(collection));
83
84  Ok(())
85}
86
87fn on_event<R>(app: &AppHandle<R>, event: &RunEvent)
88where
89  R: Runtime,
90{
91  if let RunEvent::Exit = event {
92    let _ = app.svelte().0.on_exit();
93  }
94}
95
96/// Initializes the plugin with the default settings.
97pub fn init<R: Runtime>() -> TauriPlugin<R> {
98  Builder::default().build()
99}