Skip to content

Commit 044f9ae

Browse files
committed
More places using 'unload'
1 parent 951f9ca commit 044f9ae

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 3 additions & 11 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 } from '../util/types';
26+
import { DocumentLike, WindowLike, terminationEvent } from '../util/types';
2727

2828
import { BundleCache } from './bundle_cache';
2929
import { IndexManager } from './index_manager';
@@ -942,14 +942,6 @@ export class IndexedDbPersistence implements Persistence {
942942
}
943943
}
944944

945-
/**
946-
* Returns the page termination event to listen to. 'pagehide' is recommended
947-
* if available, it falls back to the less reliable 'unload'.
948-
*/
949-
private terminationEvent(): string {
950-
return 'onpagehide' in this.window! ? 'pagehide' : 'unload';
951-
}
952-
953945
/**
954946
* Attaches a window.unload handler that will synchronously write our
955947
* clientId to a "zombie client id" location in LocalStorage. This can be used
@@ -975,7 +967,7 @@ export class IndexedDbPersistence implements Persistence {
975967
});
976968
};
977969
this.window.addEventListener(
978-
this.terminationEvent(),
970+
terminationEvent(this.window),
979971
this.windowUnloadHandler
980972
);
981973
}
@@ -988,7 +980,7 @@ export class IndexedDbPersistence implements Persistence {
988980
"Expected 'window.removeEventListener' to be a function"
989981
);
990982
this.window!.removeEventListener(
991-
this.terminationEvent(),
983+
terminationEvent(this.window),
992984
this.windowUnloadHandler
993985
);
994986
this.windowUnloadHandler = null;

packages/firestore/src/local/shared_client_state.ts

Lines changed: 4 additions & 2 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, WindowLike } from '../util/types';
35+
import { isSafeInteger, terminationEvent, WindowLike } from '../util/types';
3636

3737
import {
3838
CLIENT_STATE_KEY_PREFIX,
@@ -613,7 +613,9 @@ 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('unload', () => this.shutdown());
616+
this.window.addEventListener(terminationEvent(this.window), () =>
617+
this.shutdown()
618+
);
617619

618620
this.started = true;
619621
}

packages/firestore/src/util/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ 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+
6270
/** The subset of the browser's Document interface used by the SDK. */
6371
export interface DocumentLike {
6472
readonly visibilityState: VisibilityState;

0 commit comments

Comments
 (0)