mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-23 11:36:13 +02:00
Enable auto save by default
This commit is contained in:
@@ -17,9 +17,9 @@ interface ChangePayload<T> {
|
||||
*/
|
||||
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};
|
||||
use log::warn;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub use serde_json::Value as JsonValue;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@@ -42,15 +42,30 @@ pub struct StoreCollection<R: Runtime> {
|
||||
stores: Mutex<HashMap<PathBuf, (Weak<Store<R>>, Option<ResourceId>)>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
enum AutoSave {
|
||||
DebounceDuration(u64),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn create_store<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
path: PathBuf,
|
||||
auto_save: Option<u64>,
|
||||
auto_save: Option<AutoSave>,
|
||||
) -> Result<ResourceId> {
|
||||
let mut builder = app.store_builder(path.clone());
|
||||
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 rid = app.resources_table().add_arc(store);
|
||||
|
||||
@@ -66,7 +66,7 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
defaults: None,
|
||||
serialize: default_serialize,
|
||||
deserialize: default_deserialize,
|
||||
auto_save: None,
|
||||
auto_save: Some(Duration::from_millis(100)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +167,24 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
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`].
|
||||
///
|
||||
/// # Examples
|
||||
|
||||
Reference in New Issue
Block a user