Skip to content

Commit 91484ec

Browse files
committed
Fix document change leads to time travel across multiple tabs
1 parent b3951c6 commit 91484ec

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/firestore/src/local/local_store_impl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,9 @@ function setMaxReadTime(
12731273
collectionGroup: string,
12741274
changedDocs: SortedMap<DocumentKey, Document>
12751275
): void {
1276-
let readTime = SnapshotVersion.min();
1276+
let readTime =
1277+
localStoreImpl.collectionGroupReadTime.get(collectionGroup) ||
1278+
SnapshotVersion.min();
12771279
changedDocs.forEach((_, doc) => {
12781280
if (doc.readTime.compareTo(readTime) > 0) {
12791281
readTime = doc.readTime;

packages/firestore/test/unit/specs/listen_spec.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,47 @@ describeSpec('Listens:', [], () => {
909909
}
910910
);
911911

912+
// Reproduces: https://github.com/firebase/firebase-js-sdk/issues/6511
913+
specTest(
914+
'Secondary client raises latency compensated snapshot from primary mutation',
915+
['multi-client'],
916+
() => {
917+
const query1 = query('collection');
918+
const docA = doc('collection/a', 1000, { key: '1' });
919+
const docAMutated = doc('collection/a', 1500, {
920+
key: '2'
921+
}).setHasLocalMutations();
922+
923+
return (
924+
client(0)
925+
.becomeVisible()
926+
.expectPrimaryState(true)
927+
.userListens(query1)
928+
.watchAcksFull(query1, 1000, docA)
929+
.expectEvents(query1, { added: [docA] })
930+
.userUnlistens(query1)
931+
.watchRemoves(query1)
932+
.client(1)
933+
.userListens(query1)
934+
.expectEvents(query1, { added: [docA], fromCache: true })
935+
.client(0)
936+
.expectListen(query1, { resumeToken: 'resume-token-1000' })
937+
.watchAcksFull(query1, 1500, docA)
938+
.client(1)
939+
.expectEvents(query1, {})
940+
.client(0)
941+
.userSets('collection/a', { key: '2' })
942+
.client(1)
943+
// Without the fix for 6511, this would raise two snapshots, first one as expected and
944+
// second one travels back in time and raise the old stale document.
945+
.expectEvents(query1, {
946+
modified: [docAMutated],
947+
hasPendingWrites: true
948+
})
949+
);
950+
}
951+
);
952+
912953
specTest(
913954
'Mirror queries from same secondary client',
914955
['multi-client'],

0 commit comments

Comments
 (0)