@@ -19,13 +19,13 @@ import { Query } from '../core/query';
19
19
import { SnapshotVersion } from '../core/snapshot_version' ;
20
20
import {
21
21
DocumentKeySet ,
22
+ documentKeySet ,
22
23
DocumentMap ,
23
24
documentMap ,
24
25
MaybeDocumentMap ,
25
26
maybeDocumentMap ,
26
27
NullableMaybeDocumentMap ,
27
28
nullableMaybeDocumentMap ,
28
- documentKeySet
29
29
} from '../model/collections' ;
30
30
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
31
31
import { DocumentKey } from '../model/document_key' ;
@@ -35,10 +35,10 @@ import { ResourcePath } from '../model/path';
35
35
import { assert } from '../util/assert' ;
36
36
import { IndexManager } from './index_manager' ;
37
37
import { MutationQueue } from './mutation_queue' ;
38
+ import { PatchMutation } from '../model/mutation' ;
38
39
import { PersistenceTransaction } from './persistence' ;
39
40
import { PersistencePromise } from './persistence_promise' ;
40
41
import { RemoteDocumentCache } from './remote_document_cache' ;
41
- import { PatchMutation } from '../model/mutation' ;
42
42
43
43
/**
44
44
* A readonly view of the local state of all documents we're tracking (i.e. we
@@ -51,7 +51,7 @@ export class LocalDocumentsView {
51
51
private remoteDocumentCache : RemoteDocumentCache ,
52
52
private mutationQueue : MutationQueue ,
53
53
private indexManager : IndexManager
54
- ) { }
54
+ ) { }
55
55
56
56
/**
57
57
* Get the local view of the document identified by `key`.
@@ -228,57 +228,31 @@ export class LocalDocumentsView {
228
228
// that are not in `result` yet, and back fill them via `remoteDocumentCache.getEntries`,
229
229
// otherwise those `PatchMutations` will be ignored because no base document can be found,
230
230
// 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
+ }
277
253
}
278
- }
279
- }
280
- } )
281
- . next ( ( ) => {
254
+ } ) ;
255
+ } ) . next ( ( ) => {
282
256
// Finally, filter out any documents that don't actually match
283
257
// the query.
284
258
results . forEach ( ( key , doc ) => {
@@ -290,4 +264,28 @@ export class LocalDocumentsView {
290
264
return results ;
291
265
} ) ;
292
266
}
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
+ }
293
291
}
0 commit comments