refactor(store)!: more reworks (#1860)

* refactor(store): more reworks

* Enable auto save by default

* Store to resource table by default

* Remove share store

* Clean up

* Add close store

* Add store function

* Add lazy store

* Add init to lazy store

* refresh cache in example

* Add get-or-create-store

* Revert "Add get-or-create-store"

This reverts commit 7ffd769240.

* try get first

* Docs

* Use absolute path for store

* more docs

* Allow js to use pre-stored (de)serialize functions

* Fix js get and close store

* Show case how to use pretty json

* Update readme

* Use store instead of `store_builder` in example

* Build

* Fix example

* More docs for StoreBuilder::build

* Add default (de)serialize fn

* Use pretty json by default

* Use `undefined` for empty value in get

* Change files

* Differentiate json null from no value for events

* Add create or existing

* Build

* Rename inner store's inset method to set

* Update readme

* Apply suggestions from code review

* Use close instead

* Update breaking changes

* Return result in resolve_store_path

* Change to close_resource and take &self

* Clean up

* Apply suggestions from code review

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>

* Remove unused pub(crate)

* Update change file

* Expose resolve_store_path

* Remove with_store

* Remove StoreInner from pub and expose is_empty

* Fix wrong jsdoc param

* Update readme

* rename createOrExistingStore to createOrLoad

* make api consistent with the JS implementation, add examples

* fmt

* reintroduce "get existing store" behavior for create_or_load

* rename createOrLoad to newOrExisting

* keep store lock throughout whole new_or_existing_inner

* Remove load

* Missed if load

* Don't make StoreState public

* Remove R: Runtime from Builder

* rename newOrExisting to load, load to reload

* update docs

* rename missing reload fn

* rename builder fn to build()

* fix default permission

* Fix description and create_new logic

* Clippy

* Update docs

* Update docs

* remove create_store command

* remove close_store command since we extend from Resource

* Revert "remove close_store command since we extend from Resource"

This reverts commit 4a29fc8990.

* Reapply "remove close_store command since we extend from Resource"

This reverts commit 70a1830e7d.

---------

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Tony
2024-10-17 19:14:41 +08:00
committed by GitHub
parent cfd48b3b2e
commit 8c67d44aef
20 changed files with 1073 additions and 522 deletions
+9 -4
View File
@@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT
use serde::{Serialize, Serializer};
use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>;
@@ -21,9 +20,15 @@ pub enum Error {
/// IO error.
#[error(transparent)]
Io(#[from] std::io::Error),
/// Store not found
#[error("Store \"{0}\" not found")]
NotFound(PathBuf),
// /// Store already exists
// #[error("Store at \"{0}\" already exists")]
// AlreadyExists(PathBuf),
/// Serialize function not found
#[error("Serialize Function \"{0}\" not found")]
SerializeFunctionNotFound(String),
/// Deserialize function not found
#[error("Deserialize Function \"{0}\" not found")]
DeserializeFunctionNotFound(String),
/// Some Tauri API failed
#[error(transparent)]
Tauri(#[from] tauri::Error),
+292 -153
View File
@@ -12,234 +12,392 @@
)]
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,
path::{Path, PathBuf},
sync::{Mutex, Weak},
sync::{Arc, Mutex},
time::Duration,
};
pub use store::{Store, StoreBuilder, StoreInner};
pub use store::{resolve_store_path, DeserializeFn, SerializeFn, Store, StoreBuilder};
use tauri::{
plugin::{self, TauriPlugin},
AppHandle, Manager, ResourceId, RunEvent, Runtime, Webview,
AppHandle, Manager, ResourceId, RunEvent, Runtime, State,
};
mod error;
mod store;
#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
struct ChangePayload<'a> {
path: &'a Path,
resource_id: Option<u32>,
key: &'a str,
value: &'a JsonValue,
value: Option<&'a JsonValue>,
exists: bool,
}
pub struct StoreCollection<R: Runtime> {
stores: Mutex<HashMap<PathBuf, Weak<Mutex<StoreInner<R>>>>>,
// frozen: bool,
#[derive(Debug)]
struct StoreState {
stores: Arc<Mutex<HashMap<PathBuf, ResourceId>>>,
serialize_fns: HashMap<String, SerializeFn>,
deserialize_fns: HashMap<String, DeserializeFn>,
default_serialize: SerializeFn,
default_deserialize: DeserializeFn,
}
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum AutoSave {
DebounceDuration(u64),
Bool(bool),
}
fn builder<R: Runtime>(
app: AppHandle<R>,
store_state: State<'_, StoreState>,
path: PathBuf,
auto_save: Option<AutoSave>,
serialize_fn_name: Option<String>,
deserialize_fn_name: Option<String>,
create_new: bool,
) -> Result<StoreBuilder<R>> {
let mut builder = app.store_builder(path);
if let Some(auto_save) = auto_save {
match auto_save {
AutoSave::DebounceDuration(duration) => {
builder = builder.auto_save(Duration::from_millis(duration));
}
AutoSave::Bool(false) => {
builder = builder.disable_auto_save();
}
_ => {}
}
}
if let Some(serialize_fn_name) = serialize_fn_name {
let serialize_fn = store_state
.serialize_fns
.get(&serialize_fn_name)
.ok_or_else(|| crate::Error::SerializeFunctionNotFound(serialize_fn_name))?;
builder = builder.serialize(*serialize_fn);
}
if let Some(deserialize_fn_name) = deserialize_fn_name {
let deserialize_fn = store_state
.deserialize_fns
.get(&deserialize_fn_name)
.ok_or_else(|| crate::Error::DeserializeFunctionNotFound(deserialize_fn_name))?;
builder = builder.deserialize(*deserialize_fn);
}
if create_new {
builder = builder.create_new();
}
Ok(builder)
}
#[tauri::command]
async fn create_store<R: Runtime>(
async fn load<R: Runtime>(
app: AppHandle<R>,
webview: Webview<R>,
store_state: State<'_, StoreState>,
path: PathBuf,
auto_save: Option<u64>,
auto_save: Option<AutoSave>,
serialize_fn_name: Option<String>,
deserialize_fn_name: Option<String>,
create_new: Option<bool>,
) -> Result<ResourceId> {
let mut builder = app.store_builder(path);
if let Some(auto_save) = auto_save {
builder = builder.auto_save(Duration::from_millis(auto_save));
}
let store = builder.build();
Ok(webview.resources_table().add(store))
let builder = builder(
app,
store_state,
path,
auto_save,
serialize_fn_name,
deserialize_fn_name,
create_new.unwrap_or_default(),
)?;
let (_, rid) = builder.build_inner()?;
Ok(rid)
}
#[tauri::command]
async fn get_store<R: Runtime>(
app: AppHandle<R>,
store_state: State<'_, StoreState>,
path: PathBuf,
) -> Result<Option<ResourceId>> {
let stores = store_state.stores.lock().unwrap();
Ok(stores.get(&resolve_store_path(&app, path)?).copied())
}
#[tauri::command]
async fn set<R: Runtime>(
webview: Webview<R>,
app: AppHandle<R>,
rid: ResourceId,
key: String,
value: JsonValue,
) -> Result<()> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
let store = app.resources_table().get::<Store<R>>(rid)?;
store.set(key, value);
Ok(())
}
#[tauri::command]
async fn get<R: Runtime>(
webview: Webview<R>,
app: AppHandle<R>,
rid: ResourceId,
key: String,
) -> Result<Option<JsonValue>> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
Ok(store.get(key))
) -> Result<(Option<JsonValue>, bool)> {
let store = app.resources_table().get::<Store<R>>(rid)?;
let value = store.get(key);
let exists = value.is_some();
Ok((value, exists))
}
#[tauri::command]
async fn has<R: Runtime>(webview: Webview<R>, rid: ResourceId, key: String) -> Result<bool> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn has<R: Runtime>(app: AppHandle<R>, rid: ResourceId, key: String) -> Result<bool> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.has(key))
}
#[tauri::command]
async fn delete<R: Runtime>(webview: Webview<R>, rid: ResourceId, key: String) -> Result<bool> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn delete<R: Runtime>(app: AppHandle<R>, rid: ResourceId, key: String) -> Result<bool> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.delete(key))
}
#[tauri::command]
async fn clear<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn clear<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
let store = app.resources_table().get::<Store<R>>(rid)?;
store.clear();
Ok(())
}
#[tauri::command]
async fn reset<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn reset<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
let store = app.resources_table().get::<Store<R>>(rid)?;
store.reset();
Ok(())
}
#[tauri::command]
async fn keys<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<Vec<String>> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn keys<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<Vec<String>> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.keys())
}
#[tauri::command]
async fn values<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<Vec<JsonValue>> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn values<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<Vec<JsonValue>> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.values())
}
#[tauri::command]
async fn entries<R: Runtime>(
webview: Webview<R>,
app: AppHandle<R>,
rid: ResourceId,
) -> Result<Vec<(String, JsonValue)>> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.entries())
}
#[tauri::command]
async fn length<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<usize> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn length<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<usize> {
let store = app.resources_table().get::<Store<R>>(rid)?;
Ok(store.length())
}
#[tauri::command]
async fn load<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
store.load()
async fn reload<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
let store = app.resources_table().get::<Store<R>>(rid)?;
store.reload()
}
#[tauri::command]
async fn save<R: Runtime>(webview: Webview<R>, rid: ResourceId) -> Result<()> {
let store = webview.resources_table().get::<Store<R>>(rid)?;
async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
let store = app.resources_table().get::<Store<R>>(rid)?;
store.save()
}
pub trait StoreExt<R: Runtime> {
fn store(&self, path: impl AsRef<Path>) -> Store<R>;
/// Create a store or load an existing store with default settings at the given path.
///
/// If the store is already loaded, its instance is automatically returned.
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.store("my-store")?;
/// Ok(())
/// });
/// ```
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
/// Get a store builder.
///
/// The builder can be used to configure the store.
/// To use the default settings see [`Self::store`].
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
/// use std::time::Duration;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = app.store_builder("users.json").auto_save(Duration::from_secs(1)).build()?;
/// Ok(())
/// });
/// ```
fn store_builder(&self, path: impl AsRef<Path>) -> StoreBuilder<R>;
/// Get a handle of an already loaded store.
///
/// If the store is not loaded or does not exist, it returns `None`.
///
/// Note that using this function can cause race conditions if you fallback to creating or loading the store,
/// so you should consider using [`Self::store`] if you are not sure if the store is loaded or not.
///
/// # Examples
///
/// ```
/// use tauri_plugin_store::StoreExt;
///
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = if let Some(s) = app.get_store("store.json") {
/// s
/// } else {
/// // this is not thread safe; if another thread is doing the same load/create,
/// // there will be a race condition; in this case we could remove the get_store
/// // and only run app.store() as it will return the existing store if it has been loaded
/// app.store("store.json")?
/// };
/// Ok(())
/// });
/// ```
fn get_store(&self, path: impl AsRef<Path>) -> Option<Arc<Store<R>>>;
}
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
fn store(&self, path: impl AsRef<Path>) -> Store<R> {
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<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)
}
fn get_store(&self, path: impl AsRef<Path>) -> Option<Arc<Store<R>>> {
let collection = self.state::<StoreState>();
let stores = collection.stores.lock().unwrap();
stores
.get(&resolve_store_path(self.app_handle(), path.as_ref()).ok()?)
.and_then(|rid| self.resources_table().get(*rid).ok())
}
}
// #[derive(Default)]
pub struct Builder<R: Runtime> {
stores: HashMap<PathBuf, Store<R>>,
// frozen: bool,
fn default_serialize(
cache: &HashMap<String, JsonValue>,
) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
Ok(serde_json::to_vec_pretty(&cache)?)
}
impl<R: Runtime> Default for Builder<R> {
fn default_deserialize(
bytes: &[u8],
) -> std::result::Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>> {
serde_json::from_slice(bytes).map_err(Into::into)
}
pub struct Builder {
serialize_fns: HashMap<String, SerializeFn>,
deserialize_fns: HashMap<String, DeserializeFn>,
default_serialize: SerializeFn,
default_deserialize: DeserializeFn,
}
impl Default for Builder {
fn default() -> Self {
Self {
stores: Default::default(),
// frozen: false,
serialize_fns: Default::default(),
deserialize_fns: Default::default(),
default_serialize,
default_deserialize,
}
}
}
impl<R: Runtime> Builder<R> {
impl Builder {
pub fn new() -> Self {
Self::default()
}
// /// Registers a store with the plugin.
// ///
// /// # Examples
// ///
// /// ```
// /// use tauri_plugin_store::{StoreBuilder, Builder};
// ///
// /// tauri::Builder::default()
// /// .setup(|app| {
// /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
// /// let builder = Builder::default().store(store);
// /// Ok(())
// /// });
// /// ```
// pub fn store(mut self, store: Store<R>) -> Self {
// self.stores.insert(store.path.clone(), store);
// self
// }
/// Register a serialize function to access it from the JavaScript side
///
/// # Examples
///
/// ```
/// fn no_pretty_json(
/// cache: &std::collections::HashMap<String, serde_json::Value>,
/// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
/// Ok(serde_json::to_vec(&cache)?)
/// }
///
/// tauri::Builder::default()
/// .plugin(
/// tauri_plugin_store::Builder::default()
/// .register_serialize_fn("no-pretty-json".to_owned(), no_pretty_json)
/// .build(),
/// );
/// ```
pub fn register_serialize_fn(mut self, name: String, serialize_fn: SerializeFn) -> Self {
self.serialize_fns.insert(name, serialize_fn);
self
}
// /// Registers multiple stores with the plugin.
// ///
// /// # Examples
// ///
// /// ```
// /// use tauri_plugin_store::{StoreBuilder, Builder};
// ///
// /// tauri::Builder::default()
// /// .setup(|app| {
// /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
// /// let builder = Builder::default().stores([store]);
// /// Ok(())
// /// });
// /// ```
// pub fn stores<T: IntoIterator<Item = Store<R>>>(mut self, stores: T) -> Self {
// self.stores = stores
// .into_iter()
// .map(|store| (store.path.clone(), store))
// .collect();
// self
// }
/// Register a deserialize function to access it from the JavaScript side
pub fn register_deserialize_fn(mut self, name: String, deserialize_fn: DeserializeFn) -> Self {
self.deserialize_fns.insert(name, deserialize_fn);
self
}
// /// Freezes the collection.
// ///
// /// This causes requests for plugins that haven't been registered to fail
// ///
// /// # Examples
// ///
// /// ```
// /// use tauri_plugin_store::{StoreBuilder, Builder};
// ///
// /// tauri::Builder::default()
// /// .setup(|app| {
// /// let store = StoreBuilder::new("store.bin").build(app.handle().clone());
// /// app.handle().plugin(Builder::default().freeze().build());
// /// Ok(())
// /// });
// /// ```
// pub fn freeze(mut self) -> Self {
// self.frozen = true;
// self
// }
/// Use this serialize function for stores by default
///
/// # Examples
///
/// ```
/// fn no_pretty_json(
/// cache: &std::collections::HashMap<String, serde_json::Value>,
/// ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
/// Ok(serde_json::to_vec(&cache)?)
/// }
///
/// tauri::Builder::default()
/// .plugin(
/// tauri_plugin_store::Builder::default()
/// .default_serialize_fn(no_pretty_json)
/// .build(),
/// );
/// ```
pub fn default_serialize_fn(mut self, serialize_fn: SerializeFn) -> Self {
self.default_serialize = serialize_fn;
self
}
/// Use this deserialize function for stores by default
pub fn default_deserialize_fn(mut self, deserialize_fn: DeserializeFn) -> Self {
self.default_deserialize = deserialize_fn;
self
}
/// Builds the plugin.
///
@@ -249,56 +407,37 @@ impl<R: Runtime> Builder<R> {
/// tauri::Builder::default()
/// .plugin(tauri_plugin_store::Builder::default().build())
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build();
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin").build()?;
/// Ok(())
/// });
/// ```
pub fn build(mut self) -> TauriPlugin<R> {
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
plugin::Builder::new("store")
.invoke_handler(tauri::generate_handler![
create_store,
set,
get,
has,
delete,
clear,
reset,
keys,
values,
length,
entries,
load,
save
load, get_store, set, get, has, delete, clear, reset, keys, values, length,
entries, reload, save,
])
.setup(move |app_handle, _api| {
for (path, store) in self.stores.iter_mut() {
// ignore loading errors, just use the default
if let Err(err) = store.load() {
warn!(
"Failed to load store {path:?} from disk: {err}. Falling back to default values."
);
}
}
app_handle.manage(StoreCollection::<R> {
stores: Mutex::new(HashMap::new()),
// frozen: self.frozen,
app_handle.manage(StoreState {
stores: Arc::new(Mutex::new(HashMap::new())),
serialize_fns: self.serialize_fns,
deserialize_fns: self.deserialize_fns,
default_serialize: self.default_serialize,
default_deserialize: self.default_deserialize,
});
Ok(())
})
.on_event(|_app_handle, event| {
.on_event(|app_handle, event| {
if let RunEvent::Exit = event {
// let collection = app_handle.state::<StoreCollection<R>>();
// for store in collection.stores.lock().expect("mutex poisoned").values_mut() {
// if let Some(sender) = store.auto_save_debounce_sender.take() {
// let _ = sender.send(AutoSaveMessage::Cancel);
// }
// if let Err(err) = store.save() {
// eprintln!("failed to save store {:?} with error {:?}", store.path, err);
// }
// }
let collection = app_handle.state::<StoreState>();
let stores = collection.stores.lock().unwrap();
for (path, rid) in stores.iter() {
if let Ok(store) = app_handle.resources_table().get::<Store<R>>(*rid) {
if let Err(err) = store.save() {
log::error!("failed to save store {path:?} with error {err:?}");
}
}
}
}
})
.build()
+216 -122
View File
@@ -2,38 +2,32 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{ChangePayload, StoreCollection};
use crate::{ChangePayload, StoreState};
use serde_json::Value as JsonValue;
use std::{
collections::HashMap,
fs::{create_dir_all, read, File},
io::Write,
fs,
path::{Path, PathBuf},
sync::{Arc, Mutex},
time::Duration,
};
use tauri::{AppHandle, Emitter, Manager, Resource, Runtime};
use tauri::{path::BaseDirectory, AppHandle, Emitter, Manager, Resource, ResourceId, Runtime};
use tokio::{
select,
sync::mpsc::{unbounded_channel, UnboundedSender},
time::sleep,
};
type SerializeFn =
pub type SerializeFn =
fn(&HashMap<String, JsonValue>) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>>;
pub(crate) type DeserializeFn =
pub type DeserializeFn =
fn(&[u8]) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>>;
fn default_serialize(
cache: &HashMap<String, JsonValue>,
) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
Ok(serde_json::to_vec(&cache)?)
}
fn default_deserialize(
bytes: &[u8],
) -> Result<HashMap<String, JsonValue>, Box<dyn std::error::Error + Send + Sync>> {
serde_json::from_slice(bytes).map_err(Into::into)
pub fn resolve_store_path<R: Runtime>(
app: &AppHandle<R>,
path: impl AsRef<Path>,
) -> crate::Result<PathBuf> {
Ok(dunce::simplified(&app.path().resolve(path, BaseDirectory::AppData)?).to_path_buf())
}
/// Builds a [`Store`]
@@ -41,10 +35,10 @@ pub struct StoreBuilder<R: Runtime> {
app: AppHandle<R>,
path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>,
cache: HashMap<String, JsonValue>,
serialize: SerializeFn,
deserialize: DeserializeFn,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
auto_save: Option<Duration>,
create_new: bool,
}
impl<R: Runtime> StoreBuilder<R> {
@@ -60,15 +54,18 @@ impl<R: Runtime> StoreBuilder<R> {
/// });
/// ```
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
let app = manager.app_handle().clone();
let state = app.state::<StoreState>();
let serialize_fn = state.default_serialize;
let deserialize_fn = state.default_deserialize;
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.
path: dunce::simplified(path.as_ref()).to_path_buf(),
app,
path: path.as_ref().to_path_buf(),
defaults: None,
cache: Default::default(),
serialize: default_serialize,
deserialize: default_deserialize,
auto_save: None,
serialize_fn,
deserialize_fn,
auto_save: Some(Duration::from_millis(100)),
create_new: false,
}
}
@@ -84,17 +81,16 @@ impl<R: Runtime> StoreBuilder<R> {
///
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .defaults(defaults)
/// .build();
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn defaults(mut self, defaults: HashMap<String, JsonValue>) -> Self {
self.cache.clone_from(&defaults);
self.defaults = Some(defaults);
self
}
/// Inserts multiple key-value pairs.
/// Inserts multiple default key-value pairs.
///
/// # Examples
/// ```
@@ -103,14 +99,13 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.bin")
/// .default("foo".to_string(), "bar")
/// .build();
/// .build()?;
/// Ok(())
/// });
/// ```
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.defaults
.get_or_insert(HashMap::new())
.insert(key, value);
@@ -126,12 +121,12 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .serialize(|cache| serde_json::to_vec(&cache).map_err(Into::into))
/// .build();
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn serialize(mut self, serialize: SerializeFn) -> Self {
self.serialize = serialize;
self.serialize_fn = serialize;
self
}
@@ -144,28 +139,25 @@ impl<R: Runtime> StoreBuilder<R> {
/// .setup(|app| {
/// let store = tauri_plugin_store::StoreBuilder::new(app, "store.json")
/// .deserialize(|bytes| serde_json::from_slice(&bytes).map_err(Into::into))
/// .build();
/// .build()?;
/// Ok(())
/// });
/// ```
pub fn deserialize(mut self, deserialize: DeserializeFn) -> Self {
self.deserialize = deserialize;
self.deserialize_fn = deserialize;
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();
/// .build()?;
/// Ok(())
/// });
/// ```
@@ -174,7 +166,64 @@ impl<R: Runtime> StoreBuilder<R> {
self
}
/// Builds the [`Store`].
/// Disable auto save on modified with a debounce duration.
pub fn disable_auto_save(mut self) -> Self {
self.auto_save = None;
self
}
/// Force create a new store with default values even if it already exists.
pub fn create_new(mut self) -> Self {
self.create_new = true;
self
}
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let stores = self.app.state::<StoreState>().stores.clone();
let mut stores = stores.lock().unwrap();
self.path = resolve_store_path(&self.app, self.path)?;
if self.create_new {
if let Some(rid) = stores.remove(&self.path) {
let _ = self.app.resources_table().take::<Store<R>>(rid);
}
} else if let Some(rid) = stores.get(&self.path) {
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
}
// if stores.contains_key(&self.path) {
// return Err(crate::Error::AlreadyExists(self.path));
// }
let mut store_inner = StoreInner::new(
self.app.clone(),
self.path.clone(),
self.defaults.take(),
self.serialize_fn,
self.deserialize_fn,
);
if !self.create_new {
let _ = store_inner.load();
}
let store = Store {
auto_save: self.auto_save,
auto_save_debounce_sender: Arc::new(Mutex::new(None)),
store: Arc::new(Mutex::new(store_inner)),
};
let store = Arc::new(store);
let rid = self.app.resources_table().add_arc(store.clone());
stores.insert(self.path, rid);
Ok((store, rid))
}
/// Load the existing store with the same path or creates a new [`Store`].
///
/// If a store with the same path has already been loaded its instance is returned.
///
/// # Examples
/// ```
@@ -185,131 +234,116 @@ impl<R: Runtime> StoreBuilder<R> {
/// Ok(())
/// });
/// ```
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 {
defaults: self.defaults,
serialize: self.serialize,
deserialize: self.deserialize,
auto_save: self.auto_save,
auto_save_debounce_sender: Arc::new(Mutex::new(None)),
store,
}
pub fn build(self) -> crate::Result<Arc<Store<R>>> {
let (store, _) = self.build_inner()?;
Ok(store)
}
}
pub(crate) enum AutoSaveMessage {
enum AutoSaveMessage {
Reset,
Cancel,
}
#[derive(Clone)]
pub struct StoreInner<R: Runtime> {
pub(crate) app: AppHandle<R>,
pub(crate) path: PathBuf,
pub(crate) cache: HashMap<String, JsonValue>,
struct StoreInner<R: Runtime> {
app: AppHandle<R>,
path: PathBuf,
cache: HashMap<String, JsonValue>,
defaults: Option<HashMap<String, JsonValue>>,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
}
impl<R: Runtime> StoreInner<R> {
pub fn new(app: AppHandle<R>, path: PathBuf) -> Self {
fn new(
app: AppHandle<R>,
path: PathBuf,
defaults: Option<HashMap<String, JsonValue>>,
serialize_fn: SerializeFn,
deserialize_fn: DeserializeFn,
) -> Self {
Self {
app,
path,
cache: HashMap::new(),
cache: defaults.clone().unwrap_or_default(),
defaults,
serialize_fn,
deserialize_fn,
}
}
pub fn save(&self, serialize_fn: SerializeFn) -> crate::Result<()> {
let app_dir = self
.app
.path()
.app_data_dir()
.expect("failed to resolve app dir");
let store_path = app_dir.join(&self.path);
/// Saves the store to disk at the store's `path`.
pub fn save(&self) -> crate::Result<()> {
fs::create_dir_all(self.path.parent().expect("invalid store path"))?;
create_dir_all(store_path.parent().expect("invalid store path"))?;
let bytes = serialize_fn(&self.cache).map_err(crate::Error::Serialize)?;
let mut f = File::create(&store_path)?;
f.write_all(&bytes)?;
let bytes = (self.serialize_fn)(&self.cache).map_err(crate::Error::Serialize)?;
fs::write(&self.path, bytes)?;
Ok(())
}
/// Update the store from the on-disk state
pub fn load(&mut self, deserialize_fn: DeserializeFn) -> crate::Result<()> {
let app_dir = self
.app
.path()
.app_data_dir()
.expect("failed to resolve app dir");
let store_path = app_dir.join(&self.path);
let bytes = read(store_path)?;
pub fn load(&mut self) -> crate::Result<()> {
let bytes = fs::read(&self.path)?;
self.cache
.extend(deserialize_fn(&bytes).map_err(crate::Error::Deserialize)?);
.extend((self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?);
Ok(())
}
pub fn insert(&mut self, key: impl Into<String>, value: impl Into<JsonValue>) {
/// Inserts a key-value pair into the store.
pub fn set(&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());
let _ = self.emit_change_event(&key, &value);
let _ = self.emit_change_event(&key, Some(&value));
}
/// Returns a reference to the value corresponding to the key.
pub fn get(&self, key: impl AsRef<str>) -> Option<&JsonValue> {
self.cache.get(key.as_ref())
}
/// Returns `true` if the given `key` exists in the store.
pub fn has(&self, key: impl AsRef<str>) -> bool {
self.cache.contains_key(key.as_ref())
}
/// Removes a key-value pair from the store.
pub fn delete(&mut self, key: impl AsRef<str>) -> bool {
let flag = self.cache.remove(key.as_ref()).is_some();
if flag {
let _ = self.emit_change_event(key.as_ref(), &JsonValue::Null);
let _ = self.emit_change_event(key.as_ref(), None);
}
flag
}
/// Clears the store, removing all key-value pairs.
///
/// Note: To clear the storage and reset it to its `default` value, use [`reset`](Self::reset) instead.
pub fn clear(&mut self) {
let keys: Vec<String> = self.cache.keys().cloned().collect();
self.cache.clear();
for key in &keys {
let _ = self.emit_change_event(key, &JsonValue::Null);
let _ = self.emit_change_event(key, None);
}
}
pub fn reset(&mut self, defaults: &Option<HashMap<String, JsonValue>>) {
if let Some(defaults) = &defaults {
/// Resets the store to its `default` value.
///
/// If no default value has been set, this method behaves identical to [`clear`](Self::clear).
pub fn reset(&mut self) {
if let Some(defaults) = &self.defaults {
for (key, value) in &self.cache {
if defaults.get(key) != Some(value) {
let _ =
self.emit_change_event(key, defaults.get(key).unwrap_or(&JsonValue::Null));
let _ = self.emit_change_event(key, defaults.get(key));
}
}
for (key, value) in defaults {
if !self.cache.contains_key(key) {
let _ = self.emit_change_event(key, value);
let _ = self.emit_change_event(key, Some(value));
}
}
self.cache.clone_from(defaults);
@@ -318,33 +352,43 @@ impl<R: Runtime> StoreInner<R> {
}
}
/// An iterator visiting all keys in arbitrary order.
pub fn keys(&self) -> impl Iterator<Item = &String> {
self.cache.keys()
}
/// An iterator visiting all values in arbitrary order.
pub fn values(&self) -> impl Iterator<Item = &JsonValue> {
self.cache.values()
}
/// An iterator visiting all key-value pairs in arbitrary order.
pub fn entries(&self) -> impl Iterator<Item = (&String, &JsonValue)> {
self.cache.iter()
}
/// Returns the number of elements in the store.
pub fn len(&self) -> usize {
self.cache.len()
}
/// Returns true if the store contains no elements.
pub fn is_empty(&self) -> bool {
self.cache.is_empty()
}
fn emit_change_event(&self, key: &str, value: &JsonValue) -> crate::Result<()> {
fn emit_change_event(&self, key: &str, value: Option<&JsonValue>) -> crate::Result<()> {
let state = self.app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
let exists = value.is_some();
self.app.emit(
"store://change",
ChangePayload {
path: &self.path,
resource_id: stores.get(&self.path).copied(),
key,
value,
exists,
},
)?;
Ok(())
@@ -361,38 +405,45 @@ impl<R: Runtime> std::fmt::Debug for StoreInner<R> {
}
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> Resource for Store<R> {
fn close(self: Arc<Self>) {
let store = self.store.lock().unwrap();
let state = store.app.state::<StoreState>();
let mut stores = state.stores.lock().unwrap();
stores.remove(&store.path);
}
}
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)
}
// /// Do something with the inner store,
// /// useful for batching some work if you need higher performance
// pub fn with_store<T>(&self, f: impl FnOnce(&mut StoreInner<R>) -> T) -> T {
// let mut store = self.store.lock().unwrap();
// f(&mut store)
// }
/// Inserts a key-value pair into the store.
pub fn set(&self, key: impl Into<String>, value: impl Into<JsonValue>) {
self.store.lock().unwrap().insert(key.into(), value.into());
self.store.lock().unwrap().set(key.into(), value.into());
let _ = self.trigger_auto_save();
}
/// Returns the value for the given `key` or `None` if the key does not exist.
pub fn get(&self, key: impl AsRef<str>) -> Option<JsonValue> {
self.store.lock().unwrap().get(key).cloned()
}
/// Returns `true` if the given `key` exists in the store.
pub fn has(&self, key: impl AsRef<str>) -> bool {
self.store.lock().unwrap().has(key)
}
/// Removes a key-value pair from the store.
pub fn delete(&self, key: impl AsRef<str>) -> bool {
let deleted = self.store.lock().unwrap().delete(key);
if deleted {
@@ -401,24 +452,33 @@ impl<R: Runtime> Store<R> {
deleted
}
/// Clears the store, removing all key-value pairs.
///
/// Note: To clear the storage and reset it to its `default` value, use [`reset`](Self::reset) instead.
pub fn clear(&self) {
self.store.lock().unwrap().clear();
let _ = self.trigger_auto_save();
}
/// Resets the store to its `default` value.
///
/// If no default value has been set, this method behaves identical to [`clear`](Self::clear).
pub fn reset(&self) {
self.store.lock().unwrap().reset(&self.defaults);
self.store.lock().unwrap().reset();
let _ = self.trigger_auto_save();
}
/// Returns a list of all keys in the store.
pub fn keys(&self) -> Vec<String> {
self.store.lock().unwrap().keys().cloned().collect()
}
/// Returns a list of all values in the store.
pub fn values(&self) -> Vec<JsonValue> {
self.store.lock().unwrap().values().cloned().collect()
}
/// Returns a list of all key-value pairs in the store.
pub fn entries(&self) -> Vec<(String, JsonValue)> {
self.store
.lock()
@@ -428,19 +488,40 @@ impl<R: Runtime> Store<R> {
.collect()
}
/// Returns the number of elements in the store.
pub fn length(&self) -> usize {
self.store.lock().unwrap().len()
}
pub fn load(&self) -> crate::Result<()> {
self.store.lock().unwrap().load(self.deserialize)
/// Returns true if the store contains no elements.
pub fn is_empty(&self) -> bool {
self.store.lock().unwrap().is_empty()
}
/// Update the store from the on-disk state
pub fn reload(&self) -> crate::Result<()> {
self.store.lock().unwrap().load()
}
/// Saves the store to disk at the store's `path`.
pub fn save(&self) -> crate::Result<()> {
if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() {
let _ = sender.send(AutoSaveMessage::Cancel);
}
self.store.lock().unwrap().save(self.serialize)
self.store.lock().unwrap().save()
}
/// Removes the store from the resource table
pub fn close_resource(&self) {
let store = self.store.lock().unwrap();
let app = store.app.clone();
let state = app.state::<StoreState>();
let stores = state.stores.lock().unwrap();
if let Some(rid) = stores.get(&store.path).copied() {
drop(store);
drop(stores);
let _ = app.resources_table().close(rid);
}
}
fn trigger_auto_save(&self) -> crate::Result<()> {
@@ -459,7 +540,6 @@ impl<R: Runtime> Store<R> {
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 {
@@ -471,7 +551,7 @@ impl<R: Runtime> Store<R> {
}
_ = sleep(auto_save_delay) => {
auto_save_debounce_sender.lock().unwrap().take();
let _ = store.lock().unwrap().save(serialize_fn);
let _ = store.lock().unwrap().save();
return;
}
};
@@ -479,4 +559,18 @@ impl<R: Runtime> Store<R> {
});
Ok(())
}
fn apply_pending_auto_save(&self) {
// Cancel and save if auto save is pending
if let Some(sender) = self.auto_save_debounce_sender.lock().unwrap().take() {
let _ = sender.send(AutoSaveMessage::Cancel);
let _ = self.save();
};
}
}
impl<R: Runtime> Drop for Store<R> {
fn drop(&mut self) {
self.apply_pending_auto_save();
}
}