Add create or existing

This commit is contained in:
Tony
2024-10-05 16:21:27 +08:00
parent 7faf8c31e0
commit dc5be009b7
9 changed files with 193 additions and 21 deletions
+34 -3
View File
@@ -45,6 +45,19 @@ export async function createStore(
return await Store.createStore(path, options)
}
/**
* Create a new Store or get the existing store with the path
*
* @param path: Path to save the store in `app_data_dir`
* @param options: Store configuration options
*/
export async function createOrExistingStore(
path: string,
options?: StoreOptions
): Promise<Store> {
return await Store.createOrExistingStore(path, options)
}
/**
* @param path: Path of the store in the rust side
*/
@@ -60,9 +73,7 @@ export class LazyStore implements IStore {
private get store(): Promise<Store> {
if (!this._store) {
this._store = getStore(this.path).then(
async (store) => store || (await createStore(this.path, this.options))
)
this._store = createOrExistingStore(this.path, this.options)
}
return this._store
}
@@ -183,6 +194,26 @@ export class Store extends Resource implements IStore {
)
}
/**
* Create a new Store or get the existing store with the path
*
* @param path: Path to save the store in `app_data_dir`
* @param options: Store configuration options
*/
static async createOrExistingStore(
path: string,
options?: StoreOptions
): Promise<Store> {
const rid = await invoke<number>('plugin:store|create_or_existing_store', {
path,
...options
})
return new Store(
rid
// path
)
}
/**
* @param path: Path of the store in the rust side
*/