Merge pull request #29 from tauri-apps/sync-prs/store

fix(store): fix typings
pull/46/head
Jonas Kruckenberg 2 years ago committed by GitHub
commit b191626cbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,176 +1,176 @@
import { invoke } from '@tauri-apps/api/tauri';
import { appWindow } from '@tauri-apps/api/window';
// Copyright 2021 Tauri Programme within The Commons Conservancy
/**
* A key-value store persisted by the backend layer.
*/
class Store {
constructor(path) {
this.path = path;
}
/**
* Inserts a key-value pair into the store.
*
* @param key
* @param value
* @returns
*/
async set(key, value) {
await 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(key) {
return await invoke("plugin:store|get", {
path: this.path,
key,
});
}
/**
* Returns `true` if the given `key` exists in the store.
*
* @param key
* @returns
*/
async has(key) {
return await invoke("plugin:store|has", {
path: this.path,
key,
});
}
/**
* Removes a key-value pair from the store.
*
* @param key
* @returns
*/
async delete(key) {
return await 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() {
return await 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() {
return await invoke("plugin:store|reset", {
path: this.path,
});
}
/**
* Returns a list of all key in the store.
*
* @returns
*/
async keys() {
return await invoke("plugin:store|keys", {
path: this.path,
});
}
/**
* Returns a list of all values in the store.
*
* @returns
*/
async values() {
return await invoke("plugin:store|values", {
path: this.path,
});
}
/**
* Returns a list of all entries in the store.
*
* @returns
*/
async entries() {
return await invoke("plugin:store|entries", {
path: this.path,
});
}
/**
* Returns the number of key-value pairs in the store.
*
* @returns
*/
async length() {
return await 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() {
return await invoke("plugin:store|load", {
path: this.path,
});
}
/**
* Saves the store to disk at the stores `path`.
*
* As the store is only persistet to disk before the apps exit, changes might be lost in a crash.
* This method let's you persist the store to disk whenever you deem necessary.
* @returns
*/
async save() {
return await 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.
*/
async onKeyChange(key, cb) {
return await appWindow.listen("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.
*/
async onChange(cb) {
return await appWindow.listen("store://change", (event) => {
if (event.payload.path === this.path) {
cb(event.payload.key, event.payload.value);
}
});
}
// Copyright 2021 Tauri Programme within The Commons Conservancy
/**
* A key-value store persisted by the backend layer.
*/
class Store {
constructor(path) {
this.path = path;
}
/**
* Inserts a key-value pair into the store.
*
* @param key
* @param value
* @returns
*/
async set(key, value) {
return await 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(key) {
return await invoke("plugin:store|get", {
path: this.path,
key,
});
}
/**
* Returns `true` if the given `key` exists in the store.
*
* @param key
* @returns
*/
async has(key) {
return await invoke("plugin:store|has", {
path: this.path,
key,
});
}
/**
* Removes a key-value pair from the store.
*
* @param key
* @returns
*/
async delete(key) {
return await 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() {
return await 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() {
return await invoke("plugin:store|reset", {
path: this.path,
});
}
/**
* Returns a list of all key in the store.
*
* @returns
*/
async keys() {
return await invoke("plugin:store|keys", {
path: this.path,
});
}
/**
* Returns a list of all values in the store.
*
* @returns
*/
async values() {
return await invoke("plugin:store|values", {
path: this.path,
});
}
/**
* Returns a list of all entries in the store.
*
* @returns
*/
async entries() {
return await invoke("plugin:store|entries", {
path: this.path,
});
}
/**
* Returns the number of key-value pairs in the store.
*
* @returns
*/
async length() {
return await 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() {
return await 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() {
return await 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.
*/
async onKeyChange(key, cb) {
return await appWindow.listen("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.
*/
async onChange(cb) {
return await appWindow.listen("store://change", (event) => {
if (event.payload.path === this.path) {
cb(event.payload.key, event.payload.value);
}
});
}
}
export { Store };

@ -1 +1 @@
{"version":3,"file":"index.mjs","sources":["../index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAAA;AAcA;;AAEG;MACU,KAAK,CAAA;AAEhB,IAAA,WAAA,CAAY,IAAY,EAAA;AACtB,QAAA,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;KAClB;AAED;;;;;;AAMG;AACH,IAAA,MAAM,GAAG,CAAC,GAAW,EAAE,KAAc,EAAA;QACnC,MAAM,MAAM,CAAC,kBAAkB,EAAE;YAC/B,IAAI,EAAE,IAAI,CAAC,IAAI;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ChB,CAAC,KAAK,KAAI;AACR,YAAA,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,KAAK,GAAG,EAAE;AACjE,gBAAA,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AACzB,aAAA;AACH,SAAC,CACF,CAAC;KACH;AAED;;;;AAIG;IACH,MAAM,QAAQ,CACZ,EAA0C,EAAA;QAE1C,OAAO,MAAM,SAAS,CAAC,MAAM,CAC3B,gBAAgB,EAChB,CAAC,KAAK,KAAI;YACR,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,EAAE;AACpC,gBAAA,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC5C,aAAA;AACH,SAAC,CACF,CAAC;KACH;AACF;;;;"}

@ -29,7 +29,7 @@ export class Store {
* @returns
*/
async set(key: string, value: unknown): Promise<void> {
await invoke("plugin:store|set", {
return await invoke("plugin:store|set", {
path: this.path,
key,
value,
@ -115,7 +115,7 @@ export class Store {
*
* @returns
*/
async values(): Promise<string[]> {
async values<T>(): Promise<T[]> {
return await invoke("plugin:store|values", {
path: this.path,
});
@ -137,7 +137,7 @@ export class Store {
*
* @returns
*/
async length(): Promise<string[]> {
async length(): Promise<number> {
return await invoke("plugin:store|length", {
path: this.path,
});
@ -160,8 +160,8 @@ export class Store {
/**
* Saves the store to disk at the stores `path`.
*
* As the store is only persistet to disk before the apps exit, changes might be lost in a crash.
* This method let's you persist the store to disk whenever you deem necessary.
* 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> {
@ -195,10 +195,10 @@ export class Store {
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*/
async onChange(
cb: (key: string, value: unknown) => void
async onChange<T>(
cb: (key: string, value: T | null) => void
): Promise<UnlistenFn> {
return await appWindow.listen<ChangePayload<unknown>>(
return await appWindow.listen<ChangePayload<T>>(
"store://change",
(event) => {
if (event.payload.path === this.path) {

Loading…
Cancel
Save