Skip to content

Commit

Permalink
feat: add include support to persist plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 30, 2023
1 parent 8b63427 commit d303eb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/store/src/@types/plugin.ts
Expand Up @@ -7,6 +7,7 @@ export interface PersistPluginOptions {
key?: string;
env?: 'react' | 'react-native';
exclude?: string[];
include?: string[];
}

export type Hook =
Expand Down
25 changes: 19 additions & 6 deletions packages/store/src/plugins/persist.ts
Expand Up @@ -11,6 +11,7 @@ export class PersistedState implements PluginClass {
key: 'hana-store',
env: 'react',
exclude: [],
include: [],
};

public constructor(options?: PersistPluginOptions) {
Expand Down Expand Up @@ -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)
);
}

Expand Down

0 comments on commit d303eb4

Please sign in to comment.