@tanstack/query-core 4.26.0 → 4.26.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-core",
3
- "version": "4.26.0",
3
+ "version": "4.26.1",
4
4
  "description": "The framework agnostic core that powers TanStack Query",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
package/src/mutation.ts CHANGED
@@ -89,10 +89,11 @@ export class Mutation<
89
89
  TContext = unknown,
90
90
  > extends Removable {
91
91
  state: MutationState<TData, TError, TVariables, TContext>
92
- options: MutationOptions<TData, TError, TVariables, TContext>
92
+ options!: MutationOptions<TData, TError, TVariables, TContext>
93
93
  mutationId: number
94
94
 
95
95
  private observers: MutationObserver<TData, TError, TVariables, TContext>[]
96
+ private defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>
96
97
  private mutationCache: MutationCache
97
98
  private logger: Logger
98
99
  private retryer?: Retryer<TData>
@@ -100,20 +101,25 @@ export class Mutation<
100
101
  constructor(config: MutationConfig<TData, TError, TVariables, TContext>) {
101
102
  super()
102
103
 
103
- this.options = {
104
- ...config.defaultOptions,
105
- ...config.options,
106
- }
104
+ this.defaultOptions = config.defaultOptions
107
105
  this.mutationId = config.mutationId
108
106
  this.mutationCache = config.mutationCache
109
107
  this.logger = config.logger || defaultLogger
110
108
  this.observers = []
111
109
  this.state = config.state || getDefaultState()
112
110
 
113
- this.updateCacheTime(this.options.cacheTime)
111
+ this.setOptions(config.options)
114
112
  this.scheduleGc()
115
113
  }
116
114
 
115
+ setOptions(
116
+ options?: MutationOptions<TData, TError, TVariables, TContext>,
117
+ ): void {
118
+ this.options = { ...this.defaultOptions, ...options }
119
+
120
+ this.updateCacheTime(this.options.cacheTime)
121
+ }
122
+
117
123
  get meta(): MutationMeta | undefined {
118
124
  return this.options.meta
119
125
  }
@@ -74,6 +74,7 @@ export class MutationObserver<
74
74
  observer: this,
75
75
  })
76
76
  }
77
+ this.currentMutation?.setOptions(this.options)
77
78
  }
78
79
 
79
80
  protected onUnsubscribe(): void {
@@ -2,6 +2,7 @@ import type { QueryClient } from '..'
2
2
  import { createQueryClient, executeMutation, queryKey, sleep } from './utils'
3
3
  import type { MutationState } from '../mutation'
4
4
  import { MutationObserver } from '../mutationObserver'
5
+ import { waitFor } from '@testing-library/react'
5
6
 
6
7
  describe('mutations', () => {
7
8
  let queryClient: QueryClient
@@ -358,4 +359,34 @@ describe('mutations', () => {
358
359
  expect(onSuccess).not.toHaveBeenCalled()
359
360
  expect(onSettled).not.toHaveBeenCalled()
360
361
  })
362
+
363
+ test('mutation callbacks should see updated options', async () => {
364
+ const onSuccess = jest.fn()
365
+
366
+ const mutation = new MutationObserver(queryClient, {
367
+ mutationFn: async () => {
368
+ sleep(100)
369
+ return 'update'
370
+ },
371
+ onSuccess: () => {
372
+ onSuccess(1)
373
+ },
374
+ })
375
+
376
+ void mutation.mutate()
377
+
378
+ mutation.setOptions({
379
+ mutationFn: async () => {
380
+ sleep(100)
381
+ return 'update'
382
+ },
383
+ onSuccess: () => {
384
+ onSuccess(2)
385
+ },
386
+ })
387
+
388
+ await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
389
+
390
+ expect(onSuccess).toHaveBeenCalledWith(2)
391
+ })
361
392
  })