Skip to content

Commit fcf8bdb

Browse files
committed
Port overlay recalculation bug (Android SDK PR #3495).
1 parent 1e68233 commit fcf8bdb

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

packages/firestore/src/local/local_documents_view.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,25 @@ export class LocalDocumentsView {
226226
.next(batches => {
227227
for (const batch of batches) {
228228
batch.keys().forEach(key => {
229-
let mask: FieldMask | null = masks.has(key)
230-
? masks.get(key)!
231-
: FieldMask.empty();
232-
mask = batch.applyToLocalView(docs.get(key)!, mask);
233-
masks.set(key, mask);
234-
if (documentsByBatchId.get(batch.batchId) === null) {
229+
const baseDoc = docs.get(key);
230+
if (baseDoc !== null) {
231+
let mask: FieldMask | null = masks.has(key)
232+
? masks.get(key)!
233+
: FieldMask.empty();
234+
mask = batch.applyToLocalView(baseDoc, mask);
235+
masks.set(key, mask);
236+
if (documentsByBatchId.get(batch.batchId) === null) {
237+
documentsByBatchId = documentsByBatchId.insert(
238+
batch.batchId,
239+
documentKeySet()
240+
);
241+
}
242+
const newSet = documentsByBatchId.get(batch.batchId)!.add(key);
235243
documentsByBatchId = documentsByBatchId.insert(
236244
batch.batchId,
237-
documentKeySet()
245+
newSet
238246
);
239247
}
240-
const newSet = documentsByBatchId.get(batch.batchId)!.add(key);
241-
documentsByBatchId = documentsByBatchId.insert(
242-
batch.batchId,
243-
newSet
244-
);
245248
});
246249
}
247250
})

packages/firestore/test/unit/local/local_store.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,25 @@ function genericLocalStoreTests(
18011801
);
18021802
});
18031803

1804+
it('can handle batch Ack when pending batches have other docs', () => {
1805+
// Prepare two batches, the first one will get rejected by the backend.
1806+
// When the first batch is rejected, overlay is recalculated with only the
1807+
// second batch, even though it has more documents than what is being
1808+
// rejected.
1809+
return expectLocalStore()
1810+
.afterMutations([patchMutation('foo/bar', { 'foo': 'bar' })])
1811+
.afterMutations([
1812+
setMutation('foo/bar', { 'foo': 'bar-set' }),
1813+
setMutation('foo/another', { 'foo': 'another' })
1814+
])
1815+
.afterRejectingMutation()
1816+
.toContain(doc('foo/bar', 0, { 'foo': 'bar-set' }).setHasLocalMutations())
1817+
.toContain(
1818+
doc('foo/another', 0, { 'foo': 'another' }).setHasLocalMutations()
1819+
)
1820+
.finish();
1821+
});
1822+
18041823
it('uses target mapping to execute queries', () => {
18051824
if (gcIsEager) {
18061825
return;

0 commit comments

Comments
 (0)