mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-23 11:36:13 +02:00
Add default (de)serialize fn
This commit is contained in:
@@ -40,10 +40,13 @@ struct ChangePayload<'a> {
|
||||
value: &'a JsonValue,
|
||||
}
|
||||
|
||||
pub struct StoreCollection {
|
||||
#[derive(Debug)]
|
||||
pub struct StoreState {
|
||||
stores: Mutex<HashMap<PathBuf, ResourceId>>,
|
||||
serialize_fns: HashMap<String, SerializeFn>,
|
||||
deserialize_fns: HashMap<String, DeserializeFn>,
|
||||
default_serialize: SerializeFn,
|
||||
default_deserialize: DeserializeFn,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -56,7 +59,7 @@ enum AutoSave {
|
||||
#[tauri::command]
|
||||
async fn create_store<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
store_collection: State<'_, StoreCollection>,
|
||||
store_state: State<'_, StoreState>,
|
||||
path: PathBuf,
|
||||
auto_save: Option<AutoSave>,
|
||||
serialize_fn_name: Option<String>,
|
||||
@@ -77,7 +80,7 @@ async fn create_store<R: Runtime>(
|
||||
}
|
||||
|
||||
if let Some(serialize_fn_name) = serialize_fn_name {
|
||||
let serialize_fn = store_collection
|
||||
let serialize_fn = store_state
|
||||
.serialize_fns
|
||||
.get(&serialize_fn_name)
|
||||
.ok_or_else(|| crate::Error::SerializeFunctionNotFound(serialize_fn_name))?;
|
||||
@@ -85,7 +88,7 @@ async fn create_store<R: Runtime>(
|
||||
}
|
||||
|
||||
if let Some(deserialize_fn_name) = deserialize_fn_name {
|
||||
let deserialize_fn = store_collection
|
||||
let deserialize_fn = store_state
|
||||
.deserialize_fns
|
||||
.get(&deserialize_fn_name)
|
||||
.ok_or_else(|| crate::Error::DeserializeFunctionNotFound(deserialize_fn_name))?;
|
||||
@@ -99,10 +102,10 @@ async fn create_store<R: Runtime>(
|
||||
#[tauri::command]
|
||||
async fn get_store<R: Runtime>(
|
||||
app: AppHandle<R>,
|
||||
store_collection: State<'_, StoreCollection>,
|
||||
store_state: State<'_, StoreState>,
|
||||
path: PathBuf,
|
||||
) -> Result<Option<ResourceId>> {
|
||||
let stores = store_collection.stores.lock().unwrap();
|
||||
let stores = store_state.stores.lock().unwrap();
|
||||
Ok(stores.get(&resolve_store_path(app, path)).copied())
|
||||
}
|
||||
|
||||
@@ -224,7 +227,7 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
||||
}
|
||||
|
||||
fn get_store(&self, path: impl AsRef<Path>) -> Option<Arc<Store<R>>> {
|
||||
let collection = self.state::<StoreCollection>();
|
||||
let collection = self.state::<StoreState>();
|
||||
let stores = collection.stores.lock().unwrap();
|
||||
stores
|
||||
.get(path.as_ref())
|
||||
@@ -232,10 +235,24 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
||||
}
|
||||
}
|
||||
|
||||
fn default_serialize(
|
||||
cache: &HashMap<String, JsonValue>,
|
||||
) -> std::result::Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(serde_json::to_vec(&cache)?)
|
||||
}
|
||||
|
||||
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<R: Runtime> {
|
||||
phantom_data: PhantomData<R>,
|
||||
serialize_fns: HashMap<String, SerializeFn>,
|
||||
deserialize_fns: HashMap<String, DeserializeFn>,
|
||||
default_serialize: SerializeFn,
|
||||
default_deserialize: DeserializeFn,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for Builder<R> {
|
||||
@@ -244,6 +261,8 @@ impl<R: Runtime> Default for Builder<R> {
|
||||
phantom_data: Default::default(),
|
||||
serialize_fns: Default::default(),
|
||||
deserialize_fns: Default::default(),
|
||||
default_serialize,
|
||||
default_deserialize,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -282,6 +301,18 @@ impl<R: Runtime> Builder<R> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Use this serialize function for stores by default
|
||||
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.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -314,16 +345,18 @@ impl<R: Runtime> Builder<R> {
|
||||
save,
|
||||
])
|
||||
.setup(move |app_handle, _api| {
|
||||
app_handle.manage(StoreCollection {
|
||||
app_handle.manage(StoreState {
|
||||
stores: 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| {
|
||||
if let RunEvent::Exit = event {
|
||||
let collection = app_handle.state::<StoreCollection>();
|
||||
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) {
|
||||
|
||||
+25
-24
@@ -2,7 +2,7 @@
|
||||
// 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,
|
||||
@@ -24,18 +24,6 @@ pub type SerializeFn =
|
||||
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(crate) fn resolve_store_path<R: Runtime>(app: AppHandle<R>, path: impl AsRef<Path>) -> PathBuf {
|
||||
app.path()
|
||||
.resolve(path, BaseDirectory::AppData)
|
||||
@@ -50,6 +38,7 @@ pub struct StoreBuilder<R: Runtime> {
|
||||
serialize_fn: SerializeFn,
|
||||
deserialize_fn: DeserializeFn,
|
||||
auto_save: Option<Duration>,
|
||||
load_on_build: bool,
|
||||
}
|
||||
|
||||
impl<R: Runtime> StoreBuilder<R> {
|
||||
@@ -67,14 +56,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 path = resolve_store_path(app.clone(), path);
|
||||
let state = app.state::<StoreState>();
|
||||
let serialize_fn = state.default_serialize;
|
||||
let deserialize_fn = state.default_deserialize;
|
||||
Self {
|
||||
app,
|
||||
// Since Store.path is only exposed to the user in emit calls we may as well simplify it here already.
|
||||
path: dunce::simplified(&path).to_path_buf(),
|
||||
defaults: None,
|
||||
serialize_fn: default_serialize,
|
||||
deserialize_fn: default_deserialize,
|
||||
serialize_fn,
|
||||
deserialize_fn,
|
||||
auto_save: Some(Duration::from_millis(100)),
|
||||
load_on_build: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,9 +174,15 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Skip loading the store on build
|
||||
pub fn skip_initial_load(mut self) -> Self {
|
||||
self.load_on_build = false;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||
let collection = self.app.state::<StoreCollection>();
|
||||
let mut stores = collection.stores.lock().unwrap();
|
||||
let state = self.app.state::<StoreState>();
|
||||
let mut stores = state.stores.lock().unwrap();
|
||||
|
||||
if stores.contains_key(&self.path) {
|
||||
return Err(crate::Error::AlreadyExists(self.path));
|
||||
@@ -196,7 +195,9 @@ impl<R: Runtime> StoreBuilder<R> {
|
||||
self.serialize_fn,
|
||||
self.deserialize_fn,
|
||||
);
|
||||
let _ = store_inner.load();
|
||||
if self.load_on_build {
|
||||
let _ = store_inner.load();
|
||||
}
|
||||
|
||||
let store = Store {
|
||||
auto_save: self.auto_save,
|
||||
@@ -376,8 +377,8 @@ impl<R: Runtime> StoreInner<R> {
|
||||
}
|
||||
|
||||
fn emit_change_event(&self, key: &str, value: &JsonValue) -> crate::Result<()> {
|
||||
let collection = self.app.state::<StoreCollection>();
|
||||
let stores = collection.stores.lock().unwrap();
|
||||
let state = self.app.state::<StoreState>();
|
||||
let stores = state.stores.lock().unwrap();
|
||||
self.app.emit(
|
||||
"store://change",
|
||||
ChangePayload {
|
||||
@@ -409,8 +410,8 @@ pub struct Store<R: Runtime> {
|
||||
impl<R: Runtime> Resource for Store<R> {
|
||||
fn close(self: Arc<Self>) {
|
||||
let store = self.store.lock().unwrap();
|
||||
let collection = store.app.state::<StoreCollection>();
|
||||
let mut stores = collection.stores.lock().unwrap();
|
||||
let state = store.app.state::<StoreState>();
|
||||
let mut stores = state.stores.lock().unwrap();
|
||||
stores.remove(&store.path);
|
||||
}
|
||||
}
|
||||
@@ -508,8 +509,8 @@ impl<R: Runtime> Store<R> {
|
||||
pub fn close_store(self) {
|
||||
let store = self.store.lock().unwrap();
|
||||
let app = store.app.clone();
|
||||
let collection = app.state::<StoreCollection>();
|
||||
let stores = collection.stores.lock().unwrap();
|
||||
let state = app.state::<StoreState>();
|
||||
let stores = state.stores.lock().unwrap();
|
||||
if let Some(rid) = stores.get(&store.path).copied() {
|
||||
drop(store);
|
||||
drop(stores);
|
||||
|
||||
Reference in New Issue
Block a user