feat(store)!: fully rework and add auto save (#1550)

* Add auto save to store plugin

* Put jsdoc at constructor instead of class level

* Clippy

* Use enum instead of bool

* Some(AutoSaveMessage::Cancel) | None

* from_millis

* u64

* Add change file

* Rename to emit_on_change

* should use Duration in `with_store`

* Add breaking change notice to change file

* Emit change event for inserts by reset

* Update readme example

* Update example

* Remove extra line

* Make description clear it only works with managed

* Fix links in docstring

* Fix doc string closing

* get_mut

* Proof of concept

* fmt

* Load store on create

* cargo fmt

* Fix merge conflits

* Format

* small cleanup

* update docs, use `impl Into<JsonValue>`

* fix doctests, further simplification of api

* add store options

---------

Co-authored-by: Tillmann <28728469+tweidinger@users.noreply.github.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
pull/1859/head
Tony 8 months ago committed by GitHub
parent 44273b9889
commit f12d35609a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"store-js": patch
---
**Breaking change**: Removed the `Store` constructor and added the `createStore` API.

@ -0,0 +1,7 @@
---
"store": patch
---
Add a setting `auto_save` to enable a store to debounce save on modification (on calls like set, clear, delete, reset)
**Breaking change**: Removed the `with_store` API and added `StoreExt::store_builder`.

1
Cargo.lock generated

@ -6990,6 +6990,7 @@ dependencies = [
"tauri", "tauri",
"tauri-plugin", "tauri-plugin",
"thiserror", "thiserror",
"tokio",
] ]
[[package]] [[package]]

@ -30,6 +30,10 @@ tauri = { workspace = true }
log = { workspace = true } log = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }
dunce = { workspace = true } dunce = { workspace = true }
tokio = { version = "1", features = ["sync", "time", "macros"] }
[target.'cfg(target_os = "ios")'.dependencies] [target.'cfg(target_os = "ios")'.dependencies]
tauri = { workspace = true, features = ["wry"] } tauri = { workspace = true, features = ["wry"] }
[dev-dependencies]
tauri = { workspace = true, features = ["wry"] }

@ -149,12 +149,10 @@ As you may have noticed, the `Store` crated above isn't accessible to the fronte
```rust ```rust
use tauri::Wry; use tauri::Wry;
use tauri_plugin_store::with_store; use tauri_plugin_store::StoreExt;
let stores = app.state::<StoreCollection<Wry>>(); let store = app.store_builder("app_data.bin").build();
let path = PathBuf::from("app_data.bin"); store.insert("key", "value");
with_store(app_handle, stores, path, |store| store.insert("a".to_string(), json!("b")))
``` ```
## Contributing ## Contributing

@ -1 +1 @@
if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";function a(t,a=!1){return window.__TAURI_INTERNALS__.transformCallback(t,a)}async function e(t,a={},e){return window.__TAURI_INTERNALS__.invoke(t,a,e)}var n;async function r(t,n,r){const i={kind:"Any"};return e("plugin:event|listen",{event:t,target:i,handler:a(n)}).then((a=>async()=>async function(t,a){await e("plugin:event|unlisten",{event:t,eventId:a})}(t,a)))}"function"==typeof SuppressedError&&SuppressedError,function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"}(n||(n={}));return t.Store=class{constructor(t){this.path=t}async set(t,a){await e("plugin:store|set",{path:this.path,key:t,value:a})}async get(t){return await e("plugin:store|get",{path:this.path,key:t})}async has(t){return await e("plugin:store|has",{path:this.path,key:t})}async delete(t){return await e("plugin:store|delete",{path:this.path,key:t})}async clear(){await e("plugin:store|clear",{path:this.path})}async reset(){await e("plugin:store|reset",{path:this.path})}async keys(){return await e("plugin:store|keys",{path:this.path})}async values(){return await e("plugin:store|values",{path:this.path})}async entries(){return await e("plugin:store|entries",{path:this.path})}async length(){return await e("plugin:store|length",{path:this.path})}async load(){await e("plugin:store|load",{path:this.path})}async save(){await e("plugin:store|save",{path:this.path})}async onKeyChange(t,a){return await r("store://change",(e=>{e.payload.path===this.path&&e.payload.key===t&&a(e.payload.value)}))}async onChange(t){return await r("store://change",(a=>{a.payload.path===this.path&&t(a.payload.key,a.payload.value)}))}},t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})} if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(e){"use strict";var t,r;function a(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}async function i(e,t={},r){return window.__TAURI_INTERNALS__.invoke(e,t,r)}"function"==typeof SuppressedError&&SuppressedError;class n{get rid(){return function(e,t,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(e):a?a.value:t.get(e)}(this,t,"f")}constructor(e){t.set(this,void 0),function(e,t,r,a,i){if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");t.set(e,r)}(this,t,e)}async close(){return i("plugin:resources|close",{rid:this.rid})}}async function s(e,t,r){const n={kind:"Any"};return i("plugin:event|listen",{event:e,target:n,handler:a(t)}).then((t=>async()=>async function(e,t){await i("plugin:event|unlisten",{event:e,eventId:t})}(e,t)))}t=new WeakMap,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(r||(r={}));class o extends n{constructor(e,t){super(e),this.path=t}async set(e,t){await i("plugin:store|set",{rid:this.rid,key:e,value:t})}async get(e){return await i("plugin:store|get",{rid:this.rid,key:e})}async has(e){return await i("plugin:store|has",{rid:this.rid,key:e})}async delete(e){return await i("plugin:store|delete",{rid:this.rid,key:e})}async clear(){await i("plugin:store|clear",{rid:this.rid})}async reset(){await i("plugin:store|reset",{rid:this.rid})}async keys(){return await i("plugin:store|keys",{rid:this.rid})}async values(){return await i("plugin:store|values",{rid:this.rid})}async entries(){return await i("plugin:store|entries",{rid:this.rid})}async length(){return await i("plugin:store|length",{rid:this.rid})}async load(){await i("plugin:store|load",{rid:this.rid})}async save(){await i("plugin:store|save",{rid:this.rid})}async onKeyChange(e,t){return await s("store://change",(r=>{r.payload.path===this.path&&r.payload.key===e&&t(r.payload.value)}))}async onChange(e){return await s("store://change",(t=>{t.payload.path===this.path&&e(t.payload.key,t.payload.value)}))}}return e.Store=o,e.createStore=async function(e,t){const r=await i("plugin:store|create_store",{path:e,...t});return new o(r,e)},e}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})}

@ -3,7 +3,18 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
const COMMANDS: &[&str] = &[ const COMMANDS: &[&str] = &[
"set", "get", "has", "delete", "clear", "reset", "keys", "values", "length", "entries", "load", "create_store",
"set",
"get",
"has",
"delete",
"clear",
"reset",
"keys",
"values",
"length",
"entries",
"load",
"save", "save",
]; ];

@ -21,9 +21,8 @@ impl AppSettings {
let theme = store let theme = store
.get("appSettings.theme") .get("appSettings.theme")
.and_then(|v| v.as_str()) .and_then(|v| v.as_str().map(String::from))
.map(|s| s.to_string()) .unwrap_or_else(|| "dark".to_owned());
.unwrap_or_else(|| "dark".to_string());
Ok(AppSettings { Ok(AppSettings {
launch_at_login, launch_at_login,

@ -5,17 +5,24 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!! // Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri_plugin_store::StoreBuilder; use std::time::Duration;
use serde_json::json;
use tauri_plugin_store::StoreExt;
mod app; mod app;
use app::settings::AppSettings; use app::settings::AppSettings;
fn main() { fn main() {
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::default().build()) .plugin(tauri_plugin_store::Builder::new().build())
.setup(|app| { .setup(|app| {
// Init store and load it from disk // Init store and load it from disk
let mut store = StoreBuilder::new("settings.json").build(app.handle().clone()); let store = app
.handle()
.store_builder("settings.json")
.auto_save(Duration::from_millis(100))
.build();
// If there are no saved settings yet, this will return an error so we ignore the return value. // If there are no saved settings yet, this will return an error so we ignore the return value.
let _ = store.load(); let _ = store.load();
@ -27,17 +34,20 @@ fn main() {
let theme = app_settings.theme; let theme = app_settings.theme;
let launch_at_login = app_settings.launch_at_login; let launch_at_login = app_settings.launch_at_login;
println!("theme {}", theme); println!("theme {theme}");
println!("launch_at_login {}", launch_at_login); println!("launch_at_login {launch_at_login}");
store.set(
Ok(()) "appSettings",
json!({ "theme": theme, "launchAtLogin": launch_at_login }),
);
} }
Err(err) => { Err(err) => {
eprintln!("Error loading settings: {}", err); eprintln!("Error loading settings: {err}");
// Handle the error case if needed // Handle the error case if needed
Err(err) // Convert the error to a Box<dyn Error> and return Err(err) here return Err(err); // Convert the error to a Box<dyn Error> and return Err(err) here
} }
} }
Ok(())
}) })
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");

@ -4,7 +4,7 @@
import { listen, type UnlistenFn } from '@tauri-apps/api/event' import { listen, type UnlistenFn } from '@tauri-apps/api/event'
import { invoke } from '@tauri-apps/api/core' import { invoke, Resource } from '@tauri-apps/api/core'
interface ChangePayload<T> { interface ChangePayload<T> {
path: string path: string
@ -13,12 +13,36 @@ interface ChangePayload<T> {
} }
/** /**
* A key-value store persisted by the backend layer. * Options to create a store
*/ */
export class Store { export type StoreOptions = {
path: string /**
constructor(path: string) { * Auto save on modification with debounce duration in milliseconds
this.path = path */
autoSave?: boolean
}
/**
* @param path: Path to save the store in `app_data_dir`
* @param options: Store configuration options
*/
export async function createStore(path: string, options?: StoreOptions) {
const resourceId = await invoke<number>('plugin:store|create_store', {
path,
...options
})
return new Store(resourceId, path)
}
/**
* A lazy loaded key-value store persisted by the backend layer.
*/
export class Store extends Resource {
constructor(
rid: number,
private readonly path: string
) {
super(rid)
} }
/** /**
@ -30,7 +54,7 @@ export class Store {
*/ */
async set(key: string, value: unknown): Promise<void> { async set(key: string, value: unknown): Promise<void> {
await invoke('plugin:store|set', { await invoke('plugin:store|set', {
path: this.path, rid: this.rid,
key, key,
value value
}) })
@ -44,7 +68,7 @@ export class Store {
*/ */
async get<T>(key: string): Promise<T | null> { async get<T>(key: string): Promise<T | null> {
return await invoke('plugin:store|get', { return await invoke('plugin:store|get', {
path: this.path, rid: this.rid,
key key
}) })
} }
@ -57,7 +81,7 @@ export class Store {
*/ */
async has(key: string): Promise<boolean> { async has(key: string): Promise<boolean> {
return await invoke('plugin:store|has', { return await invoke('plugin:store|has', {
path: this.path, rid: this.rid,
key key
}) })
} }
@ -70,7 +94,7 @@ export class Store {
*/ */
async delete(key: string): Promise<boolean> { async delete(key: string): Promise<boolean> {
return await invoke('plugin:store|delete', { return await invoke('plugin:store|delete', {
path: this.path, rid: this.rid,
key key
}) })
} }
@ -82,9 +106,7 @@ export class Store {
* @returns * @returns
*/ */
async clear(): Promise<void> { async clear(): Promise<void> {
await invoke('plugin:store|clear', { await invoke('plugin:store|clear', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -94,9 +116,7 @@ export class Store {
* @returns * @returns
*/ */
async reset(): Promise<void> { async reset(): Promise<void> {
await invoke('plugin:store|reset', { await invoke('plugin:store|reset', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -105,9 +125,7 @@ export class Store {
* @returns * @returns
*/ */
async keys(): Promise<string[]> { async keys(): Promise<string[]> {
return await invoke('plugin:store|keys', { return await invoke('plugin:store|keys', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -116,9 +134,7 @@ export class Store {
* @returns * @returns
*/ */
async values<T>(): Promise<T[]> { async values<T>(): Promise<T[]> {
return await invoke('plugin:store|values', { return await invoke('plugin:store|values', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -127,9 +143,7 @@ export class Store {
* @returns * @returns
*/ */
async entries<T>(): Promise<Array<[key: string, value: T]>> { async entries<T>(): Promise<Array<[key: string, value: T]>> {
return await invoke('plugin:store|entries', { return await invoke('plugin:store|entries', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -138,9 +152,7 @@ export class Store {
* @returns * @returns
*/ */
async length(): Promise<number> { async length(): Promise<number> {
return await invoke('plugin:store|length', { return await invoke('plugin:store|length', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -152,9 +164,7 @@ export class Store {
* @returns * @returns
*/ */
async load(): Promise<void> { async load(): Promise<void> {
await invoke('plugin:store|load', { await invoke('plugin:store|load', { rid: this.rid })
path: this.path
})
} }
/** /**
@ -165,9 +175,7 @@ export class Store {
* @returns * @returns
*/ */
async save(): Promise<void> { async save(): Promise<void> {
await invoke('plugin:store|save', { await invoke('plugin:store|save', { rid: this.rid })
path: this.path
})
} }
/** /**

@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-create-store"
description = "Enables the create_store command without any pre-configured scope."
commands.allow = ["create_store"]
[[permission]]
identifier = "deny-create-store"
description = "Denies the create_store command without any pre-configured scope."
commands.deny = ["create_store"]

@ -9,6 +9,7 @@ All operations are enabled by default.
- `allow-create-store`
- `allow-clear` - `allow-clear`
- `allow-delete` - `allow-delete`
- `allow-entries` - `allow-entries`
@ -60,6 +61,32 @@ Denies the clear command without any pre-configured scope.
<tr> <tr>
<td> <td>
`store:allow-create-store`
</td>
<td>
Enables the create_store command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`store:deny-create-store`
</td>
<td>
Denies the create_store command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`store:allow-delete` `store:allow-delete`
</td> </td>

@ -11,6 +11,7 @@ All operations are enabled by default.
""" """
permissions = [ permissions = [
"allow-create-store",
"allow-clear", "allow-clear",
"allow-delete", "allow-delete",
"allow-entries", "allow-entries",

@ -304,6 +304,16 @@
"type": "string", "type": "string",
"const": "deny-clear" "const": "deny-clear"
}, },
{
"description": "Enables the create_store command without any pre-configured scope.",
"type": "string",
"const": "allow-create-store"
},
{
"description": "Denies the create_store command without any pre-configured scope.",
"type": "string",
"const": "deny-create-store"
},
{ {
"description": "Enables the delete command without any pre-configured scope.", "description": "Enables the delete command without any pre-configured scope.",
"type": "string", "type": "string",

@ -18,12 +18,13 @@ pub use serde_json::Value as JsonValue;
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Mutex, sync::{Mutex, Weak},
time::Duration,
}; };
pub use store::{Store, StoreBuilder}; pub use store::{Store, StoreBuilder, StoreInner};
use tauri::{ use tauri::{
plugin::{self, TauriPlugin}, plugin::{self, TauriPlugin},
AppHandle, Manager, RunEvent, Runtime, State, AppHandle, Manager, ResourceId, RunEvent, Runtime, Webview,
}; };
mod error; mod error;
@ -37,177 +38,138 @@ struct ChangePayload<'a> {
} }
pub struct StoreCollection<R: Runtime> { pub struct StoreCollection<R: Runtime> {
stores: Mutex<HashMap<PathBuf, Store<R>>>, stores: Mutex<HashMap<PathBuf, Weak<Mutex<StoreInner<R>>>>>,
frozen: bool, // frozen: bool,
} }
pub fn with_store<R: Runtime, T, F: FnOnce(&mut Store<R>) -> Result<T>>( #[tauri::command]
async fn create_store<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
collection: State<'_, StoreCollection<R>>, webview: Webview<R>,
path: impl AsRef<Path>, path: PathBuf,
f: F, auto_save: Option<u64>,
) -> Result<T> { ) -> Result<ResourceId> {
let mut stores = collection.stores.lock().expect("mutex poisoned"); let mut builder = app.store_builder(path);
if let Some(auto_save) = auto_save {
let path = path.as_ref(); builder = builder.auto_save(Duration::from_millis(auto_save));
if !stores.contains_key(path) {
if collection.frozen {
return Err(Error::NotFound(path.to_path_buf()));
}
#[allow(unused_mut)]
let mut builder = StoreBuilder::new(path);
let mut store = builder.build(app);
// ignore loading errors, just use the default
if let Err(err) = store.load() {
warn!(
"Failed to load store {:?} from disk: {}. Falling back to default values.",
path, err
);
}
stores.insert(path.to_path_buf(), store);
} }
let store = builder.build();
f(stores Ok(webview.resources_table().add(store))
.get_mut(path)
.expect("failed to retrieve store. This is a bug!"))
} }
#[tauri::command] #[tauri::command]
async fn set<R: Runtime>( async fn set<R: Runtime>(
app: AppHandle<R>, webview: Webview<R>,
stores: State<'_, StoreCollection<R>>, rid: ResourceId,
path: PathBuf,
key: String, key: String,
value: JsonValue, value: JsonValue,
) -> Result<()> { ) -> Result<()> {
with_store(app, stores, path, |store| store.insert(key, value)) let store = webview.resources_table().get::<Store<R>>(rid)?;
store.set(key, value);
Ok(())
} }
#[tauri::command] #[tauri::command]
async fn get<R: Runtime>( async fn get<R: Runtime>(
app: AppHandle<R>, webview: Webview<R>,
stores: State<'_, StoreCollection<R>>, rid: ResourceId,
path: PathBuf,
key: String, key: String,
) -> Result<Option<JsonValue>> { ) -> Result<Option<JsonValue>> {
with_store(app, stores, path, |store| Ok(store.get(key).cloned())) let store = webview.resources_table().get::<Store<R>>(rid)?;
Ok(store.get(key))
} }
#[tauri::command] #[tauri::command]
async fn has<R: Runtime>( async fn has<R: Runtime>(webview: Webview<R>, rid: ResourceId, key: String) -> Result<bool> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, Ok(store.has(key))
path: PathBuf,
key: String,
) -> Result<bool> {
with_store(app, stores, path, |store| Ok(store.has(key)))
} }
#[tauri::command] #[tauri::command]
async fn delete<R: Runtime>( async fn delete<R: Runtime>(webview: Webview<R>, rid: ResourceId, key: String) -> Result<bool> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, Ok(store.delete(key))
path: PathBuf,
key: String,
) -> Result<bool> {
with_store(app, stores, path, |store| store.delete(key))
} }
#[tauri::command] #[tauri::command]
async fn clear<R: Runtime>( async fn clear<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, store.clear();
path: PathBuf, Ok(())
) -> Result<()> {
with_store(app, stores, path, |store| store.clear())
} }
#[tauri::command] #[tauri::command]
async fn reset<R: Runtime>( async fn reset<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
collection: State<'_, StoreCollection<R>>, store.reset();
path: PathBuf, Ok(())
) -> Result<()> {
with_store(app, collection, path, |store| store.reset())
} }
#[tauri::command] #[tauri::command]
async fn keys<R: Runtime>( async fn keys<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<Vec<String>> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, Ok(store.keys())
path: PathBuf,
) -> Result<Vec<String>> {
with_store(app, stores, path, |store| {
Ok(store.keys().cloned().collect())
})
} }
#[tauri::command] #[tauri::command]
async fn values<R: Runtime>( async fn values<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<Vec<JsonValue>> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, Ok(store.values())
path: PathBuf,
) -> Result<Vec<JsonValue>> {
with_store(app, stores, path, |store| {
Ok(store.values().cloned().collect())
})
} }
#[tauri::command] #[tauri::command]
async fn entries<R: Runtime>( async fn entries<R: Runtime>(
app: AppHandle<R>, webview: Webview<R>,
stores: State<'_, StoreCollection<R>>, rid: ResourceId,
path: PathBuf,
) -> Result<Vec<(String, JsonValue)>> { ) -> Result<Vec<(String, JsonValue)>> {
with_store(app, stores, path, |store| { let store = webview.resources_table().get::<Store<R>>(rid)?;
Ok(store Ok(store.entries())
.entries()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect())
})
} }
#[tauri::command] #[tauri::command]
async fn length<R: Runtime>( async fn length<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<usize> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, Ok(store.length())
path: PathBuf,
) -> Result<usize> {
with_store(app, stores, path, |store| Ok(store.len()))
} }
#[tauri::command] #[tauri::command]
async fn load<R: Runtime>( async fn load<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, store.load()
path: PathBuf,
) -> Result<()> {
with_store(app, stores, path, |store| store.load())
} }
#[tauri::command] #[tauri::command]
async fn save<R: Runtime>( async fn save<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
app: AppHandle<R>, let store = webview.resources_table().get::<Store<R>>(rid)?;
stores: State<'_, StoreCollection<R>>, store.save()
path: PathBuf, }
) -> Result<()> {
with_store(app, stores, path, |store| store.save()) pub trait StoreExt<R: Runtime> {
fn store(&self, path: impl AsRef<Path>) -> Store<R>;
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R>;
}
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Store<R> {
StoreBuilder::new(self.app_handle(), path).build()
}
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R> {
StoreBuilder::new(self.app_handle(), path)
}
} }
// #[derive(Default)] // #[derive(Default)]
pub struct Builder<R: Runtime> { pub struct Builder<R: Runtime> {
stores: HashMap<PathBuf, Store<R>>, stores: HashMap<PathBuf, Store<R>>,
frozen: bool, // frozen: bool,
} }
impl<R: Runtime> Default for Builder<R> { impl<R: Runtime> Default for Builder<R> {
fn default() -> Self { fn default() -> Self {
Self { Self {
stores: Default::default(), stores: Default::default(),
frozen: false, // frozen: false,
} }
} }
} }
@ -217,114 +179,126 @@ impl<R: Runtime> Builder<R> {
Self::default() Self::default()
} }
/// Registers a store with the plugin. // /// Registers a store with the plugin.
/// // ///
/// # Examples // /// # Examples
/// // ///
/// ``` // /// ```
/// use tauri_plugin_store::{StoreBuilder, Builder}; // /// use tauri_plugin_store::{StoreBuilder, Builder};
/// // ///
/// tauri::Builder::default() // /// tauri::Builder::default()
/// .setup(|app| { // /// .setup(|app| {
/// let store = StoreBuilder::new("store.bin").build(app.handle().clone()); // /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
/// let builder = Builder::default().store(store); // /// let builder = Builder::default().store(store);
/// Ok(()) // /// Ok(())
/// }); // /// });
/// ``` // /// ```
pub fn store(mut self, store: Store<R>) -> Self { // pub fn store(mut self, store: Store<R>) -> Self {
self.stores.insert(store.path.clone(), store); // self.stores.insert(store.path.clone(), store);
self // self
} // }
/// Registers multiple stores with the plugin. // /// Registers multiple stores with the plugin.
/// // ///
/// # Examples // /// # Examples
/// // ///
/// ``` // /// ```
/// use tauri_plugin_store::{StoreBuilder, Builder}; // /// use tauri_plugin_store::{StoreBuilder, Builder};
/// // ///
/// tauri::Builder::default() // /// tauri::Builder::default()
/// .setup(|app| { // /// .setup(|app| {
/// let store = StoreBuilder::new("store.bin").build(app.handle().clone()); // /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
/// let builder = Builder::default().stores([store]); // /// let builder = Builder::default().stores([store]);
/// Ok(()) // /// Ok(())
/// }); // /// });
/// ``` // /// ```
pub fn stores<T: IntoIterator<Item = Store<R>>>(mut self, stores: T) -> Self { // pub fn stores<T: IntoIterator<Item = Store<R>>>(mut self, stores: T) -> Self {
self.stores = stores // self.stores = stores
.into_iter() // .into_iter()
.map(|store| (store.path.clone(), store)) // .map(|store| (store.path.clone(), store))
.collect(); // .collect();
self // self
} // }
/// Freezes the collection. // /// Freezes the collection.
/// // ///
/// This causes requests for plugins that haven't been registered to fail // /// This causes requests for plugins that haven't been registered to fail
/// // ///
/// # Examples // /// # Examples
/// // ///
/// ``` // /// ```
/// use tauri_plugin_store::{StoreBuilder, Builder}; // /// use tauri_plugin_store::{StoreBuilder, Builder};
/// // ///
/// tauri::Builder::default() // /// tauri::Builder::default()
/// .setup(|app| { // /// .setup(|app| {
/// let store = StoreBuilder::new("store.bin").build(app.handle().clone()); // /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
/// app.handle().plugin(Builder::default().freeze().build()); // /// app.handle().plugin(Builder::default().freeze().build());
/// Ok(()) // /// Ok(())
/// }); // /// });
/// ``` // /// ```
pub fn freeze(mut self) -> Self { // pub fn freeze(mut self) -> Self {
self.frozen = true; // self.frozen = true;
self // self
} // }
/// Builds the plugin. /// Builds the plugin.
/// ///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use tauri_plugin_store::{StoreBuilder, Builder};
///
/// tauri::Builder::default() /// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| { /// .setup(|app| {
/// let store = StoreBuilder::new("store.bin").build(app.handle().clone()); /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build();
/// app.handle().plugin(Builder::default().build());
/// Ok(()) /// Ok(())
/// }); /// });
/// ``` /// ```
pub fn build(mut self) -> TauriPlugin<R> { pub fn build(mut self) -> TauriPlugin<R> {
plugin::Builder::new("store") plugin::Builder::new("store")
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
set, get, has, delete, clear, reset, keys, values, length, entries, load, save create_store,
set,
get,
has,
delete,
clear,
reset,
keys,
values,
length,
entries,
load,
save
]) ])
.setup(move |app_handle, _api| { .setup(move |app_handle, _api| {
for (path, store) in self.stores.iter_mut() { for (path, store) in self.stores.iter_mut() {
// ignore loading errors, just use the default // ignore loading errors, just use the default
if let Err(err) = store.load() { if let Err(err) = store.load() {
warn!( warn!(
"Failed to load store {:?} from disk: {}. Falling back to default values.", "Failed to load store {path:?} from disk: {err}. Falling back to default values."
path, err );
);
} }
} }
app_handle.manage(StoreCollection { app_handle.manage(StoreCollection::<R> {
stores: Mutex::new(self.stores), stores: Mutex::new(HashMap::new()),
frozen: self.frozen, // frozen: self.frozen,
}); });
Ok(()) Ok(())
}) })
.on_event(|app_handle, event| { .on_event(|_app_handle, event| {
if let RunEvent::Exit = event { if let RunEvent::Exit = event {
let collection = app_handle.state::<StoreCollection<R>>(); // let collection = app_handle.state::<StoreCollection<R>>();
for store in collection.stores.lock().expect("mutex poisoned").values() { // for store in collection.stores.lock().expect("mutex poisoned").values_mut() {
if let Err(err) = store.save() { // if let Some(sender) = store.auto_save_debounce_sender.take() {
eprintln!("failed to save store {:?} with error {:?}", store.path, err); // let _ = sender.send(AutoSaveMessage::Cancel);
} // }
} // if let Err(err) = store.save() {
// eprintln!("failed to save store {:?} with error {:?}", store.path, err);
// }
// }
} }
}) })
.build() .build()

@ -2,19 +2,26 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use crate::{ChangePayload, Error}; use crate::{ChangePayload, StoreCollection};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::{create_dir_all, read, File}, fs::{create_dir_all, read, File},
io::Write, io::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{Arc, Mutex},
time::Duration,
};
use tauri::{AppHandle, Emitter, Manager, Resource, Runtime};
use tokio::{
select,
sync::mpsc::{unbounded_channel, UnboundedSender},
time::sleep,
}; };
use tauri::{AppHandle, Emitter, Manager, Runtime};
type SerializeFn = type SerializeFn =
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>; fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
type DeserializeFn = pub(crate) type DeserializeFn =
fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>; fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>;
fn default_serialize( fn default_serialize(
@ -30,35 +37,38 @@ fn default_deserialize(
} }
/// Builds a [`Store`] /// Builds a [`Store`]
pub struct StoreBuilder { pub struct StoreBuilder<R: Runtime> {
app: AppHandle<R>,
path: PathBuf, path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>, defaults: Option<HashMap<String, JsonValue>>,
cache: HashMap<String, JsonValue>, cache: HashMap<String, JsonValue>,
serialize: SerializeFn, serialize: SerializeFn,
deserialize: DeserializeFn, deserialize: DeserializeFn,
auto_save: Option<Duration>,
} }
impl StoreBuilder { impl<R: Runtime> StoreBuilder<R> {
/// Creates a new [`StoreBuilder`]. /// Creates a new [`StoreBuilder`].
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// tauri::Builder::default()
/// use tauri_plugin_store::StoreBuilder; /// .plugin(tauri_plugin_store::Builder::default().build())
/// /// .setup(|app| {
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin"); /// let builder = tauri_plugin_store::StoreBuilder::new(app, "store.bin");
/// /// Ok(())
/// # Ok(()) /// });
/// # }
/// ``` /// ```
pub fn new<P: AsRef<Path>>(path: P) -> Self { pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
Self { Self {
app: manager.app_handle().clone(),
// Since Store.path is only exposed to the user in emit calls we may as well simplify it here already. // Since Store.path is only exposed to the user in emit calls we may as well simplify it here already.
path: dunce::simplified(path.as_ref()).to_path_buf(), path: dunce::simplified(path.as_ref()).to_path_buf(),
defaults: None, defaults: None,
cache: Default::default(), cache: Default::default(),
serialize: default_serialize, serialize: default_serialize,
deserialize: default_deserialize, deserialize: default_deserialize,
auto_save: None,
} }
} }
@ -66,19 +76,18 @@ impl StoreBuilder {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// tauri::Builder::default()
/// use tauri_plugin_store::StoreBuilder; /// .plugin(tauri_plugin_store::Builder::default().build())
/// use std::collections::HashMap; /// .setup(|app| {
/// /// let mut defaults = std::collections::HashMap::new();
/// let mut defaults = HashMap::new(); /// defaults.insert("foo".to_string(), "bar".into());
///
/// defaults.insert("foo".to_string(), "bar".into());
///
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin")
/// .defaults(defaults);
/// ///
/// # Ok(()) /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// # } /// .defaults(defaults)
/// .build();
/// Ok(())
/// });
/// ```
pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self { pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self {
self.cache.clone_from(&defaults); self.cache.clone_from(&defaults);
self.defaults = Some(defaults); self.defaults = Some(defaults);
@ -89,15 +98,18 @@ impl StoreBuilder {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// tauri::Builder::default()
/// use tauri_plugin_store::StoreBuilder; /// .plugin(tauri_plugin_store::Builder::default().build())
/// /// .setup(|app| {
/// let builder = StoreBuilder::<tauri::Wry>::new("store.bin") /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar".into()); /// .default("foo".to_string(), "bar")
/// /// .build();
/// # Ok(()) /// Ok(())
/// # } /// });
pub fn default(mut self, key: String, value: JsonValue) -> Self { /// ```
pub fn default(mut self, key: impl Into<String>, value: impl Into<JsonValue>) -> Self {
let key = key.into();
let value = value.into();
self.cache.insert(key.clone(), value.clone()); self.cache.insert(key.clone(), value.clone());
self.defaults self.defaults
.get_or_insert(HashMap::new()) .get_or_insert(HashMap::new())
@ -109,14 +121,15 @@ impl StoreBuilder {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// tauri::Builder::default()
/// use tauri_plugin_store::StoreBuilder; /// .plugin(tauri_plugin_store::Builder::default().build())
/// /// .setup(|app| {
/// let builder = StoreBuilder::<tauri::Wry>::new("store.json") /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into)); /// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// /// .build();
/// # Ok(()) /// Ok(())
/// # } /// });
/// ```
pub fn serialize(mut self, serialize: SerializeFn) -> Self { pub fn serialize(mut self, serialize: SerializeFn) -> Self {
self.serialize = serialize; self.serialize = serialize;
self self
@ -126,53 +139,102 @@ impl StoreBuilder {
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> { /// tauri::Builder::default()
/// use tauri_plugin_store::StoreBuilder; /// .plugin(tauri_plugin_store::Builder::default().build())
/// /// .setup(|app| {
/// let builder = StoreBuilder::<tauri::Wry>::new("store.json") /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into)); /// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// /// .build();
/// # Ok(()) /// Ok(())
/// # } /// });
/// ```
pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self { pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self {
self.deserialize = deserialize; self.deserialize = deserialize;
self self
} }
/// Auto save on modified with a debounce duration
///
/// Note: only works if this store is managed by the plugin (e.g. made using [`crate::with_store`] or inserted into [`crate::Builder`])
///
/// # 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 auto_save(mut self, debounce_duration: Duration) -> Self {
self.auto_save = Some(debounce_duration);
self
}
/// Builds the [`Store`]. /// Builds the [`Store`].
/// ///
/// # Examples /// # Examples
/// ``` /// ```
/// tauri::Builder::default() /// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| { /// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new("store.json").build(app.handle().clone()); /// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json").build();
/// Ok(()) /// Ok(())
/// }); /// });
/// ``` /// ```
pub fn build<R: Runtime>(self, app: AppHandle<R>) -> Store<R> { pub fn build(self) -> Store<R> {
let collection = self.app.state::<StoreCollection<R>>();
let mut stores = collection.stores.lock().unwrap();
let store = stores
.get(&self.path)
.and_then(|store| store.upgrade())
.unwrap_or_else(|| {
let mut store = StoreInner::new(self.app.clone(), self.path.clone());
let _ = store.load(self.deserialize);
let store = Arc::new(Mutex::new(store));
stores.insert(
self.path.clone(),
Arc::<Mutex<StoreInner<R>>>::downgrade(&store),
);
store
});
drop(stores);
Store { Store {
app,
path: self.path,
defaults: self.defaults, defaults: self.defaults,
cache: self.cache,
serialize: self.serialize, serialize: self.serialize,
deserialize: self.deserialize, deserialize: self.deserialize,
auto_save: self.auto_save,
auto_save_debounce_sender: Arc::new(Mutex::new(None)),
store,
} }
} }
} }
pub(crate) enum AutoSaveMessage {
Reset,
Cancel,
}
#[derive(Clone)] #[derive(Clone)]
pub struct Store<R: Runtime> { pub struct StoreInner<R: Runtime> {
pub(crate) app: AppHandle<R>, pub(crate) app: AppHandle<R>,
pub(crate) path: PathBuf, pub(crate) path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>,
pub(crate) cache: HashMap<String, JsonValue>, pub(crate) cache: HashMap<String, JsonValue>,
pub(crate) serialize: SerializeFn,
pub(crate) deserialize: DeserializeFn,
} }
impl<R: Runtime> Store<R> { impl<R: Runtime> StoreInner<R> {
pub fn save(&self) -> Result<(), Error> { pub fn new(app: AppHandle<R>, path: PathBuf) -> Self {
Self {
app,
path,
cache: HashMap::new(),
}
}
pub fn save(&self, serialize_fn: SerializeFn) -> crate::Result<()> {
let app_dir = self let app_dir = self
.app .app
.path() .path()
@ -182,7 +244,7 @@ impl<R: Runtime> Store<R> {
create_dir_all(store_path.parent().expect("invalid store path"))?; create_dir_all(store_path.parent().expect("invalid store path"))?;
let bytes = (self.serialize)(&self.cache).map_err(Error::Serialize)?; let bytes = serialize_fn(&self.cache).map_err(crate::Error::Serialize)?;
let mut f = File::create(&store_path)?; let mut f = File::create(&store_path)?;
f.write_all(&bytes)?; f.write_all(&bytes)?;
@ -190,7 +252,7 @@ impl<R: Runtime> Store<R> {
} }
/// Update the store from the on-disk state /// Update the store from the on-disk state
pub fn load(&mut self) -> Result<(), Error> { pub fn load(&mut self, deserialize_fn: DeserializeFn) -> crate::Result<()> {
let app_dir = self let app_dir = self
.app .app
.path() .path()
@ -201,23 +263,16 @@ impl<R: Runtime> Store<R> {
let bytes = read(store_path)?; let bytes = read(store_path)?;
self.cache self.cache
.extend((self.deserialize)(&bytes).map_err(Error::Deserialize)?); .extend(deserialize_fn(&bytes).map_err(crate::Error::Deserialize)?);
Ok(()) Ok(())
} }
pub fn insert(&mut self, key: String, value: JsonValue) -> Result<(), Error> { pub fn insert(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
let key = key.into();
let value = value.into();
self.cache.insert(key.clone(), value.clone()); self.cache.insert(key.clone(), value.clone());
self.app.emit( let _ = self.emit_change_event(&key, &value);
"store://change",
ChangePayload {
path: &self.path,
key: &key,
value: &value,
},
)?;
Ok(())
} }
pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> { pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> {
@ -228,57 +283,36 @@ impl<R: Runtime> Store<R> {
self.cache.contains_key(key.as_ref()) self.cache.contains_key(key.as_ref())
} }
pub fn delete(&mut self, key: impl AsRef<str>) -> Result<bool, Error> { pub fn delete(&mut self, key: impl AsRef<str>) -> bool {
let flag = self.cache.remove(key.as_ref()).is_some(); let flag = self.cache.remove(key.as_ref()).is_some();
if flag { if flag {
self.app.emit( let _ = self.emit_change_event(key.as_ref(), &JsonValue::Null);
"store://change",
ChangePayload {
path: &self.path,
key: key.as_ref(),
value: &JsonValue::Null,
},
)?;
} }
Ok(flag) flag
} }
pub fn clear(&mut self) -> Result<(), Error> { pub fn clear(&mut self) {
let keys: Vec<String> = self.cache.keys().cloned().collect(); let keys: Vec<String> = self.cache.keys().cloned().collect();
self.cache.clear(); self.cache.clear();
for key in keys { for key in &keys {
self.app.emit( let _ = self.emit_change_event(key, &JsonValue::Null);
"store://change",
ChangePayload {
path: &self.path,
key: &key,
value: &JsonValue::Null,
},
)?;
} }
Ok(())
} }
pub fn reset(&mut self) -> Result<(), Error> { pub fn reset(&mut self, defaults: &Option<HashMap<String, JsonValue>>) {
let has_defaults = self.defaults.is_some(); if let Some(defaults) = &defaults {
for (key, value) in &self.cache {
if has_defaults { if defaults.get(key) != Some(value) {
if let Some(defaults) = &self.defaults { let _ =
for (key, value) in &self.cache { self.emit_change_event(key, defaults.get(key).unwrap_or(&JsonValue::Null));
if defaults.get(key) != Some(value) {
let _ = self.app.emit(
"store://change",
ChangePayload {
path: &self.path,
key,
value: defaults.get(key).unwrap_or(&JsonValue::Null),
},
);
}
} }
self.cache.clone_from(defaults);
} }
Ok(()) for (key, value) in defaults {
if !self.cache.contains_key(key) {
let _ = self.emit_change_event(key, value);
}
}
self.cache.clone_from(defaults);
} else { } else {
self.clear() self.clear()
} }
@ -303,14 +337,152 @@ impl<R: Runtime> Store<R> {
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.cache.is_empty() self.cache.is_empty()
} }
fn emit_change_event(&self, key: &str, value: &JsonValue) -> crate::Result<()> {
self.app.emit(
"store://change",
ChangePayload {
path: &self.path,
key,
value,
},
)?;
Ok(())
}
} }
impl<R: Runtime> std::fmt::Debug for Store<R> { impl<R: Runtime> std::fmt::Debug for StoreInner<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Store") f.debug_struct("Store")
.field("path", &self.path) .field("path", &self.path)
.field("defaults", &self.defaults)
.field("cache", &self.cache) .field("cache", &self.cache)
.finish() .finish()
} }
} }
pub struct Store<R: Runtime> {
defaults: Option<HashMap<String, JsonValue>>,
serialize: SerializeFn,
deserialize: DeserializeFn,
auto_save: Option<Duration>,
auto_save_debounce_sender: Arc<Mutex<Option<UnboundedSender<AutoSaveMessage>>>>,
store: Arc<Mutex<StoreInner<R>>>,
}
impl<R: Runtime> Resource for Store<R> {}
impl<R: Runtime> Store<R> {
pub fn with_store<T>(
&self,
f: impl FnOnce(&mut StoreInner<R>) -> crate::Result<T>,
) -> crate::Result<T> {
let mut store = self.store.lock().unwrap();
f(&mut store)
}
pub fn set(&self, key: impl Into<String>, value: impl Into<JsonValue>) {
self.store.lock().unwrap().insert(key.into(), value.into());
let _ = self.trigger_auto_save();
}
pub fn get(&self, key: impl AsRef<str>) -> Option<JsonValue> {
self.store.lock().unwrap().get(key).cloned()
}
pub fn has(&self, key: impl AsRef<str>) -> bool {
self.store.lock().unwrap().has(key)
}
pub fn delete(&self, key: impl AsRef<str>) -> bool {
let deleted = self.store.lock().unwrap().delete(key);
if deleted {
let _ = self.trigger_auto_save();
}
deleted
}
pub fn clear(&self) {
self.store.lock().unwrap().clear();
let _ = self.trigger_auto_save();
}
pub fn reset(&self) {
self.store.lock().unwrap().reset(&self.defaults);
let _ = self.trigger_auto_save();
}
pub fn keys(&self) -> Vec<String> {
self.store.lock().unwrap().keys().cloned().collect()
}
pub fn values(&self) -> Vec<JsonValue> {
self.store.lock().unwrap().values().cloned().collect()
}
pub fn entries(&self) -> Vec<(String, JsonValue)> {
self.store
.lock()
.unwrap()
.entries()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect()
}
pub fn length(&self) -> usize {
self.store.lock().unwrap().len()
}
pub fn load(&self) -> crate::Result<()> {
self.store.lock().unwrap().load(self.deserialize)
}
pub fn save(&self) -> crate::Result<()> {
self.store.lock().unwrap().save(self.serialize)
}
fn trigger_auto_save(&self) -> crate::Result<()> {
let Some(auto_save_delay) = self.auto_save else {
return Ok(());
};
if auto_save_delay.is_zero() {
return self.save();
}
let mut auto_save_debounce_sender = self.auto_save_debounce_sender.lock().unwrap();
if let Some(ref sender) = *auto_save_debounce_sender {
let _ = sender.send(AutoSaveMessage::Reset);
return Ok(());
}
let (sender, mut receiver) = unbounded_channel();
auto_save_debounce_sender.replace(sender);
drop(auto_save_debounce_sender);
let store = self.store.clone();
let serialize_fn = self.serialize;
let auto_save_debounce_sender = self.auto_save_debounce_sender.clone();
tauri::async_runtime::spawn(async move {
loop {
select! {
should_cancel = receiver.recv() => {
if matches!(should_cancel, Some(AutoSaveMessage::Cancel) | None) {
return;
}
}
_ = sleep(auto_save_delay) => {
let _ = store.lock().unwrap().save(serialize_fn);
auto_save_debounce_sender.lock().unwrap().take();
return;
}
};
}
});
Ok(())
}
}
impl<R: Runtime> Drop for Store<R> {
fn drop(&mut self) {
let auto_save_debounce_sender = self.auto_save_debounce_sender.lock().unwrap();
if let Some(ref sender) = *auto_save_debounce_sender {
let _ = sender.send(AutoSaveMessage::Cancel);
}
}
}

Loading…
Cancel
Save