@tanstack/query-core 5.0.0-alpha.2 → 5.0.0-alpha.3
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 -1
- package/build/lib/mutation.esm.js +10 -2
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +10 -2
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +10 -2
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationCache.d.ts +2 -2
- package/build/lib/mutationCache.esm.js.map +1 -1
- package/build/lib/mutationCache.js.map +1 -1
- package/build/lib/mutationCache.mjs.map +1 -1
- package/build/lib/mutationObserver.esm.js +1 -0
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +1 -0
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +1 -0
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/lib/query.esm.js.map +1 -1
- package/build/lib/query.js.map +1 -1
- package/build/lib/query.mjs.map +1 -1
- package/build/lib/queryCache.d.ts +2 -2
- package/build/lib/queryCache.esm.js.map +1 -1
- package/build/lib/queryCache.js.map +1 -1
- package/build/lib/queryCache.mjs.map +1 -1
- package/build/umd/index.development.js +11 -2
- 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 +14 -5
- package/src/mutationCache.ts +2 -2
- package/src/mutationObserver.ts +1 -0
- package/src/query.ts +6 -3
- package/src/queryCache.ts +5 -2
- package/src/tests/mutations.test.tsx +31 -0
package/package.json
CHANGED
package/src/mutation.ts
CHANGED
|
@@ -85,26 +85,35 @@ export class Mutation<
|
|
|
85
85
|
TContext = unknown,
|
|
86
86
|
> extends Removable {
|
|
87
87
|
state: MutationState<TData, TError, TVariables, TContext>
|
|
88
|
-
|
|
88
|
+
options!: MutationOptions<TData, TError, TVariables, TContext>
|
|
89
89
|
readonly mutationId: number
|
|
90
90
|
|
|
91
91
|
#observers: MutationObserver<TData, TError, TVariables, TContext>[]
|
|
92
|
+
#defaultOptions?: MutationOptions<TData, TError, TVariables, TContext>
|
|
92
93
|
#mutationCache: MutationCache
|
|
93
94
|
#retryer?: Retryer<TData>
|
|
94
95
|
|
|
95
96
|
constructor(config: MutationConfig<TData, TError, TVariables, TContext>) {
|
|
96
97
|
super()
|
|
97
98
|
|
|
98
|
-
this.options = config.options
|
|
99
99
|
this.mutationId = config.mutationId
|
|
100
|
+
this.#defaultOptions = config.defaultOptions
|
|
100
101
|
this.#mutationCache = config.mutationCache
|
|
101
102
|
this.#observers = []
|
|
102
103
|
this.state = config.state || getDefaultState()
|
|
103
104
|
|
|
104
|
-
this.
|
|
105
|
+
this.setOptions(config.options)
|
|
105
106
|
this.scheduleGc()
|
|
106
107
|
}
|
|
107
108
|
|
|
109
|
+
setOptions(
|
|
110
|
+
options?: MutationOptions<TData, TError, TVariables, TContext>,
|
|
111
|
+
): void {
|
|
112
|
+
this.options = { ...this.#defaultOptions, ...options }
|
|
113
|
+
|
|
114
|
+
this.updateGcTime(this.options.gcTime)
|
|
115
|
+
}
|
|
116
|
+
|
|
108
117
|
get meta(): MutationMeta | undefined {
|
|
109
118
|
return this.options.meta
|
|
110
119
|
}
|
|
@@ -228,7 +237,7 @@ export class Mutation<
|
|
|
228
237
|
try {
|
|
229
238
|
// Notify cache callback
|
|
230
239
|
await this.#mutationCache.config.onError?.(
|
|
231
|
-
error,
|
|
240
|
+
error as any,
|
|
232
241
|
variables,
|
|
233
242
|
this.state.context,
|
|
234
243
|
this as Mutation<unknown, unknown, unknown, unknown>,
|
|
@@ -243,7 +252,7 @@ export class Mutation<
|
|
|
243
252
|
// Notify cache callback
|
|
244
253
|
await this.#mutationCache.config.onSettled?.(
|
|
245
254
|
undefined,
|
|
246
|
-
error,
|
|
255
|
+
error as any,
|
|
247
256
|
this.state.variables,
|
|
248
257
|
this.state.context,
|
|
249
258
|
this as Mutation<unknown, unknown, unknown, unknown>,
|
package/src/mutationCache.ts
CHANGED
|
@@ -12,7 +12,7 @@ import { Subscribable } from './subscribable'
|
|
|
12
12
|
|
|
13
13
|
interface MutationCacheConfig {
|
|
14
14
|
onError?: (
|
|
15
|
-
error:
|
|
15
|
+
error: DefaultError,
|
|
16
16
|
variables: unknown,
|
|
17
17
|
context: unknown,
|
|
18
18
|
mutation: Mutation<unknown, unknown, unknown>,
|
|
@@ -29,7 +29,7 @@ interface MutationCacheConfig {
|
|
|
29
29
|
) => Promise<unknown> | unknown
|
|
30
30
|
onSettled?: (
|
|
31
31
|
data: unknown | undefined,
|
|
32
|
-
error:
|
|
32
|
+
error: DefaultError | null,
|
|
33
33
|
variables: unknown,
|
|
34
34
|
context: unknown,
|
|
35
35
|
mutation: Mutation<unknown, unknown, unknown>,
|
package/src/mutationObserver.ts
CHANGED
package/src/query.ts
CHANGED
|
@@ -433,10 +433,13 @@ export class Query<
|
|
|
433
433
|
|
|
434
434
|
if (!isCancelledError(error)) {
|
|
435
435
|
// Notify cache callback
|
|
436
|
-
this.#cache.config.onError?.(
|
|
436
|
+
this.#cache.config.onError?.(
|
|
437
|
+
error as any,
|
|
438
|
+
this as Query<any, any, any, any>,
|
|
439
|
+
)
|
|
437
440
|
this.#cache.config.onSettled?.(
|
|
438
441
|
this.state.data,
|
|
439
|
-
error,
|
|
442
|
+
error as any,
|
|
440
443
|
this as Query<any, any, any, any>,
|
|
441
444
|
)
|
|
442
445
|
}
|
|
@@ -469,7 +472,7 @@ export class Query<
|
|
|
469
472
|
this.#cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
|
|
470
473
|
this.#cache.config.onSettled?.(
|
|
471
474
|
data,
|
|
472
|
-
this.state.error,
|
|
475
|
+
this.state.error as any,
|
|
473
476
|
this as Query<any, any, any, any>,
|
|
474
477
|
)
|
|
475
478
|
|
package/src/queryCache.ts
CHANGED
|
@@ -17,11 +17,14 @@ import type { QueryObserver } from './queryObserver'
|
|
|
17
17
|
// TYPES
|
|
18
18
|
|
|
19
19
|
interface QueryCacheConfig {
|
|
20
|
-
onError?: (
|
|
20
|
+
onError?: (
|
|
21
|
+
error: DefaultError,
|
|
22
|
+
query: Query<unknown, unknown, unknown>,
|
|
23
|
+
) => void
|
|
21
24
|
onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void
|
|
22
25
|
onSettled?: (
|
|
23
26
|
data: unknown | undefined,
|
|
24
|
-
error:
|
|
27
|
+
error: DefaultError | null,
|
|
25
28
|
query: Query<unknown, unknown, unknown>,
|
|
26
29
|
) => void
|
|
27
30
|
createStore?: () => QueryStore
|
|
@@ -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
|
|
@@ -377,4 +378,34 @@ describe('mutations', () => {
|
|
|
377
378
|
expect(onSuccess).not.toHaveBeenCalled()
|
|
378
379
|
expect(onSettled).not.toHaveBeenCalled()
|
|
379
380
|
})
|
|
381
|
+
|
|
382
|
+
test('mutation callbacks should see updated options', async () => {
|
|
383
|
+
const onSuccess = jest.fn()
|
|
384
|
+
|
|
385
|
+
const mutation = new MutationObserver(queryClient, {
|
|
386
|
+
mutationFn: async () => {
|
|
387
|
+
sleep(100)
|
|
388
|
+
return 'update'
|
|
389
|
+
},
|
|
390
|
+
onSuccess: () => {
|
|
391
|
+
onSuccess(1)
|
|
392
|
+
},
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
void mutation.mutate()
|
|
396
|
+
|
|
397
|
+
mutation.setOptions({
|
|
398
|
+
mutationFn: async () => {
|
|
399
|
+
sleep(100)
|
|
400
|
+
return 'update'
|
|
401
|
+
},
|
|
402
|
+
onSuccess: () => {
|
|
403
|
+
onSuccess(2)
|
|
404
|
+
},
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
await waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
|
|
408
|
+
|
|
409
|
+
expect(onSuccess).toHaveBeenCalledWith(2)
|
|
410
|
+
})
|
|
380
411
|
})
|