From 4f13a034017e2c744d29b528a7dca879c3f187cd Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 3 Oct 2024 20:10:15 +0800 Subject: [PATCH] Fix js get and close store --- examples/api/src-tauri/capabilities/base.json | 6 +-- examples/api/src-tauri/src/lib.rs | 3 +- examples/api/src/views/Store.svelte | 51 +++++++++++++++--- plugins/store/api-iife.js | 2 +- plugins/store/build.rs | 1 + plugins/store/guest-js/index.ts | 5 ++ .../autogenerated/commands/close.toml | 13 +++++ .../autogenerated/commands/close_store.toml | 13 +++++ .../permissions/autogenerated/reference.md | 53 +++++++++++++++++++ plugins/store/permissions/default.toml | 1 + plugins/store/permissions/schemas/schema.json | 20 +++++++ plugins/store/src/lib.rs | 14 +++-- plugins/store/src/store.rs | 11 ++-- 13 files changed, 171 insertions(+), 22 deletions(-) create mode 100644 plugins/store/permissions/autogenerated/commands/close.toml create mode 100644 plugins/store/permissions/autogenerated/commands/close_store.toml diff --git a/examples/api/src-tauri/capabilities/base.json b/examples/api/src-tauri/capabilities/base.json index b76e898c..607828e9 100644 --- a/examples/api/src-tauri/capabilities/base.json +++ b/examples/api/src-tauri/capabilities/base.json @@ -79,10 +79,6 @@ ], "deny": ["$APPDATA/db/*.stronghold"] }, - "store:allow-entries", - "store:allow-get", - "store:allow-set", - "store:allow-save", - "store:allow-load" + "store:default" ] } diff --git a/examples/api/src-tauri/src/lib.rs b/examples/api/src-tauri/src/lib.rs index f3cacc43..701f6731 100644 --- a/examples/api/src-tauri/src/lib.rs +++ b/examples/api/src-tauri/src/lib.rs @@ -67,7 +67,8 @@ pub fn run() { .user_agent(&format!("Tauri API - {}", std::env::consts::OS)) .title("Tauri API Validation") .inner_size(1000., 800.) - .min_inner_size(600., 400.); + .min_inner_size(600., 400.) + .visible(false); } #[cfg(target_os = "windows")] diff --git a/examples/api/src/views/Store.svelte b/examples/api/src/views/Store.svelte index 48288a50..75583a53 100644 --- a/examples/api/src/views/Store.svelte +++ b/examples/api/src/views/Store.svelte @@ -7,15 +7,23 @@ let key; let value; - const store = new LazyStore("cache.json"); + let store = new LazyStore("cache.json"); let cache = {}; - onMount(async () => { - const values = await store.entries(); - for (const [key, value] of values) { - cache[key] = value; + async function refreshEntries() { + try { + const values = await store.entries(); + cache = {}; + for (const [key, value] of values) { + cache[key] = value; + } + } catch (error) { + onMessage(error); } - cache = cache; + } + + onMount(async () => { + await refreshEntries(); }); async function write(key, value) { @@ -23,11 +31,33 @@ await store.set(key, value); const v = await store.get(key); cache[key] = v; - cache = cache; } catch (error) { onMessage(error); } } + + async function reset() { + try { + await store.reset(); + } catch (error) { + onMessage(error); + } + await refreshEntries(); + } + + async function close() { + try { + await store.close(); + onMessage("Store is now closed, any new operations will now errors out"); + } catch (error) { + onMessage(error); + } + } + + function reopen() { + store = new LazyStore("cache.json"); + onMessage("We made a new `LazyStore` instance, operations will now work"); + }
@@ -42,7 +72,12 @@
- +
+ + + + +
diff --git a/plugins/store/api-iife.js b/plugins/store/api-iife.js index 40ed1365..7246c5a2 100644 --- a/plugins/store/api-iife.js +++ b/plugins/store/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";var e,r;function a(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},r){return window.__TAURI_INTERNALS__.invoke(t,e,r)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(t):a?a.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,r,a,s){if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");e.set(t,r)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,r){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:a(e)}).then((e=>async()=>async function(t,e){await s("plugin:event|unlisten",{event:t,eventId:e})}(t,e)))}async function o(t,e){return await u.createStore(t,e)}async function c(t){return await u.getStore(t)}e=new WeakMap,function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"}(r||(r={}));class u extends i{constructor(t){super(t)}static async createStore(t,e){const r=await s("plugin:store|create_store",{path:t,...e});return new u(r)}static async getStore(t){const e=await s("plugin:store|get_store",{path:t});return e?new u(e):void 0}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){return await s("plugin:store|get",{rid:this.rid,key:t})}async has(t){return await s("plugin:store|has",{rid:this.rid,key:t})}async delete(t){return await s("plugin:store|delete",{rid:this.rid,key:t})}async clear(){await s("plugin:store|clear",{rid:this.rid})}async reset(){await s("plugin:store|reset",{rid:this.rid})}async keys(){return await s("plugin:store|keys",{rid:this.rid})}async values(){return await s("plugin:store|values",{rid:this.rid})}async entries(){return await s("plugin:store|entries",{rid:this.rid})}async length(){return await s("plugin:store|length",{rid:this.rid})}async load(){await s("plugin:store|load",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(r=>{r.payload.resourceId===this.rid&&r.payload.key===t&&e(r.payload.value)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.value)}))}}return t.LazyStore=class{get store(){return this._store||(this._store=c(this.path).then((async t=>t||await o(this.path,this.options)))),this._store}constructor(t,e){this.path=t,this.options=e}async init(){await this.store}async set(t,e){return(await this.store).set(t,e)}async get(t){return(await this.store).get(t)}async has(t){return(await this.store).has(t)}async delete(t){return(await this.store).delete(t)}async clear(){await(await this.store).clear()}async reset(){await(await this.store).reset()}async keys(){return(await this.store).keys()}async values(){return(await this.store).values()}async entries(){return(await this.store).entries()}async length(){return(await this.store).length()}async load(){await(await this.store).load()}async save(){await(await this.store).save()}async onKeyChange(t,e){return(await this.store).onKeyChange(t,e)}async onChange(t){return(await this.store).onChange(t)}async close(){this._store&&await(await this._store).close()}},t.Store=u,t.createStore=o,t.getStore=c,t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";var e,r;function a(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},r){return window.__TAURI_INTERNALS__.invoke(t,e,r)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!a:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(t):a?a.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,r,a,s){if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");e.set(t,r)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,r){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:a(e)}).then((e=>async()=>async function(t,e){await s("plugin:event|unlisten",{event:t,eventId:e})}(t,e)))}async function o(t,e){return await u.createStore(t,e)}async function c(t){return await u.getStore(t)}e=new WeakMap,function(t){t.WINDOW_RESIZED="tauri://resize",t.WINDOW_MOVED="tauri://move",t.WINDOW_CLOSE_REQUESTED="tauri://close-requested",t.WINDOW_DESTROYED="tauri://destroyed",t.WINDOW_FOCUS="tauri://focus",t.WINDOW_BLUR="tauri://blur",t.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",t.WINDOW_THEME_CHANGED="tauri://theme-changed",t.WINDOW_CREATED="tauri://window-created",t.WEBVIEW_CREATED="tauri://webview-created",t.DRAG_ENTER="tauri://drag-enter",t.DRAG_OVER="tauri://drag-over",t.DRAG_DROP="tauri://drag-drop",t.DRAG_LEAVE="tauri://drag-leave"}(r||(r={}));class u extends i{constructor(t){super(t)}static async createStore(t,e){const r=await s("plugin:store|create_store",{path:t,...e});return new u(r)}static async getStore(t){const e=await s("plugin:store|get_store",{path:t});return e?new u(e):void 0}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){return await s("plugin:store|get",{rid:this.rid,key:t})}async has(t){return await s("plugin:store|has",{rid:this.rid,key:t})}async delete(t){return await s("plugin:store|delete",{rid:this.rid,key:t})}async clear(){await s("plugin:store|clear",{rid:this.rid})}async reset(){await s("plugin:store|reset",{rid:this.rid})}async keys(){return await s("plugin:store|keys",{rid:this.rid})}async values(){return await s("plugin:store|values",{rid:this.rid})}async entries(){return await s("plugin:store|entries",{rid:this.rid})}async length(){return await s("plugin:store|length",{rid:this.rid})}async load(){await s("plugin:store|load",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(r=>{r.payload.resourceId===this.rid&&r.payload.key===t&&e(r.payload.value)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.value)}))}async close(){await s("plugin:store|close_store",{rid:this.rid})}}return t.LazyStore=class{get store(){return this._store||(this._store=c(this.path).then((async t=>t||await o(this.path,this.options)))),this._store}constructor(t,e){this.path=t,this.options=e}async init(){await this.store}async set(t,e){return(await this.store).set(t,e)}async get(t){return(await this.store).get(t)}async has(t){return(await this.store).has(t)}async delete(t){return(await this.store).delete(t)}async clear(){await(await this.store).clear()}async reset(){await(await this.store).reset()}async keys(){return(await this.store).keys()}async values(){return(await this.store).values()}async entries(){return(await this.store).entries()}async length(){return(await this.store).length()}async load(){await(await this.store).load()}async save(){await(await this.store).save()}async onKeyChange(t,e){return(await this.store).onKeyChange(t,e)}async onChange(t){return(await this.store).onChange(t)}async close(){this._store&&await(await this._store).close()}},t.Store=u,t.createStore=o,t.getStore=c,t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})} diff --git a/plugins/store/build.rs b/plugins/store/build.rs index a144df29..b22b0b86 100644 --- a/plugins/store/build.rs +++ b/plugins/store/build.rs @@ -5,6 +5,7 @@ const COMMANDS: &[&str] = &[ "create_store", "get_store", + "close_store", "set", "get", "has", diff --git a/plugins/store/guest-js/index.ts b/plugins/store/guest-js/index.ts index cc85c78a..5fa3e02f 100644 --- a/plugins/store/guest-js/index.ts +++ b/plugins/store/guest-js/index.ts @@ -276,6 +276,11 @@ export class Store extends Resource implements IStore { } }) } + + async close(): Promise { + // The default close on `Resource` can only close resources in the webview's resource table + await invoke('plugin:store|close_store', { rid: this.rid }) + } } interface IStore { diff --git a/plugins/store/permissions/autogenerated/commands/close.toml b/plugins/store/permissions/autogenerated/commands/close.toml new file mode 100644 index 00000000..fad12d15 --- /dev/null +++ b/plugins/store/permissions/autogenerated/commands/close.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-close" +description = "Enables the close command without any pre-configured scope." +commands.allow = ["close"] + +[[permission]] +identifier = "deny-close" +description = "Denies the close command without any pre-configured scope." +commands.deny = ["close"] diff --git a/plugins/store/permissions/autogenerated/commands/close_store.toml b/plugins/store/permissions/autogenerated/commands/close_store.toml new file mode 100644 index 00000000..5a8e3a0c --- /dev/null +++ b/plugins/store/permissions/autogenerated/commands/close_store.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-close-store" +description = "Enables the close_store command without any pre-configured scope." +commands.allow = ["close_store"] + +[[permission]] +identifier = "deny-close-store" +description = "Denies the close_store command without any pre-configured scope." +commands.deny = ["close_store"] diff --git a/plugins/store/permissions/autogenerated/reference.md b/plugins/store/permissions/autogenerated/reference.md index 8d05169d..d23e836c 100644 --- a/plugins/store/permissions/autogenerated/reference.md +++ b/plugins/store/permissions/autogenerated/reference.md @@ -11,6 +11,7 @@ All operations are enabled by default. - `allow-create-store` - `allow-get-store` +- `allow-close-store` - `allow-clear` - `allow-delete` - `allow-entries` @@ -62,6 +63,58 @@ Denies the clear command without any pre-configured scope. +`store:allow-close` + + + + +Enables the close command without any pre-configured scope. + + + + + + + +`store:deny-close` + + + + +Denies the close command without any pre-configured scope. + + + + + + + +`store:allow-close-store` + + + + +Enables the close_store command without any pre-configured scope. + + + + + + + +`store:deny-close-store` + + + + +Denies the close_store command without any pre-configured scope. + + + + + + + `store:allow-create-store` diff --git a/plugins/store/permissions/default.toml b/plugins/store/permissions/default.toml index 3a38e6e8..8c2b41c0 100644 --- a/plugins/store/permissions/default.toml +++ b/plugins/store/permissions/default.toml @@ -13,6 +13,7 @@ All operations are enabled by default. permissions = [ "allow-create-store", "allow-get-store", + "allow-close-store", "allow-clear", "allow-delete", "allow-entries", diff --git a/plugins/store/permissions/schemas/schema.json b/plugins/store/permissions/schemas/schema.json index bfd50f9c..a567cbe4 100644 --- a/plugins/store/permissions/schemas/schema.json +++ b/plugins/store/permissions/schemas/schema.json @@ -304,6 +304,26 @@ "type": "string", "const": "deny-clear" }, + { + "description": "Enables the close command without any pre-configured scope.", + "type": "string", + "const": "allow-close" + }, + { + "description": "Denies the close command without any pre-configured scope.", + "type": "string", + "const": "deny-close" + }, + { + "description": "Enables the close_store command without any pre-configured scope.", + "type": "string", + "const": "allow-close-store" + }, + { + "description": "Denies the close_store command without any pre-configured scope.", + "type": "string", + "const": "deny-close-store" + }, { "description": "Enables the create_store command without any pre-configured scope.", "type": "string", diff --git a/plugins/store/src/lib.rs b/plugins/store/src/lib.rs index 6e01061b..ec58a00b 100644 --- a/plugins/store/src/lib.rs +++ b/plugins/store/src/lib.rs @@ -21,6 +21,7 @@ use std::{ sync::{Arc, Mutex}, time::Duration, }; +use store::resolve_store_path; pub use store::{DeserializeFn, SerializeFn, Store, StoreBuilder, StoreInner}; use tauri::{ plugin::{self, TauriPlugin}, @@ -96,12 +97,18 @@ async fn create_store( } #[tauri::command] -async fn get_store( +async fn get_store( + app: AppHandle, store_collection: State<'_, StoreCollection>, path: PathBuf, ) -> Result> { let stores = store_collection.stores.lock().unwrap(); - Ok(stores.get(&path).copied()) + Ok(stores.get(&resolve_store_path(app, path)).copied()) +} + +#[tauri::command] +async fn close_store(app: AppHandle, rid: ResourceId) -> Result<()> { + Ok(app.resources_table().close(rid)?) } #[tauri::command] @@ -275,6 +282,7 @@ impl Builder { .invoke_handler(tauri::generate_handler![ create_store, get_store, + close_store, set, get, has, @@ -286,7 +294,7 @@ impl Builder { length, entries, load, - save + save, ]) .setup(move |app_handle, _api| { app_handle.manage(StoreCollection { diff --git a/plugins/store/src/store.rs b/plugins/store/src/store.rs index aa5117bd..5efbd173 100644 --- a/plugins/store/src/store.rs +++ b/plugins/store/src/store.rs @@ -36,6 +36,12 @@ fn default_deserialize( serde_json::from_slice(bytes).map_err(Into::into) } +pub(crate) fn resolve_store_path(app: AppHandle, path: impl AsRef) -> PathBuf { + app.path() + .resolve(path, BaseDirectory::AppData) + .expect("failed to resolve app dir") +} + /// Builds a [`Store`] pub struct StoreBuilder { app: AppHandle, @@ -60,10 +66,7 @@ impl StoreBuilder { /// ``` pub fn new, P: AsRef>(manager: &M, path: P) -> Self { let app = manager.app_handle().clone(); - let path = app - .path() - .resolve(path, BaseDirectory::AppData) - .expect("failed to resolve app dir"); + let path = resolve_store_path(app.clone(), path); Self { app, // Since Store.path is only exposed to the user in emit calls we may as well simplify it here already.