Skip to content

Commit c6ba6fc

Browse files
authored
Fix document change leads to time travel across multiple tabs (#6619)
* Fix document change leads to time travel across multiple tabs * Changeset
1 parent a491888 commit c6ba6fc

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

.changeset/nine-cherries-swim.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/firestore': patch
3+
---
4+
5+
Fix a time travel issue across multiple tabs

packages/firestore/src/local/local_store_impl.ts

+3-1
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

+41
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)