diff --git a/src/State/Users.ts b/src/State/Users.ts index 9f92183a..39f707d2 100644 --- a/src/State/Users.ts +++ b/src/State/Users.ts @@ -40,7 +40,7 @@ export function mapEventToProfile(ev: TaggedRawEvent) { } export interface UsersDb { - isAvailable(): Promise + isAvailable(): boolean query(str: string): Promise find(key: HexKey): Promise add(user: MetadataCache): Promise diff --git a/src/State/Users/Db.ts b/src/State/Users/Db.ts index 1f80c27a..85b3226d 100644 --- a/src/State/Users/Db.ts +++ b/src/State/Users/Db.ts @@ -5,7 +5,7 @@ import { UsersDb, MetadataCache, setUsers } from "State/Users"; import store from "State/Store"; class IndexedDb implements UsersDb { - async isAvailable() { + isAvailable() { try { const req = "indexedDb" in window && window.indexedDB.open('test', 1) return Boolean(req) @@ -58,7 +58,7 @@ function groupByPubkey(acc: Record, user: MetadataCache) } class ReduxUsersDb implements UsersDb { - async isAvailable() { return true } + isAvailable() { return true } async query(q: string) { const state = store.getState() @@ -128,10 +128,13 @@ class ReduxUsersDb implements UsersDb { export const indexedDb = new IndexedDb() export const inMemoryDb = new ReduxUsersDb() -let db: UsersDb = inMemoryDb -indexedDb.isAvailable().then(() => { - console.debug('IndexedDB available') - db = indexedDb -}) +const isIndexedDbAvailable = indexedDb.isAvailable() +const db: UsersDb = isIndexedDbAvailable ? indexedDb : inMemoryDb; + +if (isIndexedDbAvailable) { + console.debug('Using Indexed DB') +} else { + console.debug('Using in-memory DB') +} export default db