Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix localStorageStore.reset() does not remove all items #8161

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/ra-core/src/store/localStorageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,13 @@ export const localStorageStore = (
},
reset(): void {
const storage = getStorage();
for (let i = 0; i < storage.length; i++) {
if (storage.key(i)?.substring(0, prefixLength) === prefix) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that once you remove an item, the index of the next item decreases... So storage.key(i) 'jumps' one item each time we delete one.

const key = storage.key(i)?.substring(prefixLength + 1);
if (!key || !storage.key(i)) return;
// @ts-ignore
storage.removeItem(storage.key(i));
publish(key, undefined);
Object.keys(storage).forEach(key => {
if (key.startsWith(prefix)) {
storage.removeItem(key);
const publishKey = key.substring(prefixLength + 1);
publish(publishKey, undefined);
}
}
});
},
subscribe: (key: string, callback: (value: string) => void) => {
const id = Math.random().toString();
Expand Down