From 518c80bb1d2da36dddfd9604b3f4b5ae0c0a6ace Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 9 Feb 2026 11:59:58 -0800 Subject: [PATCH] Fix indexeddb failure --- src/lib/indexeddb.ts | 55 +++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/src/lib/indexeddb.ts b/src/lib/indexeddb.ts index 3ecab164..5da73eaf 100644 --- a/src/lib/indexeddb.ts +++ b/src/lib/indexeddb.ts @@ -21,37 +21,45 @@ export class IDB { adapters: IDBAdapters = [] connection: Maybe> unsubscribers: Maybe + 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 - this.connection = openDB(name, version, { - upgrade(idbDb: IDBPDatabase) { - const names = new Set(adapters.map(a => a.name)) + try { + this.connection = openDB(name, version, { + upgrade(idbDb: IDBPDatabase) { + const names = new Set(adapters.map(a => a.name)) - for (const table of idbDb.objectStoreNames) { - if (!names.has(table)) { - idbDb.deleteObjectStore(table) + for (const table of idbDb.objectStoreNames) { + if (!names.has(table)) { + idbDb.deleteObjectStore(table) + } } - } - for (const {name, keyPath} of adapters) { - try { - idbDb.createObjectStore(name, {keyPath}) - } catch (e) { - console.warn(e) + for (const {name, keyPath} of adapters) { + try { + idbDb.createObjectStore(name, {keyPath}) + } catch (e) { + console.warn(e) + } } - } - }, - blocked() {}, - blocking() {}, - }) + }, + blocked() {}, + 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 (table: string): Promise => { 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 (table: string, data: Iterable) => { 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) => { const connection = await this.connect() + + if (!connection) return + const tx = connection.transaction(table, "readwrite") const store = tx.objectStore(table)