Add lazy store

pull/1860/head
Tony 8 months ago
parent 6cdaa31959
commit 79bc8d08f4
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -1,5 +1,5 @@
<script> <script>
import { Store } from "@tauri-apps/plugin-store"; import { LazyStore } from "@tauri-apps/plugin-store";
import { onMount } from "svelte"; import { onMount } from "svelte";
export let onMessage; export let onMessage;
@ -7,11 +7,10 @@
let key; let key;
let value; let value;
const store = new Store("cache.json"); const store = new LazyStore("cache.json");
let cache = {}; let cache = {};
onMount(async () => { onMount(async () => {
await store.load();
const values = await store.entries(); const values = await store.entries();
for (const [key, value] of values) { for (const [key, value] of values) {
cache[key] = value; cache[key] = value;
@ -19,16 +18,14 @@
cache = cache; cache = cache;
}); });
function write(key, value) { async function write(key, value) {
store try {
.set(key, value) await store.set(key, value);
.then(() => store.get(key)) const v = await store.get(key);
.then((v) => { cache[key] = v;
cache[key] = v; } catch (error) {
cache = cache; onMessage(error);
}) }
.then(() => store.save())
.catch(onMessage);
} }
</script> </script>

@ -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__})}

@ -32,26 +32,110 @@ export async function createStore(
path: string, path: string,
options?: StoreOptions options?: StoreOptions
): Promise<Store> { ): Promise<Store> {
const resourceId = await invoke<number>('plugin:store|create_store', { return await Store.createStore(path, options)
path,
...options
})
return new Store(resourceId, path)
} }
/** /**
* @param path: Path of the store in the rust side * @param path: Path of the store in the rust side
*/ */
export async function getStore(path: string): Promise<Store | undefined> { export async function getStore(path: string): Promise<Store | undefined> {
const resourceId = await invoke<number | null>('plugin:store|get_store') return await Store.getStore(path)
return resourceId ? new Store(resourceId, path) : undefined
} }
/** /**
* 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<Store>
constructor( constructor(
private readonly path: string,
private readonly options?: StoreOptions
) {}
public get store(): Promise<Store> {
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<void> {
return (await this.store).set(key, value)
}
async get<T>(key: string): Promise<T | null> {
return (await this.store).get<T>(key)
}
async has(key: string): Promise<boolean> {
return (await this.store).has(key)
}
async delete(key: string): Promise<boolean> {
return (await this.store).delete(key)
}
async clear(): Promise<void> {
await (await this.store).clear()
}
async reset(): Promise<void> {
await (await this.store).reset()
}
async keys(): Promise<string[]> {
return (await this.store).keys()
}
async values<T>(): Promise<T[]> {
return (await this.store).values<T>()
}
async entries<T>(): Promise<Array<[key: string, value: T]>> {
return (await this.store).entries<T>()
}
async length(): Promise<number> {
return (await this.store).length()
}
async load(): Promise<void> {
await (await this.store).load()
}
async save(): Promise<void> {
await (await this.store).save()
}
async onKeyChange<T>(
key: string,
cb: (value: T | null) => void
): Promise<UnlistenFn> {
return (await this.store).onKeyChange<T>(key, cb)
}
async onChange<T>(
cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> {
return (await this.store).onChange<T>(cb)
}
async close(): Promise<void> {
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, rid: number,
private readonly path: string 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 * @throws If a store at that path already exists
* @param value */
* @returns static async createStore(
path: string,
options?: StoreOptions
): Promise<Store> {
const resourceId = await invoke<number>('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<Store | undefined> {
const resourceId = await invoke<number | null>('plugin:store|get_store')
return resourceId ? new Store(resourceId, path) : undefined
}
async set(key: string, value: unknown): Promise<void> { async set(key: string, value: unknown): Promise<void> {
await invoke('plugin:store|set', { await invoke('plugin:store|set', {
rid: this.rid, 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<T>(key: string): Promise<T | null> { async get<T>(key: string): Promise<T | null> {
return await invoke('plugin:store|get', { return await invoke('plugin:store|get', {
rid: this.rid, 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<boolean> { async has(key: string): Promise<boolean> {
return await invoke('plugin:store|has', { return await invoke('plugin:store|has', {
rid: this.rid, 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<boolean> { async delete(key: string): Promise<boolean> {
return await invoke('plugin:store|delete', { return await invoke('plugin:store|delete', {
rid: this.rid, rid: this.rid,
@ -112,84 +196,156 @@ export class Store extends Resource {
}) })
} }
async clear(): Promise<void> {
await invoke('plugin:store|clear', { rid: this.rid })
}
async reset(): Promise<void> {
await invoke('plugin:store|reset', { rid: this.rid })
}
async keys(): Promise<string[]> {
return await invoke('plugin:store|keys', { rid: this.rid })
}
async values<T>(): Promise<T[]> {
return await invoke('plugin:store|values', { rid: this.rid })
}
async entries<T>(): Promise<Array<[key: string, value: T]>> {
return await invoke('plugin:store|entries', { rid: this.rid })
}
async length(): Promise<number> {
return await invoke('plugin:store|length', { rid: this.rid })
}
async load(): Promise<void> {
await invoke('plugin:store|load', { rid: this.rid })
}
async save(): Promise<void> {
await invoke('plugin:store|save', { rid: this.rid })
}
async onKeyChange<T>(
key: string,
cb: (value: T | null) => void
): Promise<UnlistenFn> {
return await listen<ChangePayload<T>>('store://change', (event) => {
if (event.payload.path === this.path && event.payload.key === key) {
cb(event.payload.value)
}
})
}
async onChange<T>(
cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> {
return await listen<ChangePayload<T>>('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<void>
/**
* Returns the value for the given `key` or `null` if the key does not exist.
*
* @param key
* @returns
*/
get<T>(key: string): Promise<T | null>
/**
* Returns `true` if the given `key` exists in the store.
*
* @param key
* @returns
*/
has(key: string): Promise<boolean>
/**
* Removes a key-value pair from the store.
*
* @param key
* @returns
*/
delete(key: string): Promise<boolean>
/** /**
* Clears the store, removing all key-value pairs. * 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 * @returns
*/ */
async clear(): Promise<void> { clear(): Promise<void>
await invoke('plugin:store|clear', { rid: this.rid })
}
/** /**
* 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`. * If no default value has been set, this method behaves identical to `clear`.
* @returns * @returns
*/ */
async reset(): Promise<void> { reset(): Promise<void>
await invoke('plugin:store|reset', { rid: this.rid })
}
/** /**
* Returns a list of all key in the store. * Returns a list of all keys in the store.
* *
* @returns * @returns
*/ */
async keys(): Promise<string[]> { keys(): Promise<string[]>
return await invoke('plugin:store|keys', { rid: this.rid })
}
/** /**
* Returns a list of all values in the store. * Returns a list of all values in the store.
* *
* @returns * @returns
*/ */
async values<T>(): Promise<T[]> { values<T>(): Promise<T[]>
return await invoke('plugin:store|values', { rid: this.rid })
}
/** /**
* Returns a list of all entries in the store. * Returns a list of all entries in the store.
* *
* @returns * @returns
*/ */
async entries<T>(): Promise<Array<[key: string, value: T]>> { entries<T>(): Promise<Array<[key: string, value: T]>>
return await invoke('plugin:store|entries', { rid: this.rid })
}
/** /**
* Returns the number of key-value pairs in the store. * Returns the number of key-value pairs in the store.
* *
* @returns * @returns
*/ */
async length(): Promise<number> { length(): Promise<number>
return await invoke('plugin:store|length', { rid: this.rid })
}
/** /**
* 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. * 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. * Note: This method does not emit change events.
* @returns * @returns
*/ */
async load(): Promise<void> { load(): Promise<void>
await invoke('plugin:store|load', { rid: this.rid })
}
/** /**
* 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. * This method lets you persist the store to disk whenever you deem necessary.
* @returns * @returns
*/ */
async save(): Promise<void> { save(): Promise<void>
await invoke('plugin:store|save', { rid: this.rid })
}
/** /**
* Listen to changes on a store key. * Listen to changes on a store key.
@ -199,16 +355,10 @@ export class Store extends Resource {
* *
* @since 2.0.0 * @since 2.0.0
*/ */
async onKeyChange<T>( onKeyChange<T>(
key: string, key: string,
cb: (value: T | null) => void cb: (value: T | null) => void
): Promise<UnlistenFn> { ): Promise<UnlistenFn>
return await listen<ChangePayload<T>>('store://change', (event) => {
if (event.payload.path === this.path && event.payload.key === key) {
cb(event.payload.value)
}
})
}
/** /**
* Listen to changes on the store. * Listen to changes on the store.
@ -217,13 +367,11 @@ export class Store extends Resource {
* *
* @since 2.0.0 * @since 2.0.0
*/ */
async onChange<T>( onChange<T>(cb: (key: string, value: T | null) => void): Promise<UnlistenFn>
cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> { /**
return await listen<ChangePayload<T>>('store://change', (event) => { * Close the store and cleans up this resource from memory.
if (event.payload.path === this.path) { * **You should not call any method on this object anymore and should drop any reference to it.**
cb(event.payload.key, event.payload.value) */
} close(): Promise<void>
})
}
} }

@ -463,6 +463,9 @@ impl<R: Runtime> Store<R> {
self.store.lock().unwrap().save() 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) { pub fn close_store(self) {
let store = self.store.lock().unwrap(); let store = self.store.lock().unwrap();
let app = store.app.clone(); let app = store.app.clone();

Loading…
Cancel
Save