@tanstack/query-core 5.59.4 → 5.59.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -207,59 +207,43 @@ export class QueriesObserver<
207
207
  #findMatchingObservers(
208
208
  queries: Array<QueryObserverOptions>,
209
209
  ): Array<QueryObserverMatch> {
210
- const prevObservers = this.#observers
211
210
  const prevObserversMap = new Map(
212
- prevObservers.map((observer) => [observer.options.queryHash, observer]),
211
+ this.#observers.map((observer) => [observer.options.queryHash, observer]),
213
212
  )
214
213
 
215
- const defaultedQueryOptions = queries.map((options) =>
216
- this.#client.defaultQueryOptions(options),
217
- )
218
-
219
- const matchingObservers: Array<QueryObserverMatch> =
220
- defaultedQueryOptions.flatMap((defaultedOptions) => {
221
- const match = prevObserversMap.get(defaultedOptions.queryHash)
222
- if (match != null) {
223
- return [{ defaultedQueryOptions: defaultedOptions, observer: match }]
224
- }
225
- return []
226
- })
227
-
228
- const matchedQueryHashes = new Set(
229
- matchingObservers.map((match) => match.defaultedQueryOptions.queryHash),
230
- )
231
- const unmatchedQueries = defaultedQueryOptions.filter(
232
- (defaultedOptions) => !matchedQueryHashes.has(defaultedOptions.queryHash),
233
- )
214
+ const observers: Array<QueryObserverMatch> = []
234
215
 
235
- const getObserver = (options: QueryObserverOptions): QueryObserver => {
216
+ queries.forEach((options) => {
236
217
  const defaultedOptions = this.#client.defaultQueryOptions(options)
237
- const currentObserver = this.#observers.find(
238
- (o) => o.options.queryHash === defaultedOptions.queryHash,
239
- )
218
+ const match = prevObserversMap.get(defaultedOptions.queryHash)
219
+ if (match) {
220
+ observers.push({
221
+ defaultedQueryOptions: defaultedOptions,
222
+ observer: match,
223
+ })
224
+ } else {
225
+ const existingObserver = this.#observers.find(
226
+ (o) => o.options.queryHash === defaultedOptions.queryHash,
227
+ )
228
+ observers.push({
229
+ defaultedQueryOptions: defaultedOptions,
230
+ observer:
231
+ existingObserver ??
232
+ new QueryObserver(this.#client, defaultedOptions),
233
+ })
234
+ }
235
+ })
236
+
237
+ return observers.sort((a, b) => {
240
238
  return (
241
- currentObserver ?? new QueryObserver(this.#client, defaultedOptions)
239
+ queries.findIndex(
240
+ (q) => q.queryHash === a.defaultedQueryOptions.queryHash,
241
+ ) -
242
+ queries.findIndex(
243
+ (q) => q.queryHash === b.defaultedQueryOptions.queryHash,
244
+ )
242
245
  )
243
- }
244
-
245
- const newOrReusedObservers: Array<QueryObserverMatch> =
246
- unmatchedQueries.map((options) => {
247
- return {
248
- defaultedQueryOptions: options,
249
- observer: getObserver(options),
250
- }
251
- })
252
-
253
- const sortMatchesByOrderOfQueries = (
254
- a: QueryObserverMatch,
255
- b: QueryObserverMatch,
256
- ): number =>
257
- defaultedQueryOptions.indexOf(a.defaultedQueryOptions) -
258
- defaultedQueryOptions.indexOf(b.defaultedQueryOptions)
259
-
260
- return matchingObservers
261
- .concat(newOrReusedObservers)
262
- .sort(sortMatchesByOrderOfQueries)
246
+ })
263
247
  }
264
248
 
265
249
  #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {
package/src/query.ts CHANGED
@@ -3,6 +3,7 @@ import {
3
3
  noop,
4
4
  replaceData,
5
5
  resolveEnabled,
6
+ skipToken,
6
7
  timeUntilStale,
7
8
  } from './utils'
8
9
  import { notifyManager } from './notifyManager'
@@ -256,7 +257,14 @@ export class Query<
256
257
  }
257
258
 
258
259
  isDisabled(): boolean {
259
- return this.getObserversCount() > 0 && !this.isActive()
260
+ if (this.getObserversCount() > 0) {
261
+ return !this.isActive()
262
+ }
263
+ // if a query has no observers, it should still be considered disabled if it never attempted a fetch
264
+ return (
265
+ this.options.queryFn === skipToken ||
266
+ this.state.dataUpdateCount + this.state.errorUpdateCount === 0
267
+ )
260
268
  }
261
269
 
262
270
  isStale(): boolean {