diff --git a/plugins/store/guest-js/index.ts b/plugins/store/guest-js/index.ts index 28f64f12..e9c0a77c 100644 --- a/plugins/store/guest-js/index.ts +++ b/plugins/store/guest-js/index.ts @@ -49,8 +49,6 @@ export type StoreOptions = { * * @param path Path to save the store in `app_data_dir` * @param options Store configuration options - * - * @throws If a store at that path is already loaded */ export async function create( path: string, @@ -75,8 +73,6 @@ export async function create( * * @param path Path to save the store in `app_data_dir` * @param options Store configuration options - * - * @throws If a store at that path is already loaded */ export async function load( path: string, @@ -232,8 +228,6 @@ export class Store extends Resource implements IStore { * * @param path Path to save the store in `app_data_dir` * @param options Store configuration options - * - * @throws If a store at that path is already loaded */ static async create(path: string, options?: StoreOptions): Promise { const rid = await invoke('plugin:store|create_store', { @@ -259,8 +253,6 @@ export class Store extends Resource implements IStore { * * @param path Path to save the store in `app_data_dir` * @param options Store configuration options - * - * @throws If a store at that path is already loaded */ static async load(path: string, options?: StoreOptions): Promise { const rid = await invoke('plugin:store|load', { diff --git a/plugins/store/permissions/autogenerated/reference.md b/plugins/store/permissions/autogenerated/reference.md index 3b36c3ed..4260d3f8 100644 --- a/plugins/store/permissions/autogenerated/reference.md +++ b/plugins/store/permissions/autogenerated/reference.md @@ -23,7 +23,7 @@ All operations are enabled by default. - `allow-values` - `allow-entries` - `allow-length` -- `allow-load` +- `allow-reload` - `allow-save` ## Permission Table diff --git a/plugins/store/src/error.rs b/plugins/store/src/error.rs index 77526fbc..ef5ee593 100644 --- a/plugins/store/src/error.rs +++ b/plugins/store/src/error.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: MIT use serde::{Serialize, Serializer}; -use std::path::PathBuf; pub type Result = std::result::Result; @@ -21,9 +20,9 @@ pub enum Error { /// IO error. #[error(transparent)] Io(#[from] std::io::Error), - /// Store already exists - #[error("Store at \"{0}\" already exists")] - AlreadyExists(PathBuf), + // /// Store already exists + // #[error("Store at \"{0}\" already exists")] + // AlreadyExists(PathBuf), /// Serialize function not found #[error("Serialize Function \"{0}\" not found")] SerializeFunctionNotFound(String), diff --git a/plugins/store/src/store.rs b/plugins/store/src/store.rs index 9111aa2d..72af4a02 100644 --- a/plugins/store/src/store.rs +++ b/plugins/store/src/store.rs @@ -172,7 +172,7 @@ impl StoreBuilder { self } - /// Force create a new store even if it already exists. + /// 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 @@ -181,14 +181,23 @@ impl StoreBuilder { pub(crate) fn build_inner(mut self) -> crate::Result<(Arc>, ResourceId)> { let stores = self.app.state::().stores.clone(); let mut stores = stores.lock().unwrap(); - 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)); + 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::>(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(),