feat(store): load override defaults (#2857)

* feat(store): load override defaults

* Update docs

* Update example

* Allow setting defaults from js

* Tweak resolve

* Merge remote-tracking branch 'upstream/v2' into store-load-override-defaults

* Merge branch 'v2' of https://github.com/tauri-apps/plugins-workspace into store-load-override-defaults

* Merge branch 'v2' into store-load-override-defaults

* Rename to ignore defaults

* Merge remote-tracking branch 'upstream/v2' into store-load-override-defaults
This commit is contained in:
Tony
2025-08-05 18:45:09 +08:00
committed by GitHub
parent e0323ec752
commit 5ac8fbb1fa
7 changed files with 171 additions and 64 deletions
+30 -7
View File
@@ -18,6 +18,10 @@ interface ChangePayload<T> {
* Options to create a store
*/
export type StoreOptions = {
/**
* Default value of the store
*/
defaults: { [key: string]: unknown }
/**
* Auto save on modification with debounce duration in milliseconds, it's 100ms by default, pass in `false` to disable it
*/
@@ -34,6 +38,10 @@ export type StoreOptions = {
* Force create a new store with default values even if it already exists.
*/
createNew?: boolean
/**
* When creating the store, override the store with the on-disk state if it exists, ignoring defaults
*/
overrideDefaults?: boolean
}
/**
@@ -145,8 +153,8 @@ export class LazyStore implements IStore {
return (await this.store).length()
}
async reload(): Promise<void> {
await (await this.store).reload()
async reload(options?: ReloadOptions): Promise<void> {
await (await this.store).reload(options)
}
async save(): Promise<void> {
@@ -196,7 +204,7 @@ export class Store extends Resource implements IStore {
static async load(path: string, options?: StoreOptions): Promise<Store> {
const rid = await invoke<number>('plugin:store|load', {
path,
...options
options
})
return new Store(rid)
}
@@ -280,8 +288,8 @@ export class Store extends Resource implements IStore {
return await invoke('plugin:store|length', { rid: this.rid })
}
async reload(): Promise<void> {
await invoke('plugin:store|reload', { rid: this.rid })
async reload(options?: ReloadOptions): Promise<void> {
await invoke('plugin:store|reload', { rid: this.rid, ...options })
}
async save(): Promise<void> {
@@ -396,10 +404,15 @@ interface IStore {
*
* 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 loads the data and merges it with the current store,
* this behavior will be changed to resetting to default first and then merging with the on-disk state in v3,
* to fully match the store with the on-disk state, set {@linkcode ReloadOptions.ignoreDefaults} to `true`
* - This method does not emit change events.
*
* @returns
*/
reload(): Promise<void>
reload(options?: ReloadOptions): Promise<void>
/**
* Saves the store to disk at the store's `path`.
@@ -437,3 +450,13 @@ interface IStore {
*/
close(): Promise<void>
}
/**
* Options to {@linkcode IStore.reload} a {@linkcode IStore}
*/
export type ReloadOptions = {
/**
* To fully match the store with the on-disk state, ignoring defaults
*/
ignoreDefaults?: boolean
}