@@ -33,7 +33,8 @@ import {
33
33
newOverlayMap ,
34
34
documentMap ,
35
35
mutableDocumentMap ,
36
- documentKeySet
36
+ documentKeySet ,
37
+ DocumentKeyMap
37
38
} from '../model/collections' ;
38
39
import { Document , MutableDocument } from '../model/document' ;
39
40
import { DocumentKey } from '../model/document_key' ;
@@ -52,6 +53,7 @@ import { SortedMap } from '../util/sorted_map';
52
53
import { DocumentOverlayCache } from './document_overlay_cache' ;
53
54
import { IndexManager } from './index_manager' ;
54
55
import { MutationQueue } from './mutation_queue' ;
56
+ import { OverlayedDocument } from './overlayed_document' ;
55
57
import { PersistencePromise } from './persistence_promise' ;
56
58
import { PersistenceTransaction } from './persistence_transaction' ;
57
59
import { RemoteDocumentCache } from './remote_document_cache' ;
@@ -92,7 +94,7 @@ export class LocalDocumentsView {
92
94
mutationApplyToLocalView (
93
95
overlay . mutation ,
94
96
document ,
95
- null ,
97
+ FieldMask . empty ( ) ,
96
98
Timestamp . now ( )
97
99
) ;
98
100
}
@@ -141,10 +143,34 @@ export class LocalDocumentsView {
141
143
docs ,
142
144
overlays ,
143
145
existenceStateChanged
144
- ) ;
146
+ ) . next ( computeViewsResult => {
147
+ let result = documentMap ( ) ;
148
+ computeViewsResult . forEach ( ( documentKey , overlayedDocument ) => {
149
+ result = result . insert (
150
+ documentKey ,
151
+ overlayedDocument . overlayedDocument
152
+ ) ;
153
+ } ) ;
154
+ return result ;
155
+ } ) ;
145
156
} ) ;
146
157
}
147
158
159
+ /**
160
+ * Gets the overlayed documents for the given document map, which will include
161
+ * the local view of those documents and a `FieldMask` indicating which fields
162
+ * are mutated locally, `null` if overlay is a Set or Delete mutation.
163
+ */
164
+ getOverlayedDocuments (
165
+ transaction : PersistenceTransaction ,
166
+ docs : MutableDocumentMap
167
+ ) : PersistencePromise < DocumentKeyMap < OverlayedDocument > > {
168
+ const overlays = newOverlayMap ( ) ;
169
+ return this . populateOverlays ( transaction , overlays , docs ) . next ( ( ) =>
170
+ this . computeViews ( transaction , docs , overlays , documentKeySet ( ) )
171
+ ) ;
172
+ }
173
+
148
174
/**
149
175
* Fetches the overlays for {@code docs} and adds them to provided overlay map
150
176
* if the map does not already contain an entry for the given document key.
@@ -170,17 +196,26 @@ export class LocalDocumentsView {
170
196
}
171
197
172
198
/**
173
- * Computes the local view for documents, applying overlays from both
174
- * `memoizedOverlays` and the overlay cache.
199
+ * Computes the local view for the given documents.
200
+ *
201
+ * @param docs - The documents to compute views for. It also has the base
202
+ * version of the documents.
203
+ * @param overlays - The overlays that need to be applied to the given base
204
+ * version of the documents.
205
+ * @param existenceStateChanged - A set of documents whose existence states
206
+ * might have changed. This is used to determine if we need to re-calculate
207
+ * overlays from mutation queues.
208
+ * @return A map represents the local documents view.
175
209
*/
176
210
computeViews (
177
211
transaction : PersistenceTransaction ,
178
212
docs : MutableDocumentMap ,
179
213
overlays : OverlayMap ,
180
214
existenceStateChanged : DocumentKeySet
181
- ) : PersistencePromise < DocumentMap > {
182
- let results = documentMap ( ) ;
215
+ ) : PersistencePromise < DocumentKeyMap < OverlayedDocument > > {
183
216
let recalculateDocuments = mutableDocumentMap ( ) ;
217
+ const mutatedFields = newDocumentKeyMap < FieldMask | null > ( ) ;
218
+ const results = newDocumentKeyMap < OverlayedDocument > ( ) ;
184
219
docs . forEach ( ( _ , doc ) => {
185
220
const overlay = overlays . get ( doc . key ) ;
186
221
// Recalculate an overlay if the document's existence state changed due to
@@ -196,25 +231,40 @@ export class LocalDocumentsView {
196
231
) {
197
232
recalculateDocuments = recalculateDocuments . insert ( doc . key , doc ) ;
198
233
} else if ( overlay !== undefined ) {
199
- mutationApplyToLocalView ( overlay . mutation , doc , null , Timestamp . now ( ) ) ;
234
+ mutatedFields . set ( doc . key , overlay . mutation . getFieldMask ( ) ) ;
235
+ mutationApplyToLocalView (
236
+ overlay . mutation ,
237
+ doc ,
238
+ overlay . mutation . getFieldMask ( ) ,
239
+ Timestamp . now ( )
240
+ ) ;
200
241
}
201
242
} ) ;
202
243
203
244
return this . recalculateAndSaveOverlays (
204
245
transaction ,
205
246
recalculateDocuments
206
- ) . next ( ( ) => {
207
- docs . forEach ( ( key , value ) => {
208
- results = results . insert ( key , value ) ;
209
- } ) ;
247
+ ) . next ( recalculatedFields => {
248
+ recalculatedFields . forEach ( ( documentKey , mask ) =>
249
+ mutatedFields . set ( documentKey , mask )
250
+ ) ;
251
+ docs . forEach ( ( documentKey , document ) =>
252
+ results . set (
253
+ documentKey ,
254
+ new OverlayedDocument (
255
+ document ,
256
+ mutatedFields . get ( documentKey ) ?? null
257
+ )
258
+ )
259
+ ) ;
210
260
return results ;
211
261
} ) ;
212
262
}
213
263
214
264
private recalculateAndSaveOverlays (
215
265
transaction : PersistenceTransaction ,
216
266
docs : MutableDocumentMap
217
- ) : PersistencePromise < void > {
267
+ ) : PersistencePromise < DocumentKeyMap < FieldMask | null > > {
218
268
const masks = newDocumentKeyMap < FieldMask | null > ( ) ;
219
269
// A reverse lookup map from batch id to the documents within that batch.
220
270
let documentsByBatchId = new SortedMap < number , DocumentKeySet > (
@@ -274,7 +324,8 @@ export class LocalDocumentsView {
274
324
) ;
275
325
}
276
326
return PersistencePromise . waitFor ( promises ) ;
277
- } ) ;
327
+ } )
328
+ . next ( ( ) => masks ) ;
278
329
}
279
330
280
331
/**
@@ -284,7 +335,7 @@ export class LocalDocumentsView {
284
335
recalculateAndSaveOverlaysForDocumentKeys (
285
336
transaction : PersistenceTransaction ,
286
337
documentKeys : DocumentKeySet
287
- ) : PersistencePromise < void > {
338
+ ) : PersistencePromise < DocumentKeyMap < FieldMask | null > > {
288
339
return this . remoteDocumentCache
289
340
. getEntries ( transaction , documentKeys )
290
341
. next ( docs => this . recalculateAndSaveOverlays ( transaction , docs ) ) ;
@@ -407,7 +458,7 @@ export class LocalDocumentsView {
407
458
mutationApplyToLocalView (
408
459
overlay . mutation ,
409
460
document ,
410
- null ,
461
+ FieldMask . empty ( ) ,
411
462
Timestamp . now ( )
412
463
) ;
413
464
}
0 commit comments