Add lazy store

This commit is contained in:
Tony
2024-10-03 10:52:12 +08:00
parent 6cdaa31959
commit 79bc8d08f4
4 changed files with 255 additions and 107 deletions
+241 -93
View File
@@ -32,26 +32,110 @@ export async function createStore(
path: string,
options?: StoreOptions
): Promise<Store> {
const resourceId = await invoke<number>('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<Store | undefined> {
const resourceId = await invoke<number | null>('plugin:store|get_store')
return resourceId ? new Store(resourceId, path) : undefined
return await Store.getStore(path)
}
/**
* 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 LazyStore implements IStore {
private _store?: Promise<Store>
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 {
constructor(
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<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> {
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<T>(key: string): Promise<T | null> {
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<boolean> {
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<boolean> {
return await invoke('plugin:store|delete', {
rid: this.rid,
@@ -112,93 +196,38 @@ export class Store extends Resource {
})
}
/**
* Clears the store, removing all key-value pairs.
*
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
* @returns
*/
async clear(): Promise<void> {
await invoke('plugin:store|clear', { rid: this.rid })
}
/**
* Resets the store to it's `default` value.
*
* If no default value has been set, this method behaves identical to `clear`.
* @returns
*/
async reset(): Promise<void> {
await invoke('plugin:store|reset', { rid: this.rid })
}
/**
* Returns a list of all key in the store.
*
* @returns
*/
async keys(): Promise<string[]> {
return await invoke('plugin:store|keys', { rid: this.rid })
}
/**
* Returns a list of all values in the store.
*
* @returns
*/
async values<T>(): Promise<T[]> {
return await invoke('plugin:store|values', { rid: this.rid })
}
/**
* Returns a list of all entries in the store.
*
* @returns
*/
async 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
*/
async 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.
*
* 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<void> {
await invoke('plugin:store|load', { rid: this.rid })
}
/**
* Saves the store to disk at the stores `path`.
*
* As the store is only persisted to disk before the apps 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<void> {
await invoke('plugin:store|save', { rid: this.rid })
}
/**
* Listen to changes on a store key.
* @param key
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
async onKeyChange<T>(
key: string,
cb: (value: T | null) => void
@@ -210,13 +239,6 @@ export class Store extends Resource {
})
}
/**
* Listen to changes on the store.
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
async onChange<T>(
cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> {
@@ -227,3 +249,129 @@ export class Store extends Resource {
})
}
}
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.
*
* Note: To clear the storage and reset it to its `default` value, use `reset` instead.
* @returns
*/
clear(): Promise<void>
/**
* Resets the store to its `default` value.
*
* If no default value has been set, this method behaves identical to `clear`.
* @returns
*/
reset(): Promise<void>
/**
* Returns a list of all keys in the store.
*
* @returns
*/
keys(): Promise<string[]>
/**
* Returns a list of all values in the store.
*
* @returns
*/
values<T>(): Promise<T[]>
/**
* Returns a list of all entries in the store.
*
* @returns
*/
entries<T>(): Promise<Array<[key: string, value: T]>>
/**
* Returns the number of key-value pairs in the store.
*
* @returns
*/
length(): Promise<number>
/**
* 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
*/
load(): Promise<void>
/**
* Saves the store to disk at the store's `path`.
*
* 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
*/
save(): Promise<void>
/**
* Listen to changes on a store key.
* @param key
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
onKeyChange<T>(
key: string,
cb: (value: T | null) => void
): Promise<UnlistenFn>
/**
* Listen to changes on the store.
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
onChange<T>(cb: (key: string, value: T | null) => void): Promise<UnlistenFn>
/**
* 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<void>
}