@@ -19,7 +19,7 @@ import { getUA, isIndexedDBAvailable } from '@firebase/util';
19
19
20
20
import { debugAssert } from '../util/assert' ;
21
21
import { Code , FirestoreError } from '../util/error' ;
22
- import { logDebug , logError } from '../util/log' ;
22
+ import { logDebug , logError , logWarn } from '../util/log' ;
23
23
import { Deferred } from '../util/promise' ;
24
24
25
25
import { PersistencePromise } from './persistence_promise' ;
@@ -359,6 +359,26 @@ export class SimpleDb {
359
359
} ) ;
360
360
} ;
361
361
} ) ;
362
+
363
+ this . db . addEventListener (
364
+ 'close' ,
365
+ ( ) => {
366
+ // Null out this.db if the IndexedDb database connection is closed
367
+ // unexpectedly, as opposed to being closed via a call to this.close()
368
+ // (see dpjg74s26h). Such an unexpected close could occur, for
369
+ // example, by a user explicitly clearing the storage for a website in
370
+ // a browser.
371
+ if ( this . db ) {
372
+ this . db = undefined ;
373
+ logWarn (
374
+ LOG_TAG ,
375
+ 'Database unexpectedly closed, ' +
376
+ 'possibly due to browser data being cleared for this web site'
377
+ ) ;
378
+ }
379
+ } ,
380
+ { passive : true }
381
+ ) ;
362
382
}
363
383
364
384
if ( this . versionchangelistener ) {
@@ -453,10 +473,11 @@ export class SimpleDb {
453
473
}
454
474
455
475
close ( ) : void {
456
- if ( this . db ) {
457
- this . db . close ( ) ;
458
- }
476
+ // Set this.db=undefined before calling this.db.close() so that the "close"
477
+ // event listener (see dpjg74s26h) won't log a spurious warning message.
478
+ const db = this . db ;
459
479
this . db = undefined ;
480
+ db ?. close ( ) ;
460
481
}
461
482
}
462
483
0 commit comments