| |
| |
| |
|
|
| import { listen, type UnlistenFn } from '@tauri-apps/api/event' |
|
|
| import { invoke, Resource } from '@tauri-apps/api/core' |
|
|
| interface ChangePayload<T> { |
| path: string |
| resourceId?: number |
| key: string |
| value: T |
| exists: boolean |
| } |
|
|
| |
| |
| |
| export type StoreOptions = { |
| |
| |
| |
| defaults: { [key: string]: unknown } |
| |
| |
| |
| autoSave?: boolean | number |
| |
| |
| |
| serializeFnName?: string |
| |
| |
| |
| deserializeFnName?: string |
| |
| |
| |
| createNew?: boolean |
| |
| |
| |
| overrideDefaults?: boolean |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function load( |
| path: string, |
| options?: StoreOptions |
| ): Promise<Store> { |
| return await Store.load(path, options) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function getStore(path: string): Promise<Store | null> { |
| return await Store.get(path) |
| } |
|
|
| |
| |
| |
| export class LazyStore implements IStore { |
| private _store?: Promise<Store> |
|
|
| private get store(): Promise<Store> { |
| if (!this._store) { |
| this._store = load(this.path, this.options) |
| } |
| return this._store |
| } |
|
|
| |
| |
| |
| |
| |
| constructor( |
| private readonly path: string, |
| private readonly options?: StoreOptions |
| ) {} |
|
|
| |
| |
| |
| async init(): Promise<void> { |
| await this.store |
| } |
|
|
| async set(key: string, value: unknown): Promise<void> { |
| return (await this.store).set(key, value) |
| } |
|
|
| async get<T>(key: string): Promise<T | undefined> { |
| 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 reload(options?: ReloadOptions): Promise<void> { |
| await (await this.store).reload(options) |
| } |
|
|
| async save(): Promise<void> { |
| await (await this.store).save() |
| } |
|
|
| async onKeyChange<T>( |
| key: string, |
| cb: (value: T | undefined) => void |
| ): Promise<UnlistenFn> { |
| return (await this.store).onKeyChange<T>(key, cb) |
| } |
|
|
| async onChange<T>( |
| cb: (key: string, value: T | undefined) => void |
| ): Promise<UnlistenFn> { |
| return (await this.store).onChange<T>(cb) |
| } |
|
|
| async close(): Promise<void> { |
| if (this._store) { |
| await (await this._store).close() |
| } |
| } |
| } |
|
|
| |
| |
| |
| export class Store extends Resource implements IStore { |
| private constructor(rid: number) { |
| super(rid) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static async load(path: string, options?: StoreOptions): Promise<Store> { |
| const rid = await invoke<number>('plugin:store|load', { |
| path, |
| options |
| }) |
| return new Store(rid) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static async get(path: string): Promise<Store | null> { |
| return await invoke<number | null>('plugin:store|get_store', { path }).then( |
| (rid) => (rid ? new Store(rid) : null) |
| ) |
| } |
|
|
| async set(key: string, value: unknown): Promise<void> { |
| await invoke('plugin:store|set', { |
| rid: this.rid, |
| key, |
| value |
| }) |
| } |
|
|
| async get<T>(key: string): Promise<T | undefined> { |
| const [value, exists] = await invoke<[T, boolean]>('plugin:store|get', { |
| rid: this.rid, |
| key |
| }) |
| return exists ? value : undefined |
| } |
|
|
| async has(key: string): Promise<boolean> { |
| return await invoke('plugin:store|has', { |
| rid: this.rid, |
| key |
| }) |
| } |
|
|
| async delete(key: string): Promise<boolean> { |
| return await invoke('plugin:store|delete', { |
| rid: this.rid, |
| key |
| }) |
| } |
|
|
| 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 reload(options?: ReloadOptions): Promise<void> { |
| await invoke('plugin:store|reload', { rid: this.rid, ...options }) |
| } |
|
|
| async save(): Promise<void> { |
| await invoke('plugin:store|save', { rid: this.rid }) |
| } |
|
|
| async onKeyChange<T>( |
| key: string, |
| cb: (value: T | undefined) => void |
| ): Promise<UnlistenFn> { |
| return await listen<ChangePayload<T>>('store://change', (event) => { |
| if (event.payload.resourceId === this.rid && event.payload.key === key) { |
| cb(event.payload.exists ? event.payload.value : undefined) |
| } |
| }) |
| } |
|
|
| async onChange<T>( |
| cb: (key: string, value: T | undefined) => void |
| ): Promise<UnlistenFn> { |
| return await listen<ChangePayload<T>>('store://change', (event) => { |
| if (event.payload.resourceId === this.rid) { |
| cb( |
| event.payload.key, |
| event.payload.exists ? event.payload.value : undefined |
| ) |
| } |
| }) |
| } |
| } |
|
|
| interface IStore { |
| |
| |
| |
| |
| |
| |
| |
| set(key: string, value: unknown): Promise<void> |
|
|
| |
| |
| |
| |
| |
| |
| get<T>(key: string): Promise<T | undefined> |
|
|
| |
| |
| |
| |
| |
| |
| has(key: string): Promise<boolean> |
|
|
| |
| |
| |
| |
| |
| |
| delete(key: string): Promise<boolean> |
|
|
| |
| |
| |
| |
| |
| |
| clear(): Promise<void> |
|
|
| |
| |
| |
| |
| |
| |
| reset(): Promise<void> |
|
|
| |
| |
| |
| |
| |
| keys(): Promise<string[]> |
|
|
| |
| |
| |
| |
| |
| values<T>(): Promise<T[]> |
|
|
| |
| |
| |
| |
| |
| entries<T>(): Promise<Array<[key: string, value: T]>> |
|
|
| |
| |
| |
| |
| |
| length(): Promise<number> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| reload(options?: ReloadOptions): Promise<void> |
|
|
| |
| |
| |
| |
| save(): Promise<void> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| onKeyChange<T>( |
| key: string, |
| cb: (value: T | undefined) => void |
| ): Promise<UnlistenFn> |
|
|
| |
| |
| |
| |
| |
| |
| |
| onChange<T>( |
| cb: (key: string, value: T | undefined) => void |
| ): Promise<UnlistenFn> |
|
|
| |
| |
| |
| |
| close(): Promise<void> |
| } |
|
|
| |
| |
| |
| export type ReloadOptions = { |
| |
| |
| |
| ignoreDefaults?: boolean |
| } |
|
|