diff --git a/packages/store/src/@types/plugin.ts b/packages/store/src/@types/plugin.ts index ec56b68..15489d5 100644 --- a/packages/store/src/@types/plugin.ts +++ b/packages/store/src/@types/plugin.ts @@ -7,6 +7,7 @@ export interface PersistPluginOptions { key?: string; env?: 'react' | 'react-native'; exclude?: string[]; + include?: string[]; } export type Hook = diff --git a/packages/store/src/plugins/persist.ts b/packages/store/src/plugins/persist.ts index d85ff31..787acf7 100644 --- a/packages/store/src/plugins/persist.ts +++ b/packages/store/src/plugins/persist.ts @@ -11,6 +11,7 @@ export class PersistedState implements PluginClass { key: 'hana-store', env: 'react', exclude: [], + include: [], }; public constructor(options?: PersistPluginOptions) { @@ -68,15 +69,27 @@ export class PersistedState implements PluginClass { return; } - this._options.exclude.forEach((item) => { - if (state[item]) { - delete state[item]; - } - }); + let finalState: State = {}; + + if (this._options.include.length > 0) { + this._options.include.forEach((item) => { + if (state[item]) { + finalState[item] = state[item]; + } + }); + } else { + this._options.exclude.forEach((item) => { + if (state[item]) { + delete state[item]; + } + }); + + finalState = state; + } return await this._options.storage?.setItem?.( this._options.key, - JSON.stringify(state) + JSON.stringify(finalState) ); }