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