Skip to content

Commit

Permalink
fix(indexed-db): reset now clears db tables (#2445)
Browse files Browse the repository at this point in the history
* fix(indexed-db): reset now clears db tables

Co-authored-by: @taboca
Co-authored-by: @mquasar

Fix: #2435

* refactor(storage): reset - clear everything in a different way

* chore: move clear localStorage out of clearAllTables

* chore: simplify clearAllTables

* fix: reset state and clear sync storage

* chore: extract session fresh state for state reset

* chore: also clear session storage

---------

Co-authored-by: Fabricio <fabricio.zuardi@luizalabs.com>
Co-authored-by: René Aaron <rene@twentyuno.net>
Co-authored-by: Roland Bewick <roland.bewick@gmail.com>
  • Loading branch information
4 people authored Jul 4, 2023
1 parent 4882f27 commit 898da98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
11 changes: 3 additions & 8 deletions src/extension/background-script/actions/setup/reset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import type { MessageReset } from "~/types";

import db from "../../db";
import state from "../../state";

const reset = async (message: MessageReset) => {
await state.getState().password(null);
state.setState({
accounts: {},
account: null,
connector: null,
currentAccountId: null,
});
await state.getState().saveToStorage();
await state.getState().reset();
await db.clearAllTables();

return { data: { reset: true } };
};
Expand Down
4 changes: 4 additions & 0 deletions src/extension/background-script/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export class DB extends Dexie {
return true;
}

async clearAllTables() {
return Promise.all(this.tables.map((table) => table.clear()));
}

// Loads the data from the browser.storage and adds the data to the IndexedDB.
// This is needed because the IndexedDB is not necessarily persistent,
// BUT maybe there are already entries in the IndexedDB (that depends on the browser).
Expand Down
23 changes: 19 additions & 4 deletions src/extension/background-script/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ const browserStorageKeys = Object.keys(browserStorageDefaults) as Array<

let storage: "sync" | "local" = "sync";

const state = createState<State>((set, get) => ({
const getFreshState = () => ({
connector: null,
account: null,
settings: DEFAULT_SETTINGS,
settings: { ...DEFAULT_SETTINGS },
migrations: [],
accounts: {},
currentAccountId: null,
Expand All @@ -79,6 +79,10 @@ const state = createState<State>((set, get) => ({
// TODO: move bitcoin object to account state and handle encryption/decryption there
bitcoin: null,
mv2Password: null,
});

const state = createState<State>((set, get) => ({
...getFreshState(),
password: async (password) => {
if (isManifestV3) {
if (password) {
Expand Down Expand Up @@ -235,8 +239,19 @@ const state = createState<State>((set, get) => ({
});
},
reset: async () => {
set({ ...browserStorageDefaults });
get().saveToStorage();
try {
// @ts-ignore: https://github.com/mozilla/webextension-polyfill/issues/329
await browser.storage.session.clear();
} catch (error) {
console.error("Failed to clear session storage", error);
}
if (storage === "sync") {
await browser.storage.sync.clear();
} else {
await browser.storage.local.clear();
}
set({ ...getFreshState() });
await get().saveToStorage();
},
saveToStorage: () => {
const current = get();
Expand Down

0 comments on commit 898da98

Please sign in to comment.