mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-26 17:31:06 +02:00
Return result in resolve_store_path
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
- Renamed `StoreCollection` to `StoreState`
|
- Renamed `StoreCollection` to `StoreState`
|
||||||
- `StoreBuilder::build` now returns a `Result`
|
- `StoreBuilder::build` now returns a `Result`
|
||||||
- `StoreExt::store` now returns `Arc<Store>` instead of `Store`
|
- `StoreExt::store` now returns `Result<Arc<Store>>`
|
||||||
|
|
||||||
Other Changes:
|
Other Changes:
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ fn main() {
|
|||||||
.plugin(tauri_plugin_store::Builder::new().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 store = app.store("settings.json");
|
let store = app.store("settings.json")?;
|
||||||
app.listen("store://change", |event| {
|
app.listen("store://change", |event| {
|
||||||
dbg!(event);
|
dbg!(event);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ async fn create_or_existing_store<R: Runtime>(
|
|||||||
serialize_fn_name,
|
serialize_fn_name,
|
||||||
deserialize_fn_name,
|
deserialize_fn_name,
|
||||||
)?;
|
)?;
|
||||||
let (_, rid) = builder.build_or_existing_inner();
|
let (_, rid) = builder.build_or_existing_inner()?;
|
||||||
Ok(rid)
|
Ok(rid)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ async fn get_store<R: Runtime>(
|
|||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
) -> Result<Option<ResourceId>> {
|
) -> Result<Option<ResourceId>> {
|
||||||
let stores = store_state.stores.lock().unwrap();
|
let stores = store_state.stores.lock().unwrap();
|
||||||
Ok(stores.get(&resolve_store_path(&app, path)).copied())
|
Ok(stores.get(&resolve_store_path(&app, path)?).copied())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@@ -244,7 +244,7 @@ async fn save<R: Runtime>(app: AppHandle<R>, rid: ResourceId) -> Result<()> {
|
|||||||
|
|
||||||
pub trait StoreExt<R: Runtime> {
|
pub trait StoreExt<R: Runtime> {
|
||||||
/// Create a store or get an existing store with default settings at path
|
/// Create a store or get an existing store with default settings at path
|
||||||
fn store(&self, path: impl AsRef<Path>) -> Arc<Store<R>>;
|
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
|
||||||
/// Create a store with default settings
|
/// Create a store with default settings
|
||||||
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
|
fn create_store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>>;
|
||||||
/// Get a store builder
|
/// Get a store builder
|
||||||
@@ -254,7 +254,7 @@ pub trait StoreExt<R: Runtime> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
||||||
fn store(&self, path: impl AsRef<Path>) -> Arc<Store<R>> {
|
fn store(&self, path: impl AsRef<Path>) -> Result<Arc<Store<R>>> {
|
||||||
StoreBuilder::new(self.app_handle(), path).build_or_existing()
|
StoreBuilder::new(self.app_handle(), path).build_or_existing()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +270,7 @@ impl<R: Runtime, T: Manager<R>> StoreExt<R> for T {
|
|||||||
let collection = self.state::<StoreState>();
|
let collection = self.state::<StoreState>();
|
||||||
let stores = collection.stores.lock().unwrap();
|
let stores = collection.stores.lock().unwrap();
|
||||||
stores
|
stores
|
||||||
.get(&resolve_store_path(self.app_handle(), path.as_ref()))
|
.get(&resolve_store_path(self.app_handle(), path.as_ref()).ok()?)
|
||||||
.and_then(|rid| self.resources_table().get(*rid).ok())
|
.and_then(|rid| self.resources_table().get(*rid).ok())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-39
@@ -27,13 +27,8 @@ pub type DeserializeFn =
|
|||||||
pub(crate) fn resolve_store_path<R: Runtime>(
|
pub(crate) fn resolve_store_path<R: Runtime>(
|
||||||
app: &AppHandle<R>,
|
app: &AppHandle<R>,
|
||||||
path: impl AsRef<Path>,
|
path: impl AsRef<Path>,
|
||||||
) -> PathBuf {
|
) -> crate::Result<PathBuf> {
|
||||||
dunce::simplified(
|
Ok(dunce::simplified(&app.path().resolve(path, BaseDirectory::AppData)?).to_path_buf())
|
||||||
&app.path()
|
|
||||||
.resolve(path, BaseDirectory::AppData)
|
|
||||||
.expect("failed to resolve app dir"),
|
|
||||||
)
|
|
||||||
.to_path_buf()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds a [`Store`]
|
/// Builds a [`Store`]
|
||||||
@@ -61,13 +56,12 @@ impl<R: Runtime> StoreBuilder<R> {
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
|
pub fn new<M: Manager<R>, P: AsRef<Path>>(manager: &M, path: P) -> Self {
|
||||||
let app = manager.app_handle().clone();
|
let app = manager.app_handle().clone();
|
||||||
let path = resolve_store_path(&app, path);
|
|
||||||
let state = app.state::<StoreState>();
|
let state = app.state::<StoreState>();
|
||||||
let serialize_fn = state.default_serialize;
|
let serialize_fn = state.default_serialize;
|
||||||
let deserialize_fn = state.default_deserialize;
|
let deserialize_fn = state.default_deserialize;
|
||||||
Self {
|
Self {
|
||||||
app,
|
app,
|
||||||
path,
|
path: path.as_ref().to_path_buf(),
|
||||||
defaults: None,
|
defaults: None,
|
||||||
serialize_fn,
|
serialize_fn,
|
||||||
deserialize_fn,
|
deserialize_fn,
|
||||||
@@ -217,35 +211,15 @@ impl<R: Runtime> StoreBuilder<R> {
|
|||||||
Ok((store, rid))
|
Ok((store, rid))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build_or_existing_inner(mut self) -> (Arc<Store<R>>, ResourceId) {
|
pub(crate) fn build_or_existing_inner(self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
|
||||||
let state = self.app.state::<StoreState>();
|
{
|
||||||
let mut stores = state.stores.lock().unwrap();
|
let state = self.app.state::<StoreState>();
|
||||||
|
let stores = state.stores.lock().unwrap();
|
||||||
if let Some(rid) = stores.get(&self.path) {
|
if let Some(rid) = stores.get(&self.path) {
|
||||||
(self.app.resources_table().get(*rid).unwrap(), *rid)
|
return Ok((self.app.resources_table().get(*rid).unwrap(), *rid));
|
||||||
} else {
|
|
||||||
let mut store_inner = StoreInner::new(
|
|
||||||
self.app.clone(),
|
|
||||||
self.path.clone(),
|
|
||||||
self.defaults.take(),
|
|
||||||
self.serialize_fn,
|
|
||||||
self.deserialize_fn,
|
|
||||||
);
|
|
||||||
if self.load_on_build {
|
|
||||||
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);
|
|
||||||
(store, rid)
|
|
||||||
}
|
}
|
||||||
|
self.build_inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
|
/// Builds the [`Store`], also see [`build_or_existing`](Self::build_or_existing).
|
||||||
@@ -283,9 +257,9 @@ impl<R: Runtime> StoreBuilder<R> {
|
|||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
pub fn build_or_existing(self) -> Arc<Store<R>> {
|
pub fn build_or_existing(self) -> crate::Result<Arc<Store<R>>> {
|
||||||
let (store, _) = self.build_or_existing_inner();
|
let (store, _) = self.build_or_existing_inner()?;
|
||||||
store
|
Ok(store)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user