From 09cce55b4067777c7c9759b35b77b5f9641ca380 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 17 Oct 2024 07:24:17 -0300 Subject: [PATCH] remove create_store command --- plugins/store/api-iife.js | 2 +- plugins/store/build.rs | 1 - plugins/store/guest-js/index.ts | 61 ++----------------- .../autogenerated/commands/create_store.toml | 13 ---- .../permissions/autogenerated/reference.md | 27 -------- plugins/store/permissions/default.toml | 1 - plugins/store/permissions/schemas/schema.json | 10 --- plugins/store/src/lib.rs | 30 +++------ 8 files changed, 15 insertions(+), 130 deletions(-) delete mode 100644 plugins/store/permissions/autogenerated/commands/create_store.toml diff --git a/plugins/store/api-iife.js b/plugins/store/api-iife.js index cc64d0b7..d7af2bfe 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,a;function r(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},a){return window.__TAURI_INTERNALS__.invoke(t,e,a)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,a,r){if("a"===a&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?r:"a"===a?r.call(t):r?r.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,a,r,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,a)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,a){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:r(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 c.load(t,e)}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"}(a||(a={}));class c extends i{constructor(t){super(t)}static async create(t,e){const a=await s("plugin:store|create_store",{path:t,...e});return new c(a)}static async load(t,e){const a=await s("plugin:store|load",{path:t,...e});return new c(a)}static async get(t){return await s("plugin:store|get_store",{path:t}).then((t=>t?new c(t):null))}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){const[e,a]=await s("plugin:store|get",{rid:this.rid,key:t});return a?e:void 0}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 reload(){await s("plugin:store|reload",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(a=>{a.payload.resourceId===this.rid&&a.payload.key===t&&e(a.payload.exists?a.payload.value:void 0)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.exists?e.payload.value:void 0)}))}async close(){await s("plugin:store|close_store",{rid:this.rid})}}return t.LazyStore=class{get store(){return this._store||(this._store=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 reload(){await(await this.store).reload()}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=c,t.create=async function(t,e){return await c.create(t,e)},t.getStore=async function(t){return await c.get(t)},t.load=o,t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(t){"use strict";var e,a;function r(t,e=!1){return window.__TAURI_INTERNALS__.transformCallback(t,e)}async function s(t,e={},a){return window.__TAURI_INTERNALS__.invoke(t,e,a)}"function"==typeof SuppressedError&&SuppressedError;class i{get rid(){return function(t,e,a,r){if("a"===a&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===a?r:"a"===a?r.call(t):r?r.value:e.get(t)}(this,e,"f")}constructor(t){e.set(this,void 0),function(t,e,a,r,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,a)}(this,e,t)}async close(){return s("plugin:resources|close",{rid:this.rid})}}async function n(t,e,a){const i={kind:"Any"};return s("plugin:event|listen",{event:t,target:i,handler:r(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 c.load(t,e)}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"}(a||(a={}));class c extends i{constructor(t){super(t)}static async load(t,e){const a=await s("plugin:store|load",{path:t,...e});return new c(a)}static async get(t){return await s("plugin:store|get_store",{path:t}).then((t=>t?new c(t):null))}async set(t,e){await s("plugin:store|set",{rid:this.rid,key:t,value:e})}async get(t){const[e,a]=await s("plugin:store|get",{rid:this.rid,key:t});return a?e:void 0}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 reload(){await s("plugin:store|reload",{rid:this.rid})}async save(){await s("plugin:store|save",{rid:this.rid})}async onKeyChange(t,e){return await n("store://change",(a=>{a.payload.resourceId===this.rid&&a.payload.key===t&&e(a.payload.exists?a.payload.value:void 0)}))}async onChange(t){return await n("store://change",(e=>{e.payload.resourceId===this.rid&&t(e.payload.key,e.payload.exists?e.payload.value:void 0)}))}async close(){await s("plugin:store|close_store",{rid:this.rid})}}return t.LazyStore=class{get store(){return this._store||(this._store=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 reload(){await(await this.store).reload()}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=c,t.getStore=async function(t){return await c.get(t)},t.load=o,t}({});Object.defineProperty(window.__TAURI__,"store",{value:__TAURI_PLUGIN_STORE__})} diff --git a/plugins/store/build.rs b/plugins/store/build.rs index 52175aa0..747d9774 100644 --- a/plugins/store/build.rs +++ b/plugins/store/build.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: MIT const COMMANDS: &[&str] = &[ - "create_store", "load", "get_store", "close_store", diff --git a/plugins/store/guest-js/index.ts b/plugins/store/guest-js/index.ts index 772966bb..b8641db4 100644 --- a/plugins/store/guest-js/index.ts +++ b/plugins/store/guest-js/index.ts @@ -30,31 +30,10 @@ export type StoreOptions = { * Name of a deserialize function registered in the rust side plugin builder */ deserializeFnName?: string -} - -/** - * Create a new store. - * - * If the store already exists, its data will be overwritten. - * - * To load the store if it already exists you must use {@link load} instead. - * - * If the store is already loaded you must use {@link getStore} instead. - * - * @example - * ```typescript - * import { Store } from '@tauri-apps/api/store'; - * const store = await Store.create('store.json'); - * ``` - * - * @param path Path to save the store in `app_data_dir` - * @param options Store configuration options - */ -export async function create( - path: string, - options?: StoreOptions -): Promise { - return await Store.create(path, options) + /** + * Force create a new store with default values even if it already exists. + */ + createNew?: boolean } /** @@ -79,8 +58,7 @@ export async function load( /** * Gets an already loaded store. * - * If the store is not loaded, returns `null`. In this case, - * you must either {@link Store.create | create} it or {@link Store.load load} it. + * If the store is not loaded, returns `null`. In this case you must {@link Store.load load} it. * * This function is more useful when you already know the store is loaded * and just need to access its instance. Prefer {@link Store.load} otherwise. @@ -203,32 +181,6 @@ export class Store extends Resource implements IStore { super(rid) } - /** - * Create a new store. - * - * If the store already exists, its data will be overwritten. - * - * To load the store if it already exists you must use {@link load} instead. - * - * If the store is already loaded you must use {@link getStore} instead. - * - * @example - * ```typescript - * import { Store } from '@tauri-apps/api/store'; - * const store = await Store.create('store.json'); - * ``` - * - * @param path Path to save the store in `app_data_dir` - * @param options Store configuration options - */ - static async create(path: string, options?: StoreOptions): Promise { - const rid = await invoke('plugin:store|create_store', { - path, - ...options - }) - return new Store(rid) - } - /** * Create a new Store or load the existing store with the path. * @@ -252,8 +204,7 @@ export class Store extends Resource implements IStore { /** * Gets an already loaded store. * - * If the store is not loaded, returns `null`. In this case, - * you must either {@link Store.create | create} it or {@link Store.load load} it. + * If the store is not loaded, returns `null`. In this case you must {@link Store.load load} it. * * This function is more useful when you already know the store is loaded * and just need to access its instance. Prefer {@link Store.load} otherwise. diff --git a/plugins/store/permissions/autogenerated/commands/create_store.toml b/plugins/store/permissions/autogenerated/commands/create_store.toml deleted file mode 100644 index cde71c24..00000000 --- a/plugins/store/permissions/autogenerated/commands/create_store.toml +++ /dev/null @@ -1,13 +0,0 @@ -# Automatically generated - DO NOT EDIT! - -"$schema" = "../../schemas/schema.json" - -[[permission]] -identifier = "allow-create-store" -description = "Enables the create_store command without any pre-configured scope." -commands.allow = ["create_store"] - -[[permission]] -identifier = "deny-create-store" -description = "Denies the create_store command without any pre-configured scope." -commands.deny = ["create_store"] diff --git a/plugins/store/permissions/autogenerated/reference.md b/plugins/store/permissions/autogenerated/reference.md index 4260d3f8..2bd12e84 100644 --- a/plugins/store/permissions/autogenerated/reference.md +++ b/plugins/store/permissions/autogenerated/reference.md @@ -9,7 +9,6 @@ All operations are enabled by default. -- `allow-create-store` - `allow-load` - `allow-get-store` - `allow-close-store` @@ -90,32 +89,6 @@ Denies the close_store command without any pre-configured scope. -`store:allow-create-store` - - - - -Enables the create_store command without any pre-configured scope. - - - - - - - -`store:deny-create-store` - - - - -Denies the create_store command without any pre-configured scope. - - - - - - - `store:allow-delete` diff --git a/plugins/store/permissions/default.toml b/plugins/store/permissions/default.toml index 04fdc29f..297686f3 100644 --- a/plugins/store/permissions/default.toml +++ b/plugins/store/permissions/default.toml @@ -11,7 +11,6 @@ All operations are enabled by default. """ permissions = [ - "allow-create-store", "allow-load", "allow-get-store", "allow-close-store", diff --git a/plugins/store/permissions/schemas/schema.json b/plugins/store/permissions/schemas/schema.json index 2400aac5..b8962c12 100644 --- a/plugins/store/permissions/schemas/schema.json +++ b/plugins/store/permissions/schemas/schema.json @@ -314,16 +314,6 @@ "type": "string", "const": "deny-close-store" }, - { - "description": "Enables the create_store command without any pre-configured scope.", - "type": "string", - "const": "allow-create-store" - }, - { - "description": "Denies the create_store command without any pre-configured scope.", - "type": "string", - "const": "deny-create-store" - }, { "description": "Enables the delete command without any pre-configured scope.", "type": "string", diff --git a/plugins/store/src/lib.rs b/plugins/store/src/lib.rs index cabd6b6a..76e94d97 100644 --- a/plugins/store/src/lib.rs +++ b/plugins/store/src/lib.rs @@ -62,6 +62,7 @@ fn builder( auto_save: Option, serialize_fn_name: Option, deserialize_fn_name: Option, + create_new: bool, ) -> Result> { let mut builder = app.store_builder(path); if let Some(auto_save) = auto_save { @@ -91,28 +92,12 @@ fn builder( .ok_or_else(|| crate::Error::DeserializeFunctionNotFound(deserialize_fn_name))?; builder = builder.deserialize(*deserialize_fn); } - Ok(builder) -} -#[tauri::command] -async fn create_store( - app: AppHandle, - store_state: State<'_, StoreState>, - path: PathBuf, - auto_save: Option, - serialize_fn_name: Option, - deserialize_fn_name: Option, -) -> Result { - let builder = builder( - app, - store_state, - path, - auto_save, - serialize_fn_name, - deserialize_fn_name, - )?; - let (_, rid) = builder.create_new().build_inner()?; - Ok(rid) + if create_new { + builder = builder.create_new(); + } + + Ok(builder) } #[tauri::command] @@ -123,6 +108,7 @@ async fn load( auto_save: Option, serialize_fn_name: Option, deserialize_fn_name: Option, + create_new: Option, ) -> Result { let builder = builder( app, @@ -131,6 +117,7 @@ async fn load( auto_save, serialize_fn_name, deserialize_fn_name, + create_new.unwrap_or_default(), )?; let (_, rid) = builder.build_inner()?; Ok(rid) @@ -432,7 +419,6 @@ impl Builder { pub fn build(self) -> TauriPlugin { plugin::Builder::new("store") .invoke_handler(tauri::generate_handler![ - create_store, load, get_store, close_store,