Skip to content

Commit 1cd68b1

Browse files
Change the order of checks in applyRemoteEvent()
1 parent 4016987 commit 1cd68b1

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

packages/firestore/src/local/local_store.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,21 +516,32 @@ 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.NONE) never add documents to
523+
// cache.
519524
if (
520-
existingDoc == null ||
521-
doc.version.compareTo(existingDoc.version) > 0 ||
522-
(doc.version.compareTo(existingDoc.version) === 0 &&
523-
existingDoc.hasPendingWrites)
525+
doc instanceof NoDocument &&
526+
doc.version.isEqual(SnapshotVersion.MIN)
524527
) {
528+
// NoDocuments with SnapshotVersion.MIN are used in manufactured
529+
// events. We remove these documents from cache since we lost
530+
// access.
525531
documentBuffer.addEntry(doc);
526532
changedDocs = changedDocs.insert(key, doc);
527533
} else if (
528-
doc instanceof NoDocument &&
529-
doc.version.isEqual(SnapshotVersion.MIN)
534+
existingDoc == null ||
535+
doc.version.compareTo(existingDoc.version) > 0 ||
536+
(doc.version.compareTo(existingDoc.version) === 0 &&
537+
existingDoc.hasPendingWrites)
530538
) {
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.
539+
// TODO(index-free): Comment in this assert when we enable
540+
// Index-Free queries
541+
assert(
542+
!SnapshotVersion.MIN.isEqual(remoteEvent.snapshotVersion),
543+
'Cannot add a document when the remote version is zero'
544+
);
534545
documentBuffer.removeEntry(key);
535546
changedDocs = changedDocs.insert(key, doc);
536547
} else {

0 commit comments

Comments
 (0)