Skip to content

Commit 13e69b0

Browse files
author
nrahnema
committed
fix(query-core): fix queriesObserver result and observer length mismatch
In 1b54538, `setOptions` was changed to call `notifyListeners` which eventually leads to `QueriesObserver.setQueries` 1. Updating `#observerMatches` 2. Calling `setOptions` which leads to `#notify` being called 3. Updating `#result` This is an issue when `#notify` is called before the `#result` is updated, as `#trackResult` assumes these two are the same length, which may not be the case if the number of queries changed. When the length increases, `observerResult` becomes `undefined` and the function throws an exception. Fix by moving the `#observerMatches` assignment later in the function, next to the `#observers` and `#result` assignment.
1 parent f15b7fc commit 13e69b0

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

.changeset/orange-flowers-post.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
fix useQueries race condition on queries length change (#9971)

packages/query-core/src/queriesObserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export class QueriesObserver<
106106
const prevObservers = this.#observers
107107

108108
const newObserverMatches = this.#findMatchingObservers(this.#queries)
109-
this.#observerMatches = newObserverMatches
110109

111110
// set options for the new observers to notify of changes
112111
newObserverMatches.forEach((match) =>
@@ -134,6 +133,7 @@ export class QueriesObserver<
134133
if (!hasStructuralChange && !hasResultChange) return
135134

136135
if (hasStructuralChange) {
136+
this.#observerMatches = newObserverMatches
137137
this.#observers = newObservers
138138
}
139139

0 commit comments

Comments
 (0)