@@ -33,7 +33,9 @@ export const unsubscribers: Unsubscriber[] = []
3333export const getAll = async ( name : string ) => {
3434 await ready
3535
36- const tx = db ! . transaction ( name , "readwrite" )
36+ if ( ! db ) return [ ]
37+
38+ const tx = db . transaction ( name , "readwrite" )
3739 const store = tx . objectStore ( name )
3840 const result = await store . getAll ( )
3941
@@ -45,7 +47,9 @@ export const getAll = async (name: string) => {
4547export const bulkPut = async ( name : string , data : any [ ] ) => {
4648 await ready
4749
48- const tx = db ! . transaction ( name , "readwrite" )
50+ if ( ! db ) return
51+
52+ const tx = db . transaction ( name , "readwrite" )
4953 const store = tx . objectStore ( name )
5054
5155 await Promise . all (
@@ -64,7 +68,9 @@ export const bulkPut = async (name: string, data: any[]) => {
6468export const bulkDelete = async ( name : string , ids : string [ ] ) => {
6569 await ready
6670
67- const tx = db ! . transaction ( name , "readwrite" )
71+ if ( ! db ) return
72+
73+ const tx = db . transaction ( name , "readwrite" )
6874 const store = tx . objectStore ( name )
6975
7076 await Promise . all ( ids . map ( id => store . delete ( id ) ) )
@@ -76,43 +82,52 @@ export const initStorage = async (
7682 version : number ,
7783 adapters : Record < string , StorageAdapter > ,
7884) => {
79- if ( ! window . indexedDB ) return
85+ if ( ! window . indexedDB ) {
86+ console . warn ( "IndexedDB not available, running without persistence" )
87+ ready . resolve ( )
88+ return
89+ }
8090
8191 window . addEventListener ( "beforeunload" , ( ) => closeStorage ( ) )
8292
8393 if ( db ) {
8494 throw new Error ( "Db initialized multiple times" )
8595 }
8696
87- db = await openDB ( name , version , {
88- upgrade ( db : IDBPDatabase ) {
89- const names = Object . keys ( adapters )
97+ try {
98+ db = await openDB ( name , version , {
99+ upgrade ( db : IDBPDatabase ) {
100+ const names = Object . keys ( adapters )
90101
91- for ( const name of db . objectStoreNames ) {
92- if ( ! names . includes ( name ) ) {
93- db . deleteObjectStore ( name )
102+ for ( const name of db . objectStoreNames ) {
103+ if ( ! names . includes ( name ) ) {
104+ db . deleteObjectStore ( name )
105+ }
94106 }
95- }
96107
97- for ( const [ name , { keyPath} ] of Object . entries ( adapters ) ) {
98- try {
99- db . createObjectStore ( name , { keyPath} )
100- } catch ( e ) {
101- console . warn ( e )
108+ for ( const [ name , { keyPath} ] of Object . entries ( adapters ) ) {
109+ try {
110+ db . createObjectStore ( name , { keyPath} )
111+ } catch ( e ) {
112+ console . warn ( e )
113+ }
102114 }
103- }
104- } ,
105- } )
115+ } ,
116+ } )
106117
107- ready . resolve ( )
118+ ready . resolve ( )
108119
109- await Promise . all (
110- Object . values ( adapters ) . map ( async adapter => {
111- await adapter . init ( )
120+ await Promise . all (
121+ Object . values ( adapters ) . map ( async adapter => {
122+ await adapter . init ( )
112123
113- unsubscribers . push ( adapter . sync ( ) )
114- } ) ,
115- )
124+ unsubscribers . push ( adapter . sync ( ) )
125+ } ) ,
126+ )
127+ } catch ( e ) {
128+ console . error ( "Failed to initialize IndexedDB:" , e )
129+ ready . resolve ( )
130+ }
116131}
117132
118133export const closeStorage = async ( ) => {
0 commit comments