Skip to content

Commit 8c4e191

Browse files
committed
'pagehide' all the way
1 parent 044f9ae commit 8c4e191

File tree

3 files changed

+5
-21
lines changed

3 files changed

+5
-21
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { debugAssert } from '../util/assert';
2323
import { AsyncQueue, DelayedOperation, TimerId } from '../util/async_queue';
2424
import { Code, FirestoreError } from '../util/error';
2525
import { logDebug, logError } from '../util/log';
26-
import { DocumentLike, WindowLike, terminationEvent } from '../util/types';
26+
import { DocumentLike, WindowLike } from '../util/types';
2727

2828
import { BundleCache } from './bundle_cache';
2929
import { IndexManager } from './index_manager';
@@ -966,10 +966,7 @@ export class IndexedDbPersistence implements Persistence {
966966
return this.shutdown();
967967
});
968968
};
969-
this.window.addEventListener(
970-
terminationEvent(this.window),
971-
this.windowUnloadHandler
972-
);
969+
this.window.addEventListener('pagehide', this.windowUnloadHandler);
973970
}
974971
}
975972

@@ -979,10 +976,7 @@ export class IndexedDbPersistence implements Persistence {
979976
typeof this.window?.removeEventListener === 'function',
980977
"Expected 'window.removeEventListener' to be a function"
981978
);
982-
this.window!.removeEventListener(
983-
terminationEvent(this.window),
984-
this.windowUnloadHandler
985-
);
979+
this.window!.removeEventListener('pagehide', this.windowUnloadHandler);
986980
this.windowUnloadHandler = null;
987981
}
988982
}

packages/firestore/src/local/shared_client_state.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { logError, logDebug } from '../util/log';
3232
import { primitiveComparator } from '../util/misc';
3333
import { SortedMap } from '../util/sorted_map';
3434
import { SortedSet } from '../util/sorted_set';
35-
import { isSafeInteger, terminationEvent, WindowLike } from '../util/types';
35+
import { isSafeInteger, WindowLike } from '../util/types';
3636

3737
import {
3838
CLIENT_STATE_KEY_PREFIX,
@@ -613,9 +613,7 @@ export class WebStorageSharedClientState implements SharedClientState {
613613

614614
// Register a window unload hook to remove the client metadata entry from
615615
// WebStorage even if `shutdown()` was not called.
616-
this.window.addEventListener(terminationEvent(this.window), () =>
617-
this.shutdown()
618-
);
616+
this.window.addEventListener('pagehide', () => this.shutdown());
619617

620618
this.started = true;
621619
}

packages/firestore/src/util/types.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ export interface WindowLike {
5959
removeEventListener(type: string, listener: EventListener): void;
6060
}
6161

62-
/**
63-
* Returns the page termination event to listen to. 'pagehide' is recommended
64-
* if available, it falls back to the less reliable 'unload'.
65-
*/
66-
export function terminationEvent(window: WindowLike | null): string {
67-
return 'onpagehide' in window! ? 'pagehide' : 'unload';
68-
}
69-
7062
/** The subset of the browser's Document interface used by the SDK. */
7163
export interface DocumentLike {
7264
readonly visibilityState: VisibilityState;

0 commit comments

Comments
 (0)