@@ -56,7 +56,6 @@ import { SortedMap } from '../util/sorted_map';
5656
5757import { DocumentOverlayCache } from './document_overlay_cache' ;
5858import { IndexManager } from './index_manager' ;
59- import { LocalWriteResult } from './local_store_impl' ;
6059import { MutationQueue } from './mutation_queue' ;
6160import { OverlayedDocument } from './overlayed_document' ;
6261import { PersistencePromise } from './persistence_promise' ;
@@ -73,22 +72,30 @@ import {
7372 isPipeline ,
7473 QueryOrPipeline
7574} from '../core/pipeline-util' ;
76- import { Pipeline } from '../lite-api/pipeline' ;
7775import { FirestoreError } from '../util/error' ;
7876import {
7977 CorePipeline ,
8078 pipelineEvaluate ,
8179 runPipeline
8280} from '../core/pipeline_run' ;
83- import { SortedSet } from '../util/sorted_set' ;
8481import {
8582 PipelineCachedResults ,
8683 PipelineResultsCache
8784} from './pipeline_results_cache' ;
88- import { TargetId } from '../core/types' ;
85+ import { BatchId , TargetId } from '../core/types' ;
8986import { TargetData } from './target_data' ;
9087import { targetIsPipelineTarget } from '../core/target' ;
9188
89+ export interface NextDocuments {
90+ batchId : BatchId ;
91+ changes : DocumentMap ;
92+ }
93+
94+ export interface MergedPipelineResults {
95+ results : DocumentMap ;
96+ mutatedKeys : DocumentKeySet ;
97+ }
98+
9299/**
93100 * A readonly view of the local state of all documents we're tracking (i.e. we
94101 * have a cached version in remoteDocumentCache or local mutations for the
@@ -433,14 +440,14 @@ export class LocalDocumentsView {
433440 * @param collectionGroup The collection group for the documents.
434441 * @param offset The offset to index into.
435442 * @param count The number of documents to return
436- * @return A LocalWriteResult with the documents that follow the provided offset and the last processed batch id.
443+ * @return A NextDocuments with the documents that follow the provided offset and the last processed batch id.
437444 */
438445 getNextDocuments (
439446 transaction : PersistenceTransaction ,
440447 collectionGroup : string ,
441448 offset : IndexOffset ,
442449 count : number
443- ) : PersistencePromise < LocalWriteResult > {
450+ ) : PersistencePromise < NextDocuments > {
444451 return this . remoteDocumentCache
445452 . getAllFromCollectionGroup ( transaction , collectionGroup , offset , count )
446453 . next ( ( originalDocs : MutableDocumentMap ) => {
@@ -788,9 +795,12 @@ export class LocalDocumentsView {
788795
789796 calculateMergedAugmentPipelineResults (
790797 targetMap : SortedMap < TargetId , TargetData > ,
791- currentAugmentPipelineResults : Map < TargetId , PipelineCachedResults > ,
792- changedDocs : SortedMap < DocumentKey , Document >
793- ) : Map < TargetId , MutableDocumentMap > {
798+ currentAugmentPipelineResults : Map <
799+ TargetId ,
800+ PipelineCachedResults | undefined
801+ > ,
802+ changedDocs : DocumentMap
803+ ) : Map < TargetId , MergedPipelineResults > {
794804 // We only care about documents with pending writes because changedDocs are results
795805 // of two scenarios:
796806 // 1. Global snapshot, which means currentAugmentPipelineResults should include all
@@ -803,7 +813,7 @@ export class LocalDocumentsView {
803813 }
804814 } ) ;
805815
806- const mergedResults = new Map < TargetId , MutableDocumentMap > ( ) ;
816+ const mergedResults = new Map < TargetId , MergedPipelineResults > ( ) ;
807817 currentAugmentPipelineResults . forEach ( ( results , targetId ) => {
808818 const pipeline = targetMap . get ( targetId ) ?. target ;
809819 debugAssert ( ! ! pipeline , `Target Id ${ targetId } not found` ) ;
@@ -812,13 +822,21 @@ export class LocalDocumentsView {
812822 `Target Id ${ targetId } is not a pipeline target`
813823 ) ;
814824
815- const merged = results . results ;
825+ let merged = results ?. results ?? mutableDocumentMap ( ) ;
826+ let mutatedKeys = documentKeySet ( ) ;
816827 // TODO(pipeline): We need to handle limit pipelines here!!
817828 const pipelineResult = runPipeline ( pipeline , docsWithMutations ) ;
818829 for ( const result of pipelineResult ) {
819- merged . insert ( result . key , result ) ;
830+ mutatedKeys = mutatedKeys . add ( result . key ) ;
831+ merged = merged . insert ( result . key , result ) ;
832+ }
833+ for ( const doc of docsWithMutations ) {
834+ if ( ! mutatedKeys . has ( doc . key ) && ! ! merged . get ( doc . key ) ) {
835+ merged = merged . remove ( doc . key ) ;
836+ mutatedKeys = mutatedKeys . add ( doc . key ) ;
837+ }
820838 }
821- mergedResults . set ( targetId , merged ) ;
839+ mergedResults . set ( targetId , { results : merged , mutatedKeys } ) ;
822840 } ) ;
823841
824842 return mergedResults ;
0 commit comments