Skip to content

Fix document change leads to time travel across multiple tabs #6619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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