diff --git a/examples/api/src/views/Store.svelte b/examples/api/src/views/Store.svelte
index d8e6653b..5c16fe93 100644
--- a/examples/api/src/views/Store.svelte
+++ b/examples/api/src/views/Store.svelte
@@ -1,5 +1,5 @@
diff --git a/plugins/store/api-iife.js b/plugins/store/api-iife.js
index e7b7bea2..681126e1 100644
--- a/plugins/store/api-iife.js
+++ b/plugins/store/api-iife.js
@@ -1 +1 @@
-if("__TAURI__"in window){var __TAURI_PLUGIN_STORE__=function(e){"use strict";var t,r;function a(e,t=!1){return window.__TAURI_INTERNALS__.transformCallback(e,t)}async function i(e,t={},r){return window.__TAURI_INTERNALS__.invoke(e,t,r)}"function"==typeof SuppressedError&&SuppressedError;class n{get rid(){return function(e,t,r,a){if("a"===r&&!a)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?a:"a"===r?a.call(e):a?a.value:t.get(e)}(this,t,"f")}constructor(e){t.set(this,void 0),function(e,t,r,a,i){if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");t.set(e,r)}(this,t,e)}async close(){return i("plugin:resources|close",{rid:this.rid})}}async function s(e,t,r){const n={kind:"Any"};return i("plugin:event|listen",{event:e,target:n,handler:a(t)}).then((t=>async()=>async function(e,t){await i("plugin:event|unlisten",{event:e,eventId:t})}(e,t)))}t=new WeakMap,function(e){e.WINDOW_RESIZED="tauri://resize",e.WINDOW_MOVED="tauri://move",e.WINDOW_CLOSE_REQUESTED="tauri://close-requested",e.WINDOW_DESTROYED="tauri://destroyed",e.WINDOW_FOCUS="tauri://focus",e.WINDOW_BLUR="tauri://blur",e.WINDOW_SCALE_FACTOR_CHANGED="tauri://scale-change",e.WINDOW_THEME_CHANGED="tauri://theme-changed",e.WINDOW_CREATED="tauri://window-created",e.WEBVIEW_CREATED="tauri://webview-created",e.DRAG_ENTER="tauri://drag-enter",e.DRAG_OVER="tauri://drag-over",e.DRAG_DROP="tauri://drag-drop",e.DRAG_LEAVE="tauri://drag-leave"}(r||(r={}));class o extends n{constructor(e,t){super(e),this.path=t}async set(e,t){await i("plugin:store|set",{rid:this.rid,key:e,value:t})}async get(e){return await i("plugin:store|get",{rid:this.rid,key:e})}async has(e){return await i("plugin:store|has",{rid:this.rid,key:e})}async delete(e){return await i("plugin:store|delete",{rid:this.rid,key:e})}async clear(){await i("plugin:store|clear",{rid:this.rid})}async reset(){await i("plugin:store|reset",{rid:this.rid})}async keys(){return await i("plugin:store|keys",{rid:this.rid})}async values(){return await i("plugin:store|values",{rid:this.rid})}async entries(){return await i("plugin:store|entries",{rid:this.rid})}async length(){return await i("plugin:store|length",{rid:this.rid})}async load(){await i("plugin:store|load",{rid:this.rid})}async save(){await i("plugin:store|save",{rid:this.rid})}async onKeyChange(e,t){return await s("store://change",(r=>{r.payload.path===this.path&&r.payload.key===e&&t(r.payload.value)}))}async onChange(e){return await s("store://change",(t=>{t.payload.path===this.path&&e(t.payload.key,t.payload.value)}))}}return e.Store=o,e.createStore=async function(e,t){const r=await i("plugin:store|create_store",{path:e,...t});return new o(r,e)},e.getStore=async function(e){const t=await i("plugin:store|get_store");return t?new o(t,e):void 0},e}({});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 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"}(a||(a={}));class u extends i{constructor(t,e){super(t),this.path=e}static async createStore(t,e){const a=await s("plugin:store|create_store",{path:t,...e});return new u(a,t)}static async getStore(t){const e=await s("plugin:store|get_store");return e?new u(e,t):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",(a=>{a.payload.path===this.path&&a.payload.key===t&&e(a.payload.value)}))}async onChange(t){return await n("store://change",(e=>{e.payload.path===this.path&&t(e.payload.key,e.payload.value)}))}}return t.LazyStore=class{constructor(t,e){this.path=t,this.options=e}get store(){return this._store||(this._store=o(this.path,this.options).catch((async()=>await c(this.path)))),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/guest-js/index.ts b/plugins/store/guest-js/index.ts
index c9df7a31..15e55681 100644
--- a/plugins/store/guest-js/index.ts
+++ b/plugins/store/guest-js/index.ts
@@ -32,26 +32,110 @@ export async function createStore(
path: string,
options?: StoreOptions
): Promise {
- const resourceId = await invoke('plugin:store|create_store', {
- path,
- ...options
- })
- return new Store(resourceId, path)
+ return await Store.createStore(path, options)
}
/**
* @param path: Path of the store in the rust side
*/
export async function getStore(path: string): Promise {
- const resourceId = await invoke('plugin:store|get_store')
- return resourceId ? new Store(resourceId, path) : undefined
+ return await Store.getStore(path)
}
/**
- * A key-value store persisted by the backend layer.
+ * A lazy loaded key-value store persisted by the backend layer.
+ *
+ * Note that the options are not applied if someone else already created the store
*/
-export class Store extends Resource {
+export class LazyStore implements IStore {
+ private _store?: Promise
constructor(
+ private readonly path: string,
+ private readonly options?: StoreOptions
+ ) {}
+
+ public get store(): Promise {
+ if (!this._store) {
+ this._store = createStore(this.path, this.options).catch(
+ async () => (await getStore(this.path))!
+ )
+ }
+ return this._store
+ }
+
+ async set(key: string, value: unknown): Promise {
+ return (await this.store).set(key, value)
+ }
+
+ async get(key: string): Promise {
+ return (await this.store).get(key)
+ }
+
+ async has(key: string): Promise {
+ return (await this.store).has(key)
+ }
+
+ async delete(key: string): Promise {
+ return (await this.store).delete(key)
+ }
+
+ async clear(): Promise {
+ await (await this.store).clear()
+ }
+
+ async reset(): Promise {
+ await (await this.store).reset()
+ }
+
+ async keys(): Promise {
+ return (await this.store).keys()
+ }
+
+ async values(): Promise {
+ return (await this.store).values()
+ }
+
+ async entries(): Promise> {
+ return (await this.store).entries()
+ }
+
+ async length(): Promise {
+ return (await this.store).length()
+ }
+
+ async load(): Promise {
+ await (await this.store).load()
+ }
+
+ async save(): Promise {
+ await (await this.store).save()
+ }
+
+ async onKeyChange(
+ key: string,
+ cb: (value: T | null) => void
+ ): Promise {
+ return (await this.store).onKeyChange(key, cb)
+ }
+
+ async onChange(
+ cb: (key: string, value: T | null) => void
+ ): Promise {
+ return (await this.store).onChange(cb)
+ }
+
+ async close(): Promise {
+ if (this._store) {
+ await (await this._store).close()
+ }
+ }
+}
+
+/**
+ * A key-value store persisted by the backend layer.
+ */
+export class Store extends Resource implements IStore {
+ private constructor(
rid: number,
private readonly path: string
) {
@@ -59,12 +143,30 @@ export class Store extends Resource {
}
/**
- * Inserts a key-value pair into the store.
+ * @param path: Path to save the store in `app_data_dir`
+ * @param options: Store configuration options
*
- * @param key
- * @param value
- * @returns
+ * @throws If a store at that path already exists
+ */
+ static async createStore(
+ path: string,
+ options?: StoreOptions
+ ): Promise {
+ const resourceId = await invoke('plugin:store|create_store', {
+ path,
+ ...options
+ })
+ return new Store(resourceId, path)
+ }
+
+ /**
+ * @param path: Path of the store in the rust side
*/
+ static async getStore(path: string): Promise {
+ const resourceId = await invoke('plugin:store|get_store')
+ return resourceId ? new Store(resourceId, path) : undefined
+ }
+
async set(key: string, value: unknown): Promise {
await invoke('plugin:store|set', {
rid: this.rid,
@@ -73,12 +175,6 @@ export class Store extends Resource {
})
}
- /**
- * Returns the value for the given `key` or `null` the key does not exist.
- *
- * @param key
- * @returns
- */
async get(key: string): Promise {
return await invoke('plugin:store|get', {
rid: this.rid,
@@ -86,12 +182,6 @@ export class Store extends Resource {
})
}
- /**
- * Returns `true` if the given `key` exists in the store.
- *
- * @param key
- * @returns
- */
async has(key: string): Promise {
return await invoke('plugin:store|has', {
rid: this.rid,
@@ -99,12 +189,6 @@ export class Store extends Resource {
})
}
- /**
- * Removes a key-value pair from the store.
- *
- * @param key
- * @returns
- */
async delete(key: string): Promise {
return await invoke('plugin:store|delete', {
rid: this.rid,
@@ -112,84 +196,156 @@ export class Store extends Resource {
})
}
+ async clear(): Promise {
+ await invoke('plugin:store|clear', { rid: this.rid })
+ }
+
+ async reset(): Promise {
+ await invoke('plugin:store|reset', { rid: this.rid })
+ }
+
+ async keys(): Promise {
+ return await invoke('plugin:store|keys', { rid: this.rid })
+ }
+
+ async values(): Promise {
+ return await invoke('plugin:store|values', { rid: this.rid })
+ }
+
+ async entries(): Promise> {
+ return await invoke('plugin:store|entries', { rid: this.rid })
+ }
+
+ async length(): Promise {
+ return await invoke('plugin:store|length', { rid: this.rid })
+ }
+
+ async load(): Promise {
+ await invoke('plugin:store|load', { rid: this.rid })
+ }
+
+ async save(): Promise {
+ await invoke('plugin:store|save', { rid: this.rid })
+ }
+
+ async onKeyChange(
+ key: string,
+ cb: (value: T | null) => void
+ ): Promise {
+ return await listen>('store://change', (event) => {
+ if (event.payload.path === this.path && event.payload.key === key) {
+ cb(event.payload.value)
+ }
+ })
+ }
+
+ async onChange(
+ cb: (key: string, value: T | null) => void
+ ): Promise {
+ return await listen>('store://change', (event) => {
+ if (event.payload.path === this.path) {
+ cb(event.payload.key, event.payload.value)
+ }
+ })
+ }
+}
+
+interface IStore {
+ /**
+ * Inserts a key-value pair into the store.
+ *
+ * @param key
+ * @param value
+ * @returns
+ */
+ set(key: string, value: unknown): Promise
+
+ /**
+ * Returns the value for the given `key` or `null` if the key does not exist.
+ *
+ * @param key
+ * @returns
+ */
+ get(key: string): Promise
+
+ /**
+ * Returns `true` if the given `key` exists in the store.
+ *
+ * @param key
+ * @returns
+ */
+ has(key: string): Promise
+
+ /**
+ * Removes a key-value pair from the store.
+ *
+ * @param key
+ * @returns
+ */
+ delete(key: string): Promise
+
/**
* Clears the store, removing all key-value pairs.
*
- * Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
+ * Note: To clear the storage and reset it to its `default` value, use `reset` instead.
* @returns
*/
- async clear(): Promise {
- await invoke('plugin:store|clear', { rid: this.rid })
- }
+ clear(): Promise
/**
- * Resets the store to it's `default` value.
+ * Resets the store to its `default` value.
*
* If no default value has been set, this method behaves identical to `clear`.
* @returns
*/
- async reset(): Promise {
- await invoke('plugin:store|reset', { rid: this.rid })
- }
+ reset(): Promise
/**
- * Returns a list of all key in the store.
+ * Returns a list of all keys in the store.
*
* @returns
*/
- async keys(): Promise {
- return await invoke('plugin:store|keys', { rid: this.rid })
- }
+ keys(): Promise
/**
* Returns a list of all values in the store.
*
* @returns
*/
- async values(): Promise {
- return await invoke('plugin:store|values', { rid: this.rid })
- }
+ values(): Promise
/**
* Returns a list of all entries in the store.
*
* @returns
*/
- async entries(): Promise> {
- return await invoke('plugin:store|entries', { rid: this.rid })
- }
+ entries(): Promise>
/**
* Returns the number of key-value pairs in the store.
*
* @returns
*/
- async length(): Promise {
- return await invoke('plugin:store|length', { rid: this.rid })
- }
+ length(): Promise
/**
- * Attempts to load the on-disk state at the stores `path` into memory.
+ * Attempts to load the on-disk state at the store's `path` into memory.
*
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
*
* Note: This method does not emit change events.
* @returns
*/
- async load(): Promise {
- await invoke('plugin:store|load', { rid: this.rid })
- }
+ load(): Promise
/**
- * Saves the store to disk at the stores `path`.
+ * Saves the store to disk at the store's `path`.
*
- * As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
+ * As the store is only persisted to disk before the app's exit, changes might be lost in a crash.
* This method lets you persist the store to disk whenever you deem necessary.
* @returns
*/
- async save(): Promise {
- await invoke('plugin:store|save', { rid: this.rid })
- }
+ save(): Promise
/**
* Listen to changes on a store key.
@@ -199,16 +355,10 @@ export class Store extends Resource {
*
* @since 2.0.0
*/
- async onKeyChange(
+ onKeyChange(
key: string,
cb: (value: T | null) => void
- ): Promise {
- return await listen>('store://change', (event) => {
- if (event.payload.path === this.path && event.payload.key === key) {
- cb(event.payload.value)
- }
- })
- }
+ ): Promise
/**
* Listen to changes on the store.
@@ -217,13 +367,11 @@ export class Store extends Resource {
*
* @since 2.0.0
*/
- async onChange(
- cb: (key: string, value: T | null) => void
- ): Promise {
- return await listen>('store://change', (event) => {
- if (event.payload.path === this.path) {
- cb(event.payload.key, event.payload.value)
- }
- })
- }
+ onChange(cb: (key: string, value: T | null) => void): Promise
+
+ /**
+ * Close the store and cleans up this resource from memory.
+ * **You should not call any method on this object anymore and should drop any reference to it.**
+ */
+ close(): Promise
}
diff --git a/plugins/store/src/store.rs b/plugins/store/src/store.rs
index c6c99558..aba7f902 100644
--- a/plugins/store/src/store.rs
+++ b/plugins/store/src/store.rs
@@ -463,6 +463,9 @@ impl Store {
self.store.lock().unwrap().save()
}
+ /// Removes the store from the resource table,
+ /// this doesn't remove other references of the same store held by you,
+ /// and the store is only truely closed once all reference held by you are also dropped
pub fn close_store(self) {
let store = self.store.lock().unwrap();
let app = store.app.clone();