Browse Source

Add ability to purge IndexedDB

develop
Ezerous 4 years ago
parent
commit
a214014ee8
  1. 1
      packages/concordia-app/src/index.jsx
  2. 11
      packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx
  3. 1
      packages/concordia-app/src/options/breezeOptions.js
  4. 20
      packages/concordia-app/src/utils/indexedDB/indexedDBUtils.js
  5. 46
      packages/concordia-app/src/utils/indexedDB/patchIndexedDB.js

1
packages/concordia-app/src/index.jsx

@ -1,3 +1,4 @@
import './utils/indexedDB/patchIndexedDB';
import './utils/wdyr'; import './utils/wdyr';
import React, { Suspense } from 'react'; import React, { Suspense } from 'react';
import { render } from 'react-dom'; import { render } from 'react-dom';

11
packages/concordia-app/src/layouts/MainLayout/MainLayoutMenu/index.jsx

@ -5,6 +5,7 @@ import { useHistory } from 'react-router';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import AppContext from '../../../components/AppContext'; import AppContext from '../../../components/AppContext';
import appLogo from '../../../assets/images/app_logo.png'; import appLogo from '../../../assets/images/app_logo.png';
import purgeIndexedDBs from '../../../utils/indexedDB/indexedDBUtils';
const MainLayoutMenu = () => { const MainLayoutMenu = () => {
const hasSignedUp = useSelector((state) => state.user.hasSignedUp); const hasSignedUp = useSelector((state) => state.user.hasSignedUp);
@ -56,6 +57,16 @@ const MainLayoutMenu = () => {
{t('topbar.button.register')} {t('topbar.button.register')}
</Menu.Item> </Menu.Item>
)} )}
<Menu.Item
link
name="purge"
key="purge"
onClick={async () => {
await purgeIndexedDBs();
}}
>
Purge
</Menu.Item>
</Menu.Menu> </Menu.Menu>
</Menu> </Menu>
)} )}

1
packages/concordia-app/src/options/breezeOptions.js

@ -10,6 +10,7 @@ const REACT_APP_RENDEZVOUS_PORT = process.env.REACT_APP_RENDEZVOUS_PORT || REACT
const breezeOptions = { const breezeOptions = {
ipfs: { ipfs: {
repo: 'concordia',
config: { config: {
Addresses: { Addresses: {
Swarm: [ Swarm: [

20
packages/concordia-app/src/utils/indexedDB/indexedDBUtils.js

@ -0,0 +1,20 @@
import { breeze } from '../../redux/store';
async function purgeIndexedDBs() {
const { ipfs, orbit } = breeze;
if (orbit) await orbit.stop();
if (ipfs) await ipfs.stop();
const dbs = await indexedDB.databases();
await Promise.all(
dbs.map((db) => new Promise(
(resolve, reject) => {
const request = indexedDB.deleteDatabase(db.name);
request.onsuccess = resolve;
request.onerror = reject;
},
)),
);
}
export default purgeIndexedDBs;

46
packages/concordia-app/src/utils/indexedDB/patchIndexedDB.js

@ -0,0 +1,46 @@
/* Patches browsers that do not yet support indexedDB.databases()
(https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/databases)
See also https://gist.github.com/rmehner/b9a41d9f659c9b1c3340#gistcomment-3449418) */
if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') {
const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases';
// Store a key value map of databases
const getFromStorage = () => JSON.parse(window.localStorage[LOCALSTORAGE_CACHE_KEY] || '{}');
// Write the database to local storage
const writeToStorage = (value) => { window.localStorage[LOCALSTORAGE_CACHE_KEY] = JSON.stringify(value); };
IDBFactory.prototype.databases = () => Promise.resolve(
Object.entries(getFromStorage()).reduce((acc, [name, version]) => {
acc.push({ name, version });
return acc;
}, []),
);
// Intercept the existing open handler to write our DBs names
// and versions to localStorage
const { open } = IDBFactory.prototype;
// eslint-disable-next-line func-names
IDBFactory.prototype.open = function (...args) {
const dbName = args[0];
const version = args[1] || 1;
const existing = getFromStorage();
writeToStorage({ ...existing, [dbName]: version });
return open.apply(this, args);
};
// Intercept the existing deleteDatabase handler remove our
// dbNames from localStorage
const { deleteDatabase } = IDBFactory.prototype;
// eslint-disable-next-line func-names
IDBFactory.prototype.deleteDatabase = function (...args) {
const dbName = args[0];
const existing = getFromStorage();
delete existing[dbName];
writeToStorage(existing);
return deleteDatabase.apply(this, args);
};
console.debug('IndexedDB patched successfully!');
}
Loading…
Cancel
Save