@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/build/lib/mutation.d.ts +2 -0
- package/build/lib/mutation.esm.js +9 -4
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +9 -4
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +9 -4
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationObserver.esm.js +6 -2
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +6 -2
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +6 -2
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/umd/index.development.js +15 -6
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/mutation.ts +12 -6
- package/src/mutationObserver.ts +1 -0
- package/src/tests/mutations.test.tsx +31 -0
package/package.json
CHANGED
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
|
|
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.
|
|
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.
|
|
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
|
}
|
package/src/mutationObserver.ts
CHANGED
|
@@ -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
|
})
|