feat: fallback to DB localStorage for idb names (#8682)
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@ export class DataManager {
|
|||||||
|
|
||||||
async initDb() {
|
async initDb() {
|
||||||
if (this.db) return this.db;
|
if (this.db) return this.db;
|
||||||
|
const dbName = `cw-store-${this.accountId}`;
|
||||||
this.db = await openDB(`cw-store-${this.accountId}`, DATA_VERSION, {
|
this.db = await openDB(`cw-store-${this.accountId}`, DATA_VERSION, {
|
||||||
upgrade(db) {
|
upgrade(db) {
|
||||||
db.createObjectStore('cache-keys');
|
db.createObjectStore('cache-keys');
|
||||||
@@ -19,6 +20,13 @@ export class DataManager {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Store the database name in LocalStorage
|
||||||
|
const dbNames = JSON.parse(localStorage.getItem('cw-idb-names') || '[]');
|
||||||
|
if (!dbNames.includes(dbName)) {
|
||||||
|
dbNames.push(dbName);
|
||||||
|
localStorage.setItem('cw-idb-names', JSON.stringify(dbNames));
|
||||||
|
}
|
||||||
|
|
||||||
return this.db;
|
return this.db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,10 +44,29 @@ export const clearLocalStorageOnLogout = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const deleteIndexedDBOnLogout = async () => {
|
export const deleteIndexedDBOnLogout = async () => {
|
||||||
const dbs = await window.indexedDB.databases();
|
let dbs = [];
|
||||||
dbs.forEach(db => {
|
try {
|
||||||
window.indexedDB.deleteDatabase(db.name);
|
dbs = await window.indexedDB.databases();
|
||||||
|
dbs = dbs.map(db => db.name);
|
||||||
|
} catch (e) {
|
||||||
|
dbs = JSON.parse(localStorage.getItem('cw-idb-names') || '[]');
|
||||||
|
}
|
||||||
|
|
||||||
|
dbs.forEach(dbName => {
|
||||||
|
const deleteRequest = window.indexedDB.deleteDatabase(dbName);
|
||||||
|
|
||||||
|
deleteRequest.onerror = event => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error(`Error deleting database ${dbName}.`, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
deleteRequest.onsuccess = () => {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`Database ${dbName} deleted successfully.`);
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
localStorage.removeItem('cw-idb-names');
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clearCookiesOnLogout = () => {
|
export const clearCookiesOnLogout = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user