Skip to content

Commit 68995c2

Browse files
Fix bug in AsyncQueue visibility handler (#3586)
1 parent 2c37560 commit 68995c2

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

.changeset/tidy-elephants-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fixed a bug that caused slow retries for IndexedDB operations even when a webpage re-entered the foreground.

packages/firestore/src/local/indexeddb_persistence.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ export class IndexedDbPersistence implements Persistence {
294294
debugAssert(!this.started, 'IndexedDbPersistence double-started!');
295295
debugAssert(this.window !== null, "Expected 'window' to be defined");
296296

297-
// NOTE: This is expected to fail sometimes (in the case of another tab
298-
// already having the persistence lock), so it's the first thing we should
297+
// NOTE: This is expected to fail sometimes (in the case of another tab
298+
// already having the persistence lock), so it's the first thing we should
299299
// do.
300300
return this.updateClientMetadataAndTryBecomePrimary()
301301
.then(() => {

packages/firestore/src/util/async_queue.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { logDebug, logError } from './log';
2121
import { Deferred } from './promise';
2222
import { ExponentialBackoff } from '../remote/backoff';
2323
import { isIndexedDbTransactionError } from '../local/simple_db';
24-
import { getWindow } from '../platform/dom';
24+
import { getDocument } from '../platform/dom';
2525

2626
const LOG_TAG = 'AsyncQueue';
2727

@@ -235,12 +235,22 @@ export class AsyncQueue {
235235
// Visibility handler that triggers an immediate retry of all retryable
236236
// operations. Meant to speed up recovery when we regain file system access
237237
// after page comes into foreground.
238-
private visibilityHandler = (): void => this.backoff.skipBackoff();
238+
private visibilityHandler: () => void = () => {
239+
const document = getDocument();
240+
if (document) {
241+
logDebug(
242+
LOG_TAG,
243+
'Visibility state changed to ',
244+
document.visibilityState
245+
);
246+
}
247+
this.backoff.skipBackoff();
248+
};
239249

240250
constructor() {
241-
const window = getWindow();
242-
if (window && typeof window.addEventListener === 'function') {
243-
window.addEventListener('visibilitychange', this.visibilityHandler);
251+
const document = getDocument();
252+
if (document && typeof document.addEventListener === 'function') {
253+
document.addEventListener('visibilitychange', this.visibilityHandler);
244254
}
245255
}
246256

@@ -293,9 +303,12 @@ export class AsyncQueue {
293303
this.verifyNotFailed();
294304
if (!this._isShuttingDown) {
295305
this._isShuttingDown = true;
296-
const window = getWindow();
297-
if (window) {
298-
window.removeEventListener('visibilitychange', this.visibilityHandler);
306+
const document = getDocument();
307+
if (document && typeof document.removeEventListener === 'function') {
308+
document.removeEventListener(
309+
'visibilitychange',
310+
this.visibilityHandler
311+
);
299312
}
300313
await this.enqueueEvenAfterShutdown(op);
301314
}

0 commit comments

Comments
 (0)