@tanstack/query-core 5.28.0 → 5.28.4

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.
Files changed (93) hide show
  1. package/build/legacy/hydration.d.cts +1 -1
  2. package/build/legacy/hydration.d.ts +1 -1
  3. package/build/legacy/index.d.cts +1 -1
  4. package/build/legacy/index.d.ts +1 -1
  5. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  6. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  7. package/build/legacy/infiniteQueryObserver.d.cts +1 -1
  8. package/build/legacy/infiniteQueryObserver.d.ts +1 -1
  9. package/build/legacy/mutation.d.cts +1 -1
  10. package/build/legacy/mutation.d.ts +1 -1
  11. package/build/legacy/mutationCache.d.cts +1 -1
  12. package/build/legacy/mutationCache.d.ts +1 -1
  13. package/build/legacy/mutationObserver.cjs +2 -2
  14. package/build/legacy/mutationObserver.cjs.map +1 -1
  15. package/build/legacy/mutationObserver.d.cts +1 -1
  16. package/build/legacy/mutationObserver.d.ts +1 -1
  17. package/build/legacy/mutationObserver.js +2 -2
  18. package/build/legacy/mutationObserver.js.map +1 -1
  19. package/build/legacy/queriesObserver.d.cts +1 -1
  20. package/build/legacy/queriesObserver.d.ts +1 -1
  21. package/build/legacy/query.cjs +17 -10
  22. package/build/legacy/query.cjs.map +1 -1
  23. package/build/legacy/query.d.cts +1 -1
  24. package/build/legacy/query.d.ts +1 -1
  25. package/build/legacy/query.js +15 -9
  26. package/build/legacy/query.js.map +1 -1
  27. package/build/legacy/queryCache.d.cts +1 -1
  28. package/build/legacy/queryCache.d.ts +1 -1
  29. package/build/legacy/{queryClient-UWB6EkqM.d.ts → queryClient-6vLRMq5C.d.ts} +8 -1
  30. package/build/legacy/{queryClient-WxLAyIG_.d.cts → queryClient-C_S9rQE6.d.cts} +8 -1
  31. package/build/legacy/queryClient.d.cts +1 -1
  32. package/build/legacy/queryClient.d.ts +1 -1
  33. package/build/legacy/queryObserver.cjs +22 -21
  34. package/build/legacy/queryObserver.cjs.map +1 -1
  35. package/build/legacy/queryObserver.d.cts +1 -1
  36. package/build/legacy/queryObserver.d.ts +1 -1
  37. package/build/legacy/queryObserver.js +22 -21
  38. package/build/legacy/queryObserver.js.map +1 -1
  39. package/build/legacy/retryer.d.cts +1 -1
  40. package/build/legacy/retryer.d.ts +1 -1
  41. package/build/legacy/types.d.cts +1 -1
  42. package/build/legacy/types.d.ts +1 -1
  43. package/build/legacy/utils.d.cts +1 -1
  44. package/build/legacy/utils.d.ts +1 -1
  45. package/build/modern/hydration.d.cts +1 -1
  46. package/build/modern/hydration.d.ts +1 -1
  47. package/build/modern/index.d.cts +1 -1
  48. package/build/modern/index.d.ts +1 -1
  49. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  50. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  51. package/build/modern/infiniteQueryObserver.d.cts +1 -1
  52. package/build/modern/infiniteQueryObserver.d.ts +1 -1
  53. package/build/modern/mutation.d.cts +1 -1
  54. package/build/modern/mutation.d.ts +1 -1
  55. package/build/modern/mutationCache.d.cts +1 -1
  56. package/build/modern/mutationCache.d.ts +1 -1
  57. package/build/modern/mutationObserver.cjs +2 -2
  58. package/build/modern/mutationObserver.cjs.map +1 -1
  59. package/build/modern/mutationObserver.d.cts +1 -1
  60. package/build/modern/mutationObserver.d.ts +1 -1
  61. package/build/modern/mutationObserver.js +2 -2
  62. package/build/modern/mutationObserver.js.map +1 -1
  63. package/build/modern/queriesObserver.d.cts +1 -1
  64. package/build/modern/queriesObserver.d.ts +1 -1
  65. package/build/modern/query.cjs +17 -10
  66. package/build/modern/query.cjs.map +1 -1
  67. package/build/modern/query.d.cts +1 -1
  68. package/build/modern/query.d.ts +1 -1
  69. package/build/modern/query.js +15 -9
  70. package/build/modern/query.js.map +1 -1
  71. package/build/modern/queryCache.d.cts +1 -1
  72. package/build/modern/queryCache.d.ts +1 -1
  73. package/build/modern/{queryClient-UWB6EkqM.d.ts → queryClient-6vLRMq5C.d.ts} +8 -1
  74. package/build/modern/{queryClient-WxLAyIG_.d.cts → queryClient-C_S9rQE6.d.cts} +8 -1
  75. package/build/modern/queryClient.d.cts +1 -1
  76. package/build/modern/queryClient.d.ts +1 -1
  77. package/build/modern/queryObserver.cjs +22 -21
  78. package/build/modern/queryObserver.cjs.map +1 -1
  79. package/build/modern/queryObserver.d.cts +1 -1
  80. package/build/modern/queryObserver.d.ts +1 -1
  81. package/build/modern/queryObserver.js +22 -21
  82. package/build/modern/queryObserver.js.map +1 -1
  83. package/build/modern/retryer.d.cts +1 -1
  84. package/build/modern/retryer.d.ts +1 -1
  85. package/build/modern/types.d.cts +1 -1
  86. package/build/modern/types.d.ts +1 -1
  87. package/build/modern/utils.d.cts +1 -1
  88. package/build/modern/utils.d.ts +1 -1
  89. package/package.json +1 -1
  90. package/src/mutationObserver.ts +2 -2
  91. package/src/query.ts +22 -9
  92. package/src/queryObserver.ts +24 -24
  93. package/src/tests/mutationObserver.test.tsx +154 -0
@@ -9,9 +9,9 @@ import {
9
9
  import { notifyManager } from './notifyManager'
10
10
  import { focusManager } from './focusManager'
11
11
  import { Subscribable } from './subscribable'
12
- import { canFetch } from './retryer'
13
- import type { QueryClient } from './queryClient'
12
+ import { fetchState } from './query'
14
13
  import type { FetchOptions, Query, QueryState } from './query'
14
+ import type { QueryClient } from './queryClient'
15
15
  import type {
16
16
  DefaultError,
17
17
  DefaultedQueryObserverOptions,
@@ -437,7 +437,7 @@ export class QueryObserver<
437
437
  : this.#currentQueryInitialState
438
438
 
439
439
  const { state } = query
440
- let { error, errorUpdatedAt, fetchStatus, status } = state
440
+ let newState = { ...state }
441
441
  let isPlaceholderData = false
442
442
  let data: TData | undefined
443
443
 
@@ -451,31 +451,31 @@ export class QueryObserver<
451
451
  mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions)
452
452
 
453
453
  if (fetchOnMount || fetchOptionally) {
454
- fetchStatus = canFetch(query.options.networkMode)
455
- ? 'fetching'
456
- : 'paused'
457
- if (state.data === undefined) {
458
- status = 'pending'
454
+ newState = {
455
+ ...newState,
456
+ ...fetchState(state.data, query.options),
459
457
  }
460
458
  }
461
459
  if (options._optimisticResults === 'isRestoring') {
462
- fetchStatus = 'idle'
460
+ newState.fetchStatus = 'idle'
463
461
  }
464
462
  }
465
463
 
464
+ let { error, errorUpdatedAt, status } = newState
465
+
466
466
  // Select data if needed
467
- if (options.select && state.data !== undefined) {
467
+ if (options.select && newState.data !== undefined) {
468
468
  // Memoize select result
469
469
  if (
470
470
  prevResult &&
471
- state.data === prevResultState?.data &&
471
+ newState.data === prevResultState?.data &&
472
472
  options.select === this.#selectFn
473
473
  ) {
474
474
  data = this.#selectResult
475
475
  } else {
476
476
  try {
477
477
  this.#selectFn = options.select
478
- data = options.select(state.data)
478
+ data = options.select(newState.data)
479
479
  data = replaceData(prevResult?.data, data, options)
480
480
  this.#selectResult = data
481
481
  this.#selectError = null
@@ -486,7 +486,7 @@ export class QueryObserver<
486
486
  }
487
487
  // Use query data
488
488
  else {
489
- data = state.data as unknown as TData
489
+ data = newState.data as unknown as TData
490
490
  }
491
491
 
492
492
  // Show placeholder data if needed
@@ -541,36 +541,36 @@ export class QueryObserver<
541
541
  status = 'error'
542
542
  }
543
543
 
544
- const isFetching = fetchStatus === 'fetching'
544
+ const isFetching = newState.fetchStatus === 'fetching'
545
545
  const isPending = status === 'pending'
546
546
  const isError = status === 'error'
547
547
 
548
548
  const isLoading = isPending && isFetching
549
- const hasData = state.data !== undefined
549
+ const hasData = data !== undefined
550
550
 
551
551
  const result: QueryObserverBaseResult<TData, TError> = {
552
552
  status,
553
- fetchStatus,
553
+ fetchStatus: newState.fetchStatus,
554
554
  isPending,
555
555
  isSuccess: status === 'success',
556
556
  isError,
557
557
  isInitialLoading: isLoading,
558
558
  isLoading,
559
559
  data,
560
- dataUpdatedAt: state.dataUpdatedAt,
560
+ dataUpdatedAt: newState.dataUpdatedAt,
561
561
  error,
562
562
  errorUpdatedAt,
563
- failureCount: state.fetchFailureCount,
564
- failureReason: state.fetchFailureReason,
565
- errorUpdateCount: state.errorUpdateCount,
566
- isFetched: state.dataUpdateCount > 0 || state.errorUpdateCount > 0,
563
+ failureCount: newState.fetchFailureCount,
564
+ failureReason: newState.fetchFailureReason,
565
+ errorUpdateCount: newState.errorUpdateCount,
566
+ isFetched: newState.dataUpdateCount > 0 || newState.errorUpdateCount > 0,
567
567
  isFetchedAfterMount:
568
- state.dataUpdateCount > queryInitialState.dataUpdateCount ||
569
- state.errorUpdateCount > queryInitialState.errorUpdateCount,
568
+ newState.dataUpdateCount > queryInitialState.dataUpdateCount ||
569
+ newState.errorUpdateCount > queryInitialState.errorUpdateCount,
570
570
  isFetching,
571
571
  isRefetching: isFetching && !isPending,
572
572
  isLoadingError: isError && !hasData,
573
- isPaused: fetchStatus === 'paused',
573
+ isPaused: newState.fetchStatus === 'paused',
574
574
  isPlaceholderData,
575
575
  isRefetchError: isError && hasData,
576
576
  isStale: isStale(query, options),
@@ -169,4 +169,158 @@ describe('mutationObserver', () => {
169
169
 
170
170
  unsubscribe()
171
171
  })
172
+
173
+ test('changing mutation meta should not affect successful mutations', async () => {
174
+ const mutationObserver = new MutationObserver(queryClient, {
175
+ meta: { a: 1 },
176
+ mutationFn: async (text: string) => {
177
+ await sleep(5)
178
+ return text
179
+ },
180
+ })
181
+
182
+ const subscriptionHandler = vi.fn()
183
+
184
+ const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
185
+
186
+ await mutationObserver.mutate('input')
187
+
188
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
189
+ options: { meta: { a: 1 } },
190
+ state: {
191
+ status: 'success',
192
+ data: 'input',
193
+ },
194
+ })
195
+
196
+ mutationObserver.setOptions({
197
+ meta: { a: 2 },
198
+ })
199
+
200
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
201
+ options: { meta: { a: 1 } },
202
+ state: {
203
+ status: 'success',
204
+ data: 'input',
205
+ },
206
+ })
207
+
208
+ unsubscribe()
209
+ })
210
+
211
+ test('mutation cache should have different meta when updated between mutations', async () => {
212
+ const mutationFn = async (text: string) => {
213
+ await sleep(5)
214
+ return text
215
+ }
216
+ const mutationObserver = new MutationObserver(queryClient, {
217
+ meta: { a: 1 },
218
+ mutationFn,
219
+ })
220
+
221
+ const subscriptionHandler = vi.fn()
222
+
223
+ const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
224
+
225
+ await mutationObserver.mutate('input')
226
+
227
+ mutationObserver.setOptions({
228
+ meta: { a: 2 },
229
+ mutationFn,
230
+ })
231
+
232
+ await mutationObserver.mutate('input')
233
+
234
+ const mutations = queryClient.getMutationCache().findAll()
235
+ expect(mutations[0]).toMatchObject({
236
+ options: { meta: { a: 1 } },
237
+ state: {
238
+ status: 'success',
239
+ data: 'input',
240
+ },
241
+ })
242
+ expect(mutations[1]).toMatchObject({
243
+ options: { meta: { a: 2 } },
244
+ state: {
245
+ status: 'success',
246
+ data: 'input',
247
+ },
248
+ })
249
+
250
+ unsubscribe()
251
+ })
252
+
253
+ test('changing mutation meta should not affect rejected mutations', async () => {
254
+ const mutationObserver = new MutationObserver(queryClient, {
255
+ meta: { a: 1 },
256
+ mutationFn: async (_: string) => {
257
+ await sleep(5)
258
+ return Promise.reject(new Error('err'))
259
+ },
260
+ })
261
+
262
+ const subscriptionHandler = vi.fn()
263
+
264
+ const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
265
+
266
+ await mutationObserver.mutate('input').catch(() => undefined)
267
+
268
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
269
+ options: { meta: { a: 1 } },
270
+ state: {
271
+ status: 'error',
272
+ },
273
+ })
274
+
275
+ mutationObserver.setOptions({
276
+ meta: { a: 2 },
277
+ })
278
+
279
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
280
+ options: { meta: { a: 1 } },
281
+ state: {
282
+ status: 'error',
283
+ },
284
+ })
285
+
286
+ unsubscribe()
287
+ })
288
+
289
+ test('changing mutation meta should affect pending mutations', async () => {
290
+ const mutationObserver = new MutationObserver(queryClient, {
291
+ meta: { a: 1 },
292
+ mutationFn: async (text: string) => {
293
+ await sleep(20)
294
+ return text
295
+ },
296
+ })
297
+
298
+ const subscriptionHandler = vi.fn()
299
+
300
+ const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
301
+
302
+ mutationObserver.mutate('input')
303
+
304
+ await sleep(0)
305
+
306
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
307
+ options: { meta: { a: 1 } },
308
+ state: {
309
+ status: 'pending',
310
+ },
311
+ })
312
+
313
+ mutationObserver.setOptions({
314
+ meta: { a: 2 },
315
+ })
316
+
317
+ expect(queryClient.getMutationCache().find({})).toMatchObject({
318
+ options: { meta: { a: 2 } },
319
+ state: {
320
+ status: 'pending',
321
+ },
322
+ })
323
+
324
+ unsubscribe()
325
+ })
172
326
  })