mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-25 13:17:47 +02:00
aba07c27b8
Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: FabianLars <fabianlars@fabianlars.de> Co-authored-by: FabianLars <FabianLars@users.noreply.github.com> Co-authored-by: Alexandre Dang <124160233+vdang-crabnebula@users.noreply.github.com> Co-authored-by: Ludea <ludovicw35@hotmail.com> Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com> Co-authored-by: Duke Jones <104690+dukejones@users.noreply.github.com> Co-authored-by: NaokiM03 <37442712+NaokiM03@users.noreply.github.com> Co-authored-by: Thibault <thibault_poisson@orange.fr> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: David Blythe <49919035+writeDavid@users.noreply.github.com> Co-authored-by: Lucas Nogueira <lucas@tauri.studio> fix(stronghold): change wrong argument name for `remove` (#422) fix(window-state): correctly set decoration state if no saved state exists, fixes #421 (#424) fix(stronghold): return null if there is no record (#129) fix(window-state): propagate promise (#435) closes #432 fix(window-state): manual default implentation (#425) fix(window-state): manual default implentation, closes #421 fix(deps): update rust crate iota-crypto to 0.21 (#438) fix readme example (#447) fix: handle recursive directory correctly (#455) fix(deps): update rust crate sqlx to 0.7. plugin-sql msrv is now 1.65 (#464) fix(persisted-scope): separately save asset protocol patterns (#459) fix(deps): update rust crate iota-crypto to 0.22 (#475) fix(deps): update tauri monorepo to v1.4.0 (#482) resolve to v15.1.0 (#489) fix(deps): update rust crate iota-crypto to 0.23 (#495)
213 lines
4.8 KiB
TypeScript
213 lines
4.8 KiB
TypeScript
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import { listen, UnlistenFn } from "@tauri-apps/api/event";
|
|
|
|
declare global {
|
|
interface Window {
|
|
__TAURI_INVOKE__: <T>(cmd: string, args?: unknown) => Promise<T>;
|
|
}
|
|
}
|
|
|
|
interface ChangePayload<T> {
|
|
path: string;
|
|
key: string;
|
|
value: T | null;
|
|
}
|
|
|
|
/**
|
|
* A key-value store persisted by the backend layer.
|
|
*/
|
|
export class Store {
|
|
path: string;
|
|
constructor(path: string) {
|
|
this.path = path;
|
|
}
|
|
|
|
/**
|
|
* Inserts a key-value pair into the store.
|
|
*
|
|
* @param key
|
|
* @param value
|
|
* @returns
|
|
*/
|
|
async set(key: string, value: unknown): Promise<void> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|set", {
|
|
path: this.path,
|
|
key,
|
|
value,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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 window.__TAURI_INVOKE__("plugin:store|get", {
|
|
path: this.path,
|
|
key,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns `true` if the given `key` exists in the store.
|
|
*
|
|
* @param key
|
|
* @returns
|
|
*/
|
|
async has(key: string): Promise<boolean> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|has", {
|
|
path: this.path,
|
|
key,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Removes a key-value pair from the store.
|
|
*
|
|
* @param key
|
|
* @returns
|
|
*/
|
|
async delete(key: string): Promise<boolean> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|delete", {
|
|
path: this.path,
|
|
key,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|clear", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|reset", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns a list of all key in the store.
|
|
*
|
|
* @returns
|
|
*/
|
|
async keys(): Promise<string[]> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|keys", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns a list of all values in the store.
|
|
*
|
|
* @returns
|
|
*/
|
|
async values<T>(): Promise<T[]> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|values", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns a list of all entries in the store.
|
|
*
|
|
* @returns
|
|
*/
|
|
async entries<T>(): Promise<Array<[key: string, value: T]>> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|entries", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Returns the number of key-value pairs in the store.
|
|
*
|
|
* @returns
|
|
*/
|
|
async length(): Promise<number> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|length", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|load", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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> {
|
|
return await window.__TAURI_INVOKE__("plugin:store|save", {
|
|
path: this.path,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
): 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.
|
|
* @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> {
|
|
return await listen<ChangePayload<T>>("store://change", (event) => {
|
|
if (event.payload.path === this.path) {
|
|
cb(event.payload.key, event.payload.value);
|
|
}
|
|
});
|
|
}
|
|
}
|