@@ -19,13 +19,13 @@ import { Query } from '../core/query';
1919import { SnapshotVersion } from '../core/snapshot_version' ;
2020import {
2121 DocumentKeySet ,
22+ documentKeySet ,
2223 DocumentMap ,
2324 documentMap ,
2425 MaybeDocumentMap ,
2526 maybeDocumentMap ,
2627 NullableMaybeDocumentMap ,
2728 nullableMaybeDocumentMap ,
28- documentKeySet
2929} from '../model/collections' ;
3030import { Document , MaybeDocument , NoDocument } from '../model/document' ;
3131import { DocumentKey } from '../model/document_key' ;
@@ -35,10 +35,10 @@ import { ResourcePath } from '../model/path';
3535import { assert } from '../util/assert' ;
3636import { IndexManager } from './index_manager' ;
3737import { MutationQueue } from './mutation_queue' ;
38+ import { PatchMutation } from '../model/mutation' ;
3839import { PersistenceTransaction } from './persistence' ;
3940import { PersistencePromise } from './persistence_promise' ;
4041import { RemoteDocumentCache } from './remote_document_cache' ;
41- import { PatchMutation } from '../model/mutation' ;
4242
4343/**
4444 * A readonly view of the local state of all documents we're tracking (i.e. we
@@ -51,7 +51,7 @@ export class LocalDocumentsView {
5151 private remoteDocumentCache : RemoteDocumentCache ,
5252 private mutationQueue : MutationQueue ,
5353 private indexManager : IndexManager
54- ) { }
54+ ) { }
5555
5656 /**
5757 * Get the local view of the document identified by `key`.
@@ -228,57 +228,31 @@ export class LocalDocumentsView {
228228 // that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
229229 // otherwise those `PatchMutations` will be ignored because no base document can be found,
230230 // and lead to missing result for the query.
231- let missingBaseDocEntriesForPatching = documentKeySet ( ) ;
232- for ( const batch of matchingMutationBatches ) {
233- for ( const mutation of batch . mutations ) {
234- // Only process documents belonging to the collection.
235- if ( ! query . path . isImmediateParentOf ( mutation . key . path ) ) {
236- continue ;
237- }
238- if (
239- mutation instanceof PatchMutation &&
240- results . get ( mutation . key ) === null
241- ) {
242- missingBaseDocEntriesForPatching = missingBaseDocEntriesForPatching . add (
243- mutation . key
244- ) ;
245- }
246- }
247- }
248- return this . remoteDocumentCache . getEntries (
249- transaction ,
250- missingBaseDocEntriesForPatching
251- ) ;
252- } )
253- . next ( docsWithPendingPatches => {
254- docsWithPendingPatches . forEach ( ( key , doc ) => {
255- if ( doc !== null && doc instanceof Document ) {
256- results = results . insert ( key , doc ) ;
257- }
258- } ) ;
259- for ( const batch of mutationBatches ) {
260- for ( const mutation of batch . mutations ) {
261- const key = mutation . key ;
262- // Only process documents belonging to the collection.
263- if ( ! query . path . isImmediateParentOf ( key . path ) ) {
264- continue ;
265- }
266-
267- const baseDoc = results . get ( key ) ;
268- const mutatedDoc = mutation . applyToLocalView (
269- baseDoc ,
270- baseDoc ,
271- batch . localWriteTime
272- ) ;
273- if ( mutatedDoc instanceof Document ) {
274- results = results . insert ( key , mutatedDoc ) ;
275- } else {
276- results = results . remove ( key ) ;
231+ return this . getDocumentWithPendingPatches ( transaction , mutationBatches , results )
232+ . next ( docsWithPendingPatches => {
233+ docsWithPendingPatches . forEach ( ( key , doc ) => {
234+ if ( doc !== null && doc instanceof Document ) {
235+ results = results . insert ( key , doc ) ;
236+ }
237+ } ) ;
238+ for ( const batch of mutationBatches ) {
239+ for ( const mutation of batch . mutations ) {
240+ const key = mutation . key ;
241+ const baseDoc = results . get ( key ) ;
242+ const mutatedDoc = mutation . applyToLocalView (
243+ baseDoc ,
244+ baseDoc ,
245+ batch . localWriteTime
246+ ) ;
247+ if ( mutatedDoc instanceof Document ) {
248+ results = results . insert ( key , mutatedDoc ) ;
249+ } else {
250+ results = results . remove ( key ) ;
251+ }
252+ }
277253 }
278- }
279- }
280- } )
281- . next ( ( ) => {
254+ } ) ;
255+ } ) . next ( ( ) => {
282256 // Finally, filter out any documents that don't actually match
283257 // the query.
284258 results . forEach ( ( key , doc ) => {
@@ -290,4 +264,28 @@ export class LocalDocumentsView {
290264 return results ;
291265 } ) ;
292266 }
267+
268+ private getDocumentWithPendingPatches (
269+ transaction : PersistenceTransaction ,
270+ matchingMutationBatches : MutationBatch [ ] ,
271+ results : DocumentMap
272+ ) : PersistencePromise < NullableMaybeDocumentMap > {
273+ let missingBaseDocEntriesForPatching = documentKeySet ( ) ;
274+ for ( const batch of matchingMutationBatches ) {
275+ for ( const mutation of batch . mutations ) {
276+ if (
277+ mutation instanceof PatchMutation &&
278+ results . get ( mutation . key ) === null
279+ ) {
280+ missingBaseDocEntriesForPatching = missingBaseDocEntriesForPatching . add (
281+ mutation . key
282+ ) ;
283+ }
284+ }
285+ }
286+ return this . remoteDocumentCache . getEntries (
287+ transaction ,
288+ missingBaseDocEntriesForPatching
289+ ) ;
290+ }
293291}
0 commit comments