fix: remove promise from isAvailable

This commit is contained in:
Alejandro Gomez 2023-01-23 13:41:08 +01:00
parent 71e92db24c
commit 186cdbeadd
No known key found for this signature in database
GPG Key ID: 4DF39E566658C817
2 changed files with 11 additions and 8 deletions

View File

@ -40,7 +40,7 @@ export function mapEventToProfile(ev: TaggedRawEvent) {
} }
export interface UsersDb { export interface UsersDb {
isAvailable(): Promise<boolean> isAvailable(): boolean
query(str: string): Promise<MetadataCache[]> query(str: string): Promise<MetadataCache[]>
find(key: HexKey): Promise<MetadataCache | undefined> find(key: HexKey): Promise<MetadataCache | undefined>
add(user: MetadataCache): Promise<any> add(user: MetadataCache): Promise<any>

View File

@ -5,7 +5,7 @@ import { UsersDb, MetadataCache, setUsers } from "State/Users";
import store from "State/Store"; import store from "State/Store";
class IndexedDb implements UsersDb { class IndexedDb implements UsersDb {
async isAvailable() { isAvailable() {
try { try {
const req = "indexedDb" in window && window.indexedDB.open('test', 1) const req = "indexedDb" in window && window.indexedDB.open('test', 1)
return Boolean(req) return Boolean(req)
@ -58,7 +58,7 @@ function groupByPubkey(acc: Record<HexKey, MetadataCache>, user: MetadataCache)
} }
class ReduxUsersDb implements UsersDb { class ReduxUsersDb implements UsersDb {
async isAvailable() { return true } isAvailable() { return true }
async query(q: string) { async query(q: string) {
const state = store.getState() const state = store.getState()
@ -128,10 +128,13 @@ class ReduxUsersDb implements UsersDb {
export const indexedDb = new IndexedDb() export const indexedDb = new IndexedDb()
export const inMemoryDb = new ReduxUsersDb() export const inMemoryDb = new ReduxUsersDb()
let db: UsersDb = inMemoryDb const isIndexedDbAvailable = indexedDb.isAvailable()
indexedDb.isAvailable().then(() => { const db: UsersDb = isIndexedDbAvailable ? indexedDb : inMemoryDb;
console.debug('IndexedDB available')
db = indexedDb if (isIndexedDbAvailable) {
}) console.debug('Using Indexed DB')
} else {
console.debug('Using in-memory DB')
}
export default db export default db