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(indexed-db): reset now clears db tables #2445

Merged
merged 8 commits into from
Jul 4, 2023
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();
fczuardi marked this conversation as resolved.
Show resolved Hide resolved
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
7 changes: 6 additions & 1 deletion src/extension/background-script/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,13 @@ const state = createState<State>((set, get) => ({
});
},
reset: async () => {
if (storage === "sync") {
await browser.storage.sync.clear();
} else {
await browser.storage.local.clear();
}
set({ ...browserStorageDefaults });
get().saveToStorage();
await get().saveToStorage();
},
saveToStorage: () => {
const current = get();
Expand Down