Fix indexeddb failure
This commit is contained in:
parent
0067d049e6
commit
518c80bb1d
1 changed files with 36 additions and 19 deletions
|
|
@ -21,14 +21,16 @@ export class IDB {
|
|||
adapters: IDBAdapters = []
|
||||
connection: Maybe<Promise<IDBPDatabase>>
|
||||
unsubscribers: Maybe<Unsubscriber[]>
|
||||
failedToConnect = false
|
||||
|
||||
constructor(readonly options: IDBOptions) {}
|
||||
|
||||
async connect() {
|
||||
if (!this.connection) {
|
||||
if (!this.failedToConnect && !this.connection) {
|
||||
const {name, version} = this.options
|
||||
const adapters = this.adapters
|
||||
|
||||
try {
|
||||
this.connection = openDB(name, version, {
|
||||
upgrade(idbDb: IDBPDatabase) {
|
||||
const names = new Set(adapters.map(a => a.name))
|
||||
|
|
@ -51,7 +53,13 @@ export class IDB {
|
|||
blocking() {},
|
||||
})
|
||||
|
||||
this.unsubscribers = await Promise.all(adapters.map(({name, init}) => init(this.table(name))))
|
||||
this.unsubscribers = await Promise.all(
|
||||
adapters.map(({name, init}) => init(this.table(name))),
|
||||
)
|
||||
} catch (e) {
|
||||
console.error("Failed to connect to indexeddb", e)
|
||||
this.failedToConnect = true
|
||||
}
|
||||
}
|
||||
|
||||
return this.connection
|
||||
|
|
@ -61,6 +69,9 @@ export class IDB {
|
|||
|
||||
getAll = async <T>(table: string): Promise<T[]> => {
|
||||
const connection = await this.connect()
|
||||
|
||||
if (!connection) return []
|
||||
|
||||
const tx = connection.transaction(table, "readwrite")
|
||||
const store = tx.objectStore(table)
|
||||
const result = await store.getAll()
|
||||
|
|
@ -72,6 +83,9 @@ export class IDB {
|
|||
|
||||
bulkPut = async <T>(table: string, data: Iterable<T>) => {
|
||||
const connection = await this.connect()
|
||||
|
||||
if (!connection) return
|
||||
|
||||
const tx = connection.transaction(table, "readwrite")
|
||||
const store = tx.objectStore(table)
|
||||
|
||||
|
|
@ -90,6 +104,9 @@ export class IDB {
|
|||
|
||||
bulkDelete = async (table: string, ids: Iterable<string>) => {
|
||||
const connection = await this.connect()
|
||||
|
||||
if (!connection) return
|
||||
|
||||
const tx = connection.transaction(table, "readwrite")
|
||||
const store = tx.objectStore(table)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue