Skip to content

Commit

Permalink
refactor(indexedDB): remove dexie package and add polyfill for firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmcg committed Sep 27, 2022
1 parent dd99d9f commit 180df26
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 270 deletions.
1 change: 0 additions & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const Config = {
ipfs: {
gateway: 'https://satellite.infura-ipfs.io/ipfs/',
},
indexedDbName: 'SatelliteDB',
// Keep in sync with Sounds enum in SoundManager.ts
sounds: {
doesLoop: ['call'],
Expand Down
94 changes: 0 additions & 94 deletions libraries/SatelliteDB/SatelliteDB.test.ts

This file was deleted.

153 changes: 0 additions & 153 deletions libraries/SatelliteDB/SatelliteDB.ts

This file was deleted.

10 changes: 0 additions & 10 deletions libraries/SatelliteDB/__snapshots__/SatelliteDB.test.ts.snap

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"dayjs": "^1.10.8",
"deepmerge": "^4.2.2",
"detectrtc": "^1.4.1",
"dexie": "^3.2.1",
"ed25519-hd-key": "^1.2.0",
"ed2curve": "^0.3.0",
"emoji-regex": "^10.1.0",
Expand Down Expand Up @@ -87,10 +86,10 @@
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"@nuxt/bridge": "npm:@nuxt/bridge-edge",
"@nuxtjs/device": "^2.1.0",
"@nuxtjs/style-resources": "^1.2.1",
"@nuxt/types": "^2.15.8",
"@nuxtjs/device": "^2.1.0",
"@nuxtjs/eslint-config-typescript": "^10.0.0",
"@nuxtjs/style-resources": "^1.2.1",
"@types/ed2curve": "^0.2.2",
"@types/howler": "^2.2.5",
"@types/jest": "^28.1.7",
Expand Down
2 changes: 0 additions & 2 deletions store/settings/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Dexie } from 'dexie'
import * as actions from './actions'
import { SettingsError } from './types'
import { db } from '~/libraries/SatelliteDB/SatelliteDB'

describe('actions.default', () => {
const original = window.location
Expand Down
62 changes: 55 additions & 7 deletions store/settings/actions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,66 @@
import { Dexie } from 'dexie'
import { SettingsError } from '~/store/settings/types'

async function deleteAllDexieDbs() {
const databases = await Dexie.getDatabaseNames()
// begin - firefox polyfill
if (window.indexedDB && typeof window.indexedDB.databases === 'undefined') {
const LOCALSTORAGE_CACHE_KEY = 'indexedDBDatabases'

databases.forEach((name) => {
new Dexie(name).delete()
})
// 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: any) =>
(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.open

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.deleteDatabase

IDBFactory.prototype.deleteDatabase = function (...args) {
const dbName = args[0]
const existing = getFromStorage()
delete existing[dbName]
writeToStorage(existing)
return deleteDatabase.apply(this, args)
}
}
// end polyfill

async function deleteIndexedDb() {
const databases = await indexedDB.databases()
Promise.all(
databases.map(async (db) => {
if (db.name) {
indexedDB.deleteDatabase(db.name)
}
}),
)
}

export default {
async clearLocalStorage() {
try {
await deleteAllDexieDbs()
await deleteIndexedDb()
localStorage.clear()
} catch (e) {
throw new Error(SettingsError.DATABASE_NOT_CLEARED)
Expand Down

0 comments on commit 180df26

Please sign in to comment.