@@ -133,51 +133,49 @@ function createStore(dbName: string, storeName: string): UseStore {
133133 }
134134
135135 // Proactive IDB health check when tab returns to foreground.
136- // Safari kills IDB connections for backgrounded tabs. By probing before
137- // the ReconnectApp write storm hits, we drop the stale dbp early so the
138- // first real operation opens a fresh connection instead of failing.
139- if ( typeof document !== 'undefined' ) {
140- document . addEventListener ( 'visibilitychange' , ( ) => {
141- if ( document . visibilityState !== 'visible' || ! dbp ) {
142- return ;
143- }
136+ // Safari kills IDB connections for backgrounded tabs. By probing as soon as
137+ // the tab becomes visible, we drop the stale dbp early so the first real
138+ // operation opens a fresh connection instead of failing.
139+ document . addEventListener ( 'visibilitychange' , ( ) => {
140+ if ( document . visibilityState !== 'visible' || ! dbp ) {
141+ return ;
142+ }
144143
145- Logger . logInfo ( 'IDB visibilitychange probe: tab became visible, checking connection health' , { dbName, storeName} ) ;
144+ Logger . logInfo ( 'IDB visibilitychange probe: tab became visible, checking connection health' , { dbName, storeName} ) ;
146145
147- const probePromise = dbp ;
146+ const probePromise = dbp ;
148147
149- const dropCacheIfStale = ( error : unknown ) => {
150- if ( dbp !== probePromise || ! isStaleConnectionError ( error ) ) {
151- return ;
152- }
153- Logger . logAlert ( 'IDB visibilitychange probe: stale connection detected, dropping cached connection' , {
154- dbName,
155- storeName,
156- errorMessage : error instanceof Error ? error . message : String ( error ) ,
157- } ) ;
158- dbp = undefined ;
159- } ;
160-
161- probePromise . then ( ( db ) => {
162- if ( dbp !== probePromise ) {
163- return ;
164- }
165- try {
166- const tx = db . transaction ( storeName , 'readonly' ) ;
167- const probeStore = tx . objectStore ( storeName ) ;
168- const req = probeStore . count ( ) ;
169- req . onsuccess = ( ) => {
170- Logger . logInfo ( 'IDB visibilitychange probe: connection is healthy' , { dbName, storeName} ) ;
171- } ;
172- req . onerror = ( ) => {
173- dropCacheIfStale ( req . error ) ;
174- } ;
175- } catch ( error ) {
176- dropCacheIfStale ( error ) ;
177- }
148+ const dropCacheIfStale = ( error : unknown ) => {
149+ if ( dbp !== probePromise || ! isStaleConnectionError ( error ) ) {
150+ return ;
151+ }
152+ Logger . logAlert ( 'IDB visibilitychange probe: stale connection detected, dropping cached connection' , {
153+ dbName,
154+ storeName,
155+ errorMessage : error instanceof Error ? error . message : String ( error ) ,
178156 } ) ;
157+ dbp = undefined ;
158+ } ;
159+
160+ probePromise . then ( ( db ) => {
161+ if ( dbp !== probePromise ) {
162+ return ;
163+ }
164+ try {
165+ const tx = db . transaction ( storeName , 'readonly' ) ;
166+ const probeStore = tx . objectStore ( storeName ) ;
167+ const req = probeStore . count ( ) ;
168+ req . onsuccess = ( ) => {
169+ Logger . logInfo ( 'IDB visibilitychange probe: connection is healthy' , { dbName, storeName} ) ;
170+ } ;
171+ req . onerror = ( ) => {
172+ dropCacheIfStale ( req . error ) ;
173+ } ;
174+ } catch ( error ) {
175+ dropCacheIfStale ( error ) ;
176+ }
179177 } ) ;
180- }
178+ } ) ;
181179
182180 // Handles three recoverable error classes:
183181 // 1. InvalidStateError — connection closed between getDB() resolving and db.transaction().
0 commit comments