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 {
isAvailable(): Promise<boolean>
isAvailable(): boolean
query(str: string): Promise<MetadataCache[]>
find(key: HexKey): Promise<MetadataCache | undefined>
add(user: MetadataCache): Promise<any>

View File

@ -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<HexKey, MetadataCache>, 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