Fix description and create_new logic

pull/1860/head
Tony 8 months ago
parent 3288e4bd74
commit 99cafa6d73
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -49,8 +49,6 @@ export type StoreOptions = {
* *
* @param path Path to save the store in `app_data_dir` * @param path Path to save the store in `app_data_dir`
* @param options Store configuration options * @param options Store configuration options
*
* @throws If a store at that path is already loaded
*/ */
export async function create( export async function create(
path: string, path: string,
@ -75,8 +73,6 @@ export async function create(
* *
* @param path Path to save the store in `app_data_dir` * @param path Path to save the store in `app_data_dir`
* @param options Store configuration options * @param options Store configuration options
*
* @throws If a store at that path is already loaded
*/ */
export async function load( export async function load(
path: string, 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 path Path to save the store in `app_data_dir`
* @param options Store configuration options * @param options Store configuration options
*
* @throws If a store at that path is already loaded
*/ */
static async create(path: string, options?: StoreOptions): Promise<Store> { static async create(path: string, options?: StoreOptions): Promise<Store> {
const rid = await invoke<number>('plugin:store|create_store', { const rid = await invoke<number>('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 path Path to save the store in `app_data_dir`
* @param options Store configuration options * @param options Store configuration options
*
* @throws If a store at that path is already loaded
*/ */
static async load(path: string, options?: StoreOptions): Promise<Store> { static async load(path: string, options?: StoreOptions): Promise<Store> {
const rid = await invoke<number>('plugin:store|load', { const rid = await invoke<number>('plugin:store|load', {

@ -23,7 +23,7 @@ All operations are enabled by default.
- `allow-values` - `allow-values`
- `allow-entries` - `allow-entries`
- `allow-length` - `allow-length`
- `allow-load` - `allow-reload`
- `allow-save` - `allow-save`
## Permission Table ## Permission Table

@ -3,7 +3,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use std::path::PathBuf;
pub type Result<T> = std::result::Result<T, Error>; pub type Result<T> = std::result::Result<T, Error>;
@ -21,9 +20,9 @@ pub enum Error {
/// IO error. /// IO error.
#[error(transparent)] #[error(transparent)]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
/// Store already exists // /// Store already exists
#[error("Store at \"{0}\" already exists")] // #[error("Store at \"{0}\" already exists")]
AlreadyExists(PathBuf), // AlreadyExists(PathBuf),
/// Serialize function not found /// Serialize function not found
#[error("Serialize Function \"{0}\" not found")] #[error("Serialize Function \"{0}\" not found")]
SerializeFunctionNotFound(String), SerializeFunctionNotFound(String),

@ -172,7 +172,7 @@ impl<R: Runtime> StoreBuilder<R> {
self 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 { pub fn create_new(mut self) -> Self {
self.create_new = true; self.create_new = true;
self self
@ -181,14 +181,23 @@ impl<R: Runtime> StoreBuilder<R> {
pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> { pub(crate) fn build_inner(mut self) -> crate::Result<(Arc<Store<R>>, ResourceId)> {
let stores = self.app.state::<StoreState>().stores.clone(); let stores = self.app.state::<StoreState>().stores.clone();
let mut stores = stores.lock().unwrap(); 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) { self.path = resolve_store_path(&self.app, self.path)?;
return Err(crate::Error::AlreadyExists(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( let mut store_inner = StoreInner::new(
self.app.clone(), self.app.clone(),
self.path.clone(), self.path.clone(),

Loading…
Cancel
Save