Skip to content

Commit

Permalink
Fix document change leads to time travel across multiple tabs (#6619)
Browse files Browse the repository at this point in the history
* Fix document change leads to time travel across multiple tabs

* Changeset
  • Loading branch information
wu-hui committed Sep 26, 2022
1 parent a491888 commit c6ba6fc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nine-cherries-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore': patch
---

Fix a time travel issue across multiple tabs
4 changes: 3 additions & 1 deletion packages/firestore/src/local/local_store_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,9 @@ function setMaxReadTime(
collectionGroup: string,
changedDocs: SortedMap<DocumentKey, Document>
): void {
let readTime = SnapshotVersion.min();
let readTime =
localStoreImpl.collectionGroupReadTime.get(collectionGroup) ||
SnapshotVersion.min();
changedDocs.forEach((_, doc) => {
if (doc.readTime.compareTo(readTime) > 0) {
readTime = doc.readTime;
Expand Down
41 changes: 41 additions & 0 deletions packages/firestore/test/unit/specs/listen_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,47 @@ describeSpec('Listens:', [], () => {
}
);

// Reproduces: https://github.com/firebase/firebase-js-sdk/issues/6511
specTest(
'Secondary client raises latency compensated snapshot from primary mutation',
['multi-client'],
() => {
const query1 = query('collection');
const docA = doc('collection/a', 1000, { key: '1' });
const docAMutated = doc('collection/a', 1500, {
key: '2'
}).setHasLocalMutations();

return (
client(0)
.becomeVisible()
.expectPrimaryState(true)
.userListens(query1)
.watchAcksFull(query1, 1000, docA)
.expectEvents(query1, { added: [docA] })
.userUnlistens(query1)
.watchRemoves(query1)
.client(1)
.userListens(query1)
.expectEvents(query1, { added: [docA], fromCache: true })
.client(0)
.expectListen(query1, { resumeToken: 'resume-token-1000' })
.watchAcksFull(query1, 1500, docA)
.client(1)
.expectEvents(query1, {})
.client(0)
.userSets('collection/a', { key: '2' })
.client(1)
// Without the fix for 6511, this would raise two snapshots, first one as expected and
// second one travels back in time and raise the old stale document.
.expectEvents(query1, {
modified: [docAMutated],
hasPendingWrites: true
})
);
}
);

specTest(
'Mirror queries from same secondary client',
['multi-client'],
Expand Down

0 comments on commit c6ba6fc

Please sign in to comment.