Enable auto save by default

pull/1860/head
Tony 8 months ago
parent 9811756e6c
commit 35b439f70f
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -17,9 +17,9 @@ interface ChangePayload<T> {
*/ */
export type StoreOptions = { export type StoreOptions = {
/** /**
* Auto save on modification with debounce duration in milliseconds * Auto save on modification with debounce duration in milliseconds, it's 100ms by default, pass in `false` to disable it
*/ */
autoSave?: boolean autoSave?: boolean | number
} }
/** /**

@ -13,7 +13,7 @@
pub use error::{Error, Result}; pub use error::{Error, Result};
use log::warn; use log::warn;
use serde::Serialize; use serde::{Deserialize, Serialize};
pub use serde_json::Value as JsonValue; pub use serde_json::Value as JsonValue;
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -42,15 +42,30 @@ pub struct StoreCollection<R: Runtime> {
stores: Mutex<HashMap<PathBuf, (Weak<Store<R>>, Option<ResourceId>)>>, stores: Mutex<HashMap<PathBuf, (Weak<Store<R>>, Option<ResourceId>)>>,
} }
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum AutoSave {
DebounceDuration(u64),
Bool(bool),
}
#[tauri::command] #[tauri::command]
async fn create_store<R: Runtime>( async fn create_store<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
path: PathBuf, path: PathBuf,
auto_save: Option<u64>, auto_save: Option<AutoSave>,
) -> Result<ResourceId> { ) -> Result<ResourceId> {
let mut builder = app.store_builder(path.clone()); let mut builder = app.store_builder(path.clone());
if let Some(auto_save) = auto_save { if let Some(auto_save) = auto_save {
builder = builder.auto_save(Duration::from_millis(auto_save)); match auto_save {
AutoSave::DebounceDuration(duration) => {
builder = builder.auto_save(Duration::from_millis(duration));
}
AutoSave::Bool(false) => {
builder = builder.disable_auto_save();
}
_ => {}
}
} }
let store = builder.build()?; let store = builder.build()?;
let rid = app.resources_table().add_arc(store); let rid = app.resources_table().add_arc(store);

@ -66,7 +66,7 @@ impl<R: Runtime> StoreBuilder<R> {
defaults: None, defaults: None,
serialize: default_serialize, serialize: default_serialize,
deserialize: default_deserialize, deserialize: default_deserialize,
auto_save: None, auto_save: Some(Duration::from_millis(100)),
} }
} }
@ -167,6 +167,24 @@ impl<R: Runtime> StoreBuilder<R> {
self self
} }
/// Auto save on modified with a debounce duration
///
/// # Examples
/// ```
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .auto_save(std::time::Duration::from_millis(100))
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn disable_auto_save(mut self) -> Self {
self.auto_save = None;
self
}
/// Builds the [`Store`]. /// Builds the [`Store`].
/// ///
/// # Examples /// # Examples

Loading…
Cancel
Save