1717
1818import { debugAssert , debugCast } from '../util/assert' ;
1919import { wrapInUserErrorIfRecoverable } from '../util/async_queue' ;
20- import { FirestoreError } from '../util/error' ;
20+ import { Code , FirestoreError } from '../util/error' ;
2121import { EventHandler } from '../util/misc' ;
2222import { ObjectMap } from '../util/obj_map' ;
2323
@@ -64,19 +64,17 @@ export interface EventManager {
6464 onUnlisten ?: ( query : Query , disableRemoteListen : boolean ) => Promise < void > ;
6565 onFirstRemoteStoreListen ?: ( query : Query ) => Promise < void > ;
6666 onLastRemoteStoreUnlisten ?: ( query : Query ) => Promise < void > ;
67+ terminate ( ) : void ;
6768}
6869
6970export function newEventManager ( ) : EventManager {
7071 return new EventManagerImpl ( ) ;
7172}
7273
7374export class EventManagerImpl implements EventManager {
74- queries = new ObjectMap < Query , QueryListenersInfo > (
75- q => canonifyQuery ( q ) ,
76- queryEquals
77- ) ;
75+ queries : ObjectMap < Query , QueryListenersInfo > = newQueriesObjectMap ( ) ;
7876
79- onlineState = OnlineState . Unknown ;
77+ onlineState : OnlineState = OnlineState . Unknown ;
8078
8179 snapshotsInSyncListeners : Set < Observer < void > > = new Set ( ) ;
8280
@@ -98,6 +96,20 @@ export class EventManagerImpl implements EventManager {
9896 * still listening to the cache.
9997 */
10098 onLastRemoteStoreUnlisten ?: ( query : Query ) => Promise < void > ;
99+
100+ terminate ( ) : void {
101+ errorAllTargets (
102+ this ,
103+ new FirestoreError ( Code . ABORTED , 'Firestore shutting down' )
104+ ) ;
105+ }
106+ }
107+
108+ function newQueriesObjectMap ( ) : ObjectMap < Query , QueryListenersInfo > {
109+ return new ObjectMap < Query , QueryListenersInfo > (
110+ q => canonifyQuery ( q ) ,
111+ queryEquals
112+ ) ;
101113}
102114
103115function validateEventManager ( eventManagerImpl : EventManagerImpl ) : void {
@@ -334,6 +346,23 @@ export function removeSnapshotsInSyncListener(
334346 eventManagerImpl . snapshotsInSyncListeners . delete ( observer ) ;
335347}
336348
349+ function errorAllTargets (
350+ eventManager : EventManager ,
351+ error : FirestoreError
352+ ) : void {
353+ const eventManagerImpl = debugCast ( eventManager , EventManagerImpl ) ;
354+ const queries = eventManagerImpl . queries ;
355+
356+ // Prevent further access by clearing ObjectMap.
357+ eventManagerImpl . queries = newQueriesObjectMap ( ) ;
358+
359+ queries . forEach ( ( _ , queryInfo ) => {
360+ for ( const listener of queryInfo . listeners ) {
361+ listener . onError ( error ) ;
362+ }
363+ } ) ;
364+ }
365+
337366// Call all global snapshot listeners that have been set.
338367function raiseSnapshotsInSyncEvent ( eventManagerImpl : EventManagerImpl ) : void {
339368 eventManagerImpl . snapshotsInSyncListeners . forEach ( observer => {
0 commit comments