Skip to content

Commit

Permalink
Add get-or-create-store
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Oct 3, 2024
1 parent 17d910b commit 7ffd769
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions plugins/store/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const COMMANDS: &[&str] = &[
"create_store",
"get_store",
"get_or_create_store",
"set",
"get",
"has",
Expand Down
33 changes: 30 additions & 3 deletions plugins/store/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export async function getStore(path: string): Promise<Store | undefined> {
return await Store.getStore(path)
}

/**
* @param path: Path to save the store in `app_data_dir`
* @param options: Store configuration options
*/
export async function getOrCreateStore(
path: string,
options?: StoreOptions
): Promise<Store> {
return await Store.getOrCreateStore(path, options)
}

/**
* A lazy loaded key-value store persisted by the backend layer.
*
Expand All @@ -56,9 +67,7 @@ export class LazyStore implements IStore {

private get store(): Promise<Store> {
if (!this._store) {
this._store = createStore(this.path, this.options).catch(
async () => (await getStore(this.path))!
)
this._store = getOrCreateStore(this.path, this.options)
}
return this._store
}
Expand Down Expand Up @@ -174,6 +183,24 @@ export class Store extends Resource implements IStore {
return resourceId ? new Store(resourceId, path) : undefined
}

/**
* @param path: Path to save the store in `app_data_dir`
* @param options: Store configuration options
*/
static async getOrCreateStore(
path: string,
options?: StoreOptions
): Promise<Store> {
const resourceId = await invoke<number>(
'plugin:store|get_or_create_store',
{
path,
...options
}
)
return new Store(resourceId, path)
}

async set(key: string, value: unknown): Promise<void> {
await invoke('plugin:store|set', {
rid: this.rid,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!

"$schema" = "../../schemas/schema.json"

[[permission]]
identifier = "allow-get-or-create-store"
description = "Enables the get_or_create_store command without any pre-configured scope."
commands.allow = ["get_or_create_store"]

[[permission]]
identifier = "deny-get-or-create-store"
description = "Denies the get_or_create_store command without any pre-configured scope."
commands.deny = ["get_or_create_store"]
26 changes: 26 additions & 0 deletions plugins/store/permissions/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,32 @@ Denies the get command without any pre-configured scope.
<tr>
<td>

`store:allow-get-or-create-store`

</td>
<td>

Enables the get_or_create_store command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`store:deny-get-or-create-store`

</td>
<td>

Denies the get_or_create_store command without any pre-configured scope.

</td>
</tr>

<tr>
<td>

`store:allow-get-store`

</td>
Expand Down
2 changes: 2 additions & 0 deletions plugins/store/permissions/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ All operations are enabled by default.
"""
permissions = [
"allow-create-store",
"allow-get-store",
"allow-get-or-create-store",
"allow-clear",
"allow-delete",
"allow-entries",
Expand Down
10 changes: 10 additions & 0 deletions plugins/store/permissions/schemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@
"type": "string",
"const": "deny-get"
},
{
"description": "Enables the get_or_create_store command without any pre-configured scope.",
"type": "string",
"const": "allow-get-or-create-store"
},
{
"description": "Denies the get_or_create_store command without any pre-configured scope.",
"type": "string",
"const": "deny-get-or-create-store"
},
{
"description": "Enables the get_store command without any pre-configured scope.",
"type": "string",
Expand Down
14 changes: 14 additions & 0 deletions plugins/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ async fn get_store<R: Runtime>(app: AppHandle<R>, path: PathBuf) -> Option<Resou
stores.get(&path).copied()
}

#[tauri::command]
async fn get_or_create_store<R: Runtime>(
app: AppHandle<R>,
path: PathBuf,
auto_save: Option<AutoSave>,
) -> Result<ResourceId> {
if let Some(rid) = get_store(app.clone(), path.clone()).await {
Ok(rid)
} else {
create_store(app, path, auto_save).await
}
}

#[tauri::command]
async fn set<R: Runtime>(
app: AppHandle<R>,
Expand Down Expand Up @@ -232,6 +245,7 @@ impl<R: Runtime> Builder<R> {
.invoke_handler(tauri::generate_handler![
create_store,
get_store,
get_or_create_store,
set,
get,
has,
Expand Down

0 comments on commit 7ffd769

Please sign in to comment.