Skip to content

Commit

Permalink
Fix bug in AsyncQueue visibility handler (#3586)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Aug 10, 2020
1 parent 2c37560 commit 68995c2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-elephants-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixed a bug that caused slow retries for IndexedDB operations even when a webpage re-entered the foreground.
4 changes: 2 additions & 2 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ export class IndexedDbPersistence implements Persistence {
debugAssert(!this.started, 'IndexedDbPersistence double-started!');
debugAssert(this.window !== null, "Expected 'window' to be defined");

// NOTE: This is expected to fail sometimes (in the case of another tab
// already having the persistence lock), so it's the first thing we should
// NOTE: This is expected to fail sometimes (in the case of another tab
// already having the persistence lock), so it's the first thing we should
// do.
return this.updateClientMetadataAndTryBecomePrimary()
.then(() => {
Expand Down
29 changes: 21 additions & 8 deletions packages/firestore/src/util/async_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { logDebug, logError } from './log';
import { Deferred } from './promise';
import { ExponentialBackoff } from '../remote/backoff';
import { isIndexedDbTransactionError } from '../local/simple_db';
import { getWindow } from '../platform/dom';
import { getDocument } from '../platform/dom';

const LOG_TAG = 'AsyncQueue';

Expand Down Expand Up @@ -235,12 +235,22 @@ export class AsyncQueue {
// Visibility handler that triggers an immediate retry of all retryable
// operations. Meant to speed up recovery when we regain file system access
// after page comes into foreground.
private visibilityHandler = (): void => this.backoff.skipBackoff();
private visibilityHandler: () => void = () => {
const document = getDocument();
if (document) {
logDebug(
LOG_TAG,
'Visibility state changed to ',
document.visibilityState
);
}
this.backoff.skipBackoff();
};

constructor() {
const window = getWindow();
if (window && typeof window.addEventListener === 'function') {
window.addEventListener('visibilitychange', this.visibilityHandler);
const document = getDocument();
if (document && typeof document.addEventListener === 'function') {
document.addEventListener('visibilitychange', this.visibilityHandler);
}
}

Expand Down Expand Up @@ -293,9 +303,12 @@ export class AsyncQueue {
this.verifyNotFailed();
if (!this._isShuttingDown) {
this._isShuttingDown = true;
const window = getWindow();
if (window) {
window.removeEventListener('visibilitychange', this.visibilityHandler);
const document = getDocument();
if (document && typeof document.removeEventListener === 'function') {
document.removeEventListener(
'visibilitychange',
this.visibilityHandler
);
}
await this.enqueueEvenAfterShutdown(op);
}
Expand Down

0 comments on commit 68995c2

Please sign in to comment.