@@ -24,6 +24,9 @@ import { ObjectMap } from '../util/obj_map';
2424import { canonifyQuery , Query , queryEquals , stringifyQuery } from './query' ;
2525import { OnlineState } from './types' ;
2626import { ChangeType , DocumentViewChange , ViewSnapshot } from './view_snapshot' ;
27+ import { Pipeline } from '../api/pipeline' ;
28+ import { PipelineSnapshot } from '../api/snapshot' ;
29+ import { PipelineResultView } from './sync_engine_impl' ;
2730
2831/**
2932 * Holds the listeners and the last received ViewSnapshot for a query being
@@ -64,6 +67,8 @@ export interface EventManager {
6467 onUnlisten ?: ( query : Query , disableRemoteListen : boolean ) => Promise < void > ;
6568 onFirstRemoteStoreListen ?: ( query : Query ) => Promise < void > ;
6669 onLastRemoteStoreUnlisten ?: ( query : Query ) => Promise < void > ;
70+ // TODO(pipeline): consolidate query and pipeline
71+ onListenPipeline ?: ( pipeline : PipelineListener ) => Promise < void > ;
6772 terminate ( ) : void ;
6873}
6974
@@ -85,6 +90,7 @@ export class EventManagerImpl implements EventManager {
8590 ) => Promise < ViewSnapshot > ;
8691 /** Callback invoked once all listeners to a Query are removed. */
8792 onUnlisten ?: ( query : Query , disableRemoteListen : boolean ) => Promise < void > ;
93+ onListenPipeline ?: ( pipeline : PipelineListener ) => Promise < void > ;
8894
8995 /**
9096 * Callback invoked when a Query starts listening to the remote store, while
@@ -123,6 +129,7 @@ function validateEventManager(eventManagerImpl: EventManagerImpl): void {
123129 ! ! eventManagerImpl . onLastRemoteStoreUnlisten ,
124130 'onLastRemoteStoreUnlisten not set'
125131 ) ;
132+ debugAssert ( ! ! eventManagerImpl . onListenPipeline , 'onListenPipeline not set' ) ;
126133}
127134
128135const enum ListenerSetupAction {
@@ -213,6 +220,25 @@ export async function eventManagerListen(
213220 }
214221}
215222
223+ export async function eventManagerListenPipeline (
224+ eventManager : EventManager ,
225+ listener : PipelineListener
226+ ) : Promise < void > {
227+ const eventManagerImpl = debugCast ( eventManager , EventManagerImpl ) ;
228+ validateEventManager ( eventManagerImpl ) ;
229+
230+ try {
231+ await eventManagerImpl . onListenPipeline ! ( listener ) ;
232+ } catch ( e ) {
233+ const firestoreError = wrapInUserErrorIfRecoverable (
234+ e as Error ,
235+ `Initialization of query '${ listener . pipeline } ' failed`
236+ ) ;
237+ listener . onError ( firestoreError ) ;
238+ return ;
239+ }
240+ }
241+
216242export async function eventManagerUnlisten (
217243 eventManager : EventManager ,
218244 listener : QueryListener
@@ -286,6 +312,13 @@ export function eventManagerOnWatchChange(
286312 }
287313}
288314
315+ export function eventManagerOnPipelineWatchChange (
316+ eventManager : EventManager ,
317+ viewSnaps : PipelineResultView [ ]
318+ ) : void {
319+ const eventManagerImpl = debugCast ( eventManager , EventManagerImpl ) ;
320+ }
321+
289322export function eventManagerOnWatchError (
290323 eventManager : EventManager ,
291324 query : Query ,
@@ -567,3 +600,21 @@ export class QueryListener {
567600 return this . options . source !== ListenerDataSource . Cache ;
568601 }
569602}
603+
604+ export class PipelineListener {
605+ private snap : PipelineResultView | null = null ;
606+
607+ constructor (
608+ readonly pipeline : Pipeline ,
609+ private queryObserver : Observer < PipelineSnapshot >
610+ ) { }
611+
612+ onViewSnapshot ( snap : PipelineResultView ) : boolean {
613+ this . snap = snap ;
614+ return true ;
615+ }
616+
617+ onError ( error : FirestoreError ) : void {
618+ this . queryObserver . error ( error ) ;
619+ }
620+ }
0 commit comments