@@ -19,7 +19,7 @@ import { getUA, isIndexedDBAvailable } from '@firebase/util';
1919
2020import { debugAssert } from '../util/assert' ;
2121import { Code , FirestoreError } from '../util/error' ;
22- import { logDebug , logError } from '../util/log' ;
22+ import { logDebug , logError , logWarn } from '../util/log' ;
2323import { Deferred } from '../util/promise' ;
2424
2525import { PersistencePromise } from './persistence_promise' ;
@@ -359,6 +359,28 @@ export class SimpleDb {
359359 } ) ;
360360 } ;
361361 } ) ;
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. This also avoids using a closed IndexedDb connection,
371+ // which can cause issues
372+ // (e.g. https://github.com/firebase/firebase-js-sdk/issues/8593)
373+ if ( this . db ) {
374+ this . db = undefined ;
375+ logWarn (
376+ LOG_TAG ,
377+ 'Database unexpectedly closed, ' +
378+ 'possibly due to browser data being cleared for this web site'
379+ ) ;
380+ }
381+ } ,
382+ { passive : true }
383+ ) ;
362384 }
363385
364386 if ( this . versionchangelistener ) {
@@ -453,10 +475,11 @@ export class SimpleDb {
453475 }
454476
455477 close ( ) : void {
456- if ( this . db ) {
457- this . db . close ( ) ;
458- }
478+ // Set this.db=undefined before calling this.db.close() so that the "close"
479+ // event listener (see dpjg74s26h) won't log a spurious warning message.
480+ const db = this . db ;
459481 this . db = undefined ;
482+ db ?. close ( ) ;
460483 }
461484}
462485
0 commit comments