tauri_plugin_svelte/
lib.rs1#![forbid(unsafe_code)]
5#![cfg_attr(docsrs, feature(doc_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
20pub use manager::ManagerExt;
21pub use svelte::Svelte;
22pub use tauri_store::prelude::*;
23
24#[derive(CollectionBuilder)]
26pub struct Builder<R: Runtime> {
27 path: Option<PathBuf>,
28 default_save_strategy: SaveStrategy,
29 autosave: Option<Duration>,
30 on_load: Option<Box<OnLoadFn<R>>>,
31 pretty: bool,
32 save_denylist: HashSet<StoreId>,
33 sync_denylist: HashSet<StoreId>,
34}
35
36impl<R: Runtime> Builder<R> {
37 pub fn build(self) -> TauriPlugin<R> {
39 tauri::plugin::Builder::new("svelte")
40 .setup(|app, _| setup(app, self))
41 .on_event(on_event)
42 .invoke_handler(tauri::generate_handler![
43 command::clear_autosave,
44 command::get_default_save_strategy,
45 command::get_store_collection_path,
46 command::get_save_strategy,
47 command::get_store_ids,
48 command::get_store_path,
49 command::get_store_state,
50 command::load,
51 command::patch,
52 command::save,
53 command::save_all,
54 command::save_all_now,
55 command::save_now,
56 command::save_some,
57 command::save_some_now,
58 command::set_autosave,
59 command::set_save_strategy,
60 command::set_store_collection_path,
61 command::set_store_options,
62 command::unload
63 ])
64 .build()
65 }
66}
67
68fn setup<R>(app: &AppHandle<R>, builder: Builder<R>) -> BoxResult<()>
69where
70 R: Runtime,
71{
72 let collection = builder.build_collection(app)?;
73 app.manage(Svelte(collection));
74
75 Ok(())
76}
77
78fn on_event<R>(app: &AppHandle<R>, event: &RunEvent)
79where
80 R: Runtime,
81{
82 if let RunEvent::Exit = event {
83 let _ = app.svelte().0.on_exit();
84 }
85}
86
87pub fn init<R: Runtime>() -> TauriPlugin<R> {
89 Builder::default().build()
90}