Skip to content

Commit ae8fe28

Browse files
authored
Merge b97925b into dcc62c0
2 parents dcc62c0 + b97925b commit ae8fe28

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

.changeset/slow-students-fry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': patch
3+
'firebase': patch
4+
---
5+
6+
Ensure that IndexedDB connections are discarded if they are closed unexpectedly.

packages/firestore/src/local/simple_db.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getUA, isIndexedDBAvailable } from '@firebase/util';
1919

2020
import { debugAssert } from '../util/assert';
2121
import { Code, FirestoreError } from '../util/error';
22-
import { logDebug, logError } from '../util/log';
22+
import { logDebug, logError, logWarn } from '../util/log';
2323
import { Deferred } from '../util/promise';
2424

2525
import { PersistencePromise } from './persistence_promise';
@@ -359,6 +359,26 @@ 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.
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+
);
362382
}
363383

364384
if (this.versionchangelistener) {
@@ -453,10 +473,11 @@ export class SimpleDb {
453473
}
454474

455475
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;
459479
this.db = undefined;
480+
db?.close();
460481
}
461482
}
462483

0 commit comments

Comments
 (0)