Skip to content

Commit

Permalink
feat(storage): added exclude option and clearStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Aug 11, 2021
1 parent 97dc773 commit 77ad7c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion extensions/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export default function storageExtension<TState extends BaseState>(options?: Par
type,
prefix,
sync,
exclude,
serialiser,
parser,
} = {
type: 'local',
prefix: 'harlem',
sync: true,
exclude: [],
serialiser: state => JSON.stringify(state),
parser: value => JSON.parse(value),
...options,
Expand All @@ -38,7 +40,7 @@ export default function storageExtension<TState extends BaseState>(options?: Par
const storageKey = prefix ? `${prefix}:${store.name}` : store.name;

store.on(EVENTS.mutation.success, (event?: EventPayload<MutationEventData>) => {
if (!event || event.data.mutation === '$storage') {
if (!event || event.data.mutation === '$storage' || exclude.includes(event.data.mutation)) {
return;
}

Expand All @@ -63,6 +65,10 @@ export default function storageExtension<TState extends BaseState>(options?: Par
window.removeEventListener('storage', listener);
}

function clearStorage() {
storage.removeItem(storageKey);
}

store.once(EVENTS.store.destroyed, () => stopStorageSync());

if (sync) {
Expand All @@ -72,6 +78,7 @@ export default function storageExtension<TState extends BaseState>(options?: Par
return {
startStorageSync,
stopStorageSync,
clearStorage,
};
};
}
1 change: 1 addition & 0 deletions extensions/storage/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Options<TState extends BaseState> {
type: StorageType;
prefix: string;
sync: boolean;
exclude: string[];
serialiser(state: ReadState<TState>): string;
parser(value: string): TState;
}

0 comments on commit 77ad7c0

Please sign in to comment.