Fix js get and close store

This commit is contained in:
Tony
2024-10-03 20:10:15 +08:00
parent 718f5dc90d
commit 4f13a03401
13 changed files with 171 additions and 22 deletions
+11 -3
View File
@@ -21,6 +21,7 @@ use std::{
sync::{Arc, Mutex},
time::Duration,
};
use store::resolve_store_path;
pub use store::{DeserializeFn, SerializeFn, Store, StoreBuilder, StoreInner};
use tauri::{
plugin::{self, TauriPlugin},
@@ -96,12 +97,18 @@ async fn create_store<R: Runtime>(
}
#[tauri::command]
async fn get_store(
async fn get_store<R: Runtime>(
app: AppHandle<R>,
store_collection: State<'_, StoreCollection>,
path: PathBuf,
) -> Result<Option<ResourceId>> {
let stores = store_collection.stores.lock().unwrap();
Ok(stores.get(&path).copied())
Ok(stores.get(&resolve_store_path(app, path)).copied())
}
#[tauri::command]
async fn close_store<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
Ok(app.resources_table().close(rid)?)
}
#[tauri::command]
@@ -275,6 +282,7 @@ impl<R: Runtime> Builder<R> {
.invoke_handler(tauri::generate_handler![
create_store,
get_store,
close_store,
set,
get,
has,
@@ -286,7 +294,7 @@ impl<R: Runtime> Builder<R> {
length,
entries,
load,
save
save,
])
.setup(move |app_handle, _api| {
app_handle.manage(StoreCollection {
+7 -4
View File
@@ -36,6 +36,12 @@ fn default_deserialize(
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)
.expect("failed to resolve app dir")
}
/// Builds a [`Store`]
pub struct StoreBuilder<R: Runtime> {
app: AppHandle<R>,
@@ -60,10 +66,7 @@ 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 = app
.path()
.resolve(path, BaseDirectory::AppData)
.expect("failed to resolve app dir");
let path = resolve_store_path(app.clone(), path);
Self {
app,
// Since Store.path is only exposed to the user in emit calls we may as well simplify it here already.