Skip to content

Commit e24b1f4

Browse files
Change the order of checks in applyRemoteEvent() (#2176)
1 parent 372c0c5 commit e24b1f4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

packages/firestore/src/local/local_store.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,23 +516,36 @@ export class LocalStore {
516516
documentBuffer.getEntries(txn, updatedKeys).next(existingDocs => {
517517
remoteEvent.documentUpdates.forEach((key, doc) => {
518518
const existingDoc = existingDocs.get(key);
519+
520+
// Note: The order of the steps below is important, since we want
521+
// to ensure that rejected limbo resolutions (which fabricate
522+
// NoDocuments with SnapshotVersion.MIN) never add documents to
523+
// cache.
519524
if (
525+
doc instanceof NoDocument &&
526+
doc.version.isEqual(SnapshotVersion.MIN)
527+
) {
528+
// NoDocuments with SnapshotVersion.MIN are used in manufactured
529+
// events. We remove these documents from cache since we lost
530+
// access.
531+
documentBuffer.removeEntry(key);
532+
changedDocs = changedDocs.insert(key, doc);
533+
} else if (
520534
existingDoc == null ||
521535
doc.version.compareTo(existingDoc.version) > 0 ||
522536
(doc.version.compareTo(existingDoc.version) === 0 &&
523537
existingDoc.hasPendingWrites)
524538
) {
539+
// TODO(index-free): Make this an assert when we enable
540+
// Index-Free queries
541+
if (SnapshotVersion.MIN.isEqual(remoteEvent.snapshotVersion)) {
542+
log.error(
543+
LOG_TAG,
544+
'Cannot add a document when the remote version is zero'
545+
);
546+
}
525547
documentBuffer.addEntry(doc);
526548
changedDocs = changedDocs.insert(key, doc);
527-
} else if (
528-
doc instanceof NoDocument &&
529-
doc.version.isEqual(SnapshotVersion.MIN)
530-
) {
531-
// NoDocuments with SnapshotVersion.MIN are used in manufactured events (e.g. in the
532-
// case of a limbo document resolution failing). We remove these documents from cache
533-
// since we lost access.
534-
documentBuffer.removeEntry(key);
535-
changedDocs = changedDocs.insert(key, doc);
536549
} else {
537550
log.debug(
538551
LOG_TAG,

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,13 @@ class LocalStoreTester {
239239
toContain(doc: MaybeDocument): LocalStoreTester {
240240
this.promiseChain = this.promiseChain.then(() => {
241241
return this.localStore.readDocument(doc.key).then(result => {
242-
expectEqual(result, doc);
242+
expectEqual(
243+
result,
244+
doc,
245+
`Expected ${
246+
result ? result.toString() : null
247+
} to match ${doc.toString()}.`
248+
);
243249
});
244250
});
245251
return this;
@@ -759,15 +765,15 @@ function genericLocalStoreTests(
759765
expectLocalStore()
760766
.afterAllocatingQuery(query)
761767
.toReturnTargetId(2)
762-
.after(docAddedRemoteEvent(doc('foo/bar', 0, { foo: 'old' }), [2]))
768+
.after(docAddedRemoteEvent(doc('foo/bar', 1, { foo: 'old' }), [2]))
763769
.after(patchMutation('foo/bar', { foo: 'bar' }))
764770
// Release the query so that our target count goes back to 0 and we are considered
765771
// up-to-date.
766772
.afterReleasingQuery(query)
767773
.after(setMutation('foo/bah', { foo: 'bah' }))
768774
.after(deleteMutation('foo/baz'))
769775
.toContain(
770-
doc('foo/bar', 0, { foo: 'bar' }, { hasLocalMutations: true })
776+
doc('foo/bar', 1, { foo: 'bar' }, { hasLocalMutations: true })
771777
)
772778
.toContain(
773779
doc('foo/bah', 0, { foo: 'bah' }, { hasLocalMutations: true })
@@ -800,15 +806,15 @@ function genericLocalStoreTests(
800806
expectLocalStore()
801807
.afterAllocatingQuery(query)
802808
.toReturnTargetId(2)
803-
.after(docAddedRemoteEvent(doc('foo/bar', 0, { foo: 'old' }), [2]))
809+
.after(docAddedRemoteEvent(doc('foo/bar', 1, { foo: 'old' }), [2]))
804810
.after(patchMutation('foo/bar', { foo: 'bar' }))
805811
// Release the query so that our target count goes back to 0 and we are considered
806812
// up-to-date.
807813
.afterReleasingQuery(query)
808814
.after(setMutation('foo/bah', { foo: 'bah' }))
809815
.after(deleteMutation('foo/baz'))
810816
.toContain(
811-
doc('foo/bar', 0, { foo: 'bar' }, { hasLocalMutations: true })
817+
doc('foo/bar', 1, { foo: 'bar' }, { hasLocalMutations: true })
812818
)
813819
.toContain(
814820
doc('foo/bah', 0, { foo: 'bah' }, { hasLocalMutations: true })

0 commit comments

Comments
 (0)