Skip to content

Commit 2ae2fbe

Browse files
committed
feat: add scope deprecate context on mutation state
1 parent fe9c2ed commit 2ae2fbe

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

packages/query-core/src/__tests__/mutations.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('mutations', () => {
6464

6565
expect(mutation.getCurrentResult()).toEqual({
6666
context: undefined,
67+
scope: undefined,
6768
data: undefined,
6869
error: null,
6970
failureCount: 0,
@@ -92,6 +93,7 @@ describe('mutations', () => {
9293

9394
expect(states[0]).toEqual({
9495
context: undefined,
96+
scope: undefined,
9597
data: undefined,
9698
error: null,
9799
failureCount: 0,
@@ -112,6 +114,7 @@ describe('mutations', () => {
112114

113115
expect(states[1]).toEqual({
114116
context: 'todo',
117+
scope: 'todo',
115118
data: undefined,
116119
error: null,
117120
failureCount: 0,
@@ -132,6 +135,7 @@ describe('mutations', () => {
132135

133136
expect(states[2]).toEqual({
134137
context: 'todo',
138+
scope: 'todo',
135139
data: 'todo',
136140
error: null,
137141
failureCount: 0,
@@ -170,6 +174,7 @@ describe('mutations', () => {
170174

171175
expect(states[0]).toEqual({
172176
context: undefined,
177+
scope: undefined,
173178
data: undefined,
174179
error: null,
175180
failureCount: 0,
@@ -190,6 +195,7 @@ describe('mutations', () => {
190195

191196
expect(states[1]).toEqual({
192197
context: 'todo',
198+
scope: 'todo',
193199
data: undefined,
194200
error: null,
195201
failureCount: 0,
@@ -210,6 +216,7 @@ describe('mutations', () => {
210216

211217
expect(states[2]).toEqual({
212218
context: 'todo',
219+
scope: 'todo',
213220
data: undefined,
214221
error: null,
215222
failureCount: 1,
@@ -230,6 +237,7 @@ describe('mutations', () => {
230237

231238
expect(states[3]).toEqual({
232239
context: 'todo',
240+
scope: 'todo',
233241
data: undefined,
234242
error: new Error('err'),
235243
failureCount: 2,
@@ -270,6 +278,7 @@ describe('mutations', () => {
270278
},
271279
{
272280
context: 'todo',
281+
scope: 'todo',
273282
data: undefined,
274283
error: null,
275284
failureCount: 1,
@@ -283,6 +292,7 @@ describe('mutations', () => {
283292

284293
expect(mutation.state).toEqual({
285294
context: 'todo',
295+
scope: 'todo',
286296
data: undefined,
287297
error: null,
288298
failureCount: 1,
@@ -298,6 +308,7 @@ describe('mutations', () => {
298308
// check that the mutation is correctly resumed
299309
expect(mutation.state).toEqual({
300310
context: 'todo',
311+
scope: 'todo',
301312
data: undefined,
302313
error: null,
303314
failureCount: 1,
@@ -312,6 +323,7 @@ describe('mutations', () => {
312323

313324
expect(mutation.state).toEqual({
314325
context: 'todo',
326+
scope: 'todo',
315327
data: 'todo',
316328
error: null,
317329
failureCount: 0,

packages/query-core/src/mutation.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export interface MutationState<
2929
TVariables = unknown,
3030
TScope = unknown,
3131
> {
32+
/**
33+
* @deprecated This will be removed in the next major version. Use the `scope` property instead.
34+
*/
3235
context: TScope | undefined
36+
scope: TScope | undefined
3337
data: TData | undefined
3438
error: TError | null
3539
failureCount: number
@@ -50,7 +54,7 @@ interface PendingAction<TVariables, TScope> {
5054
type: 'pending'
5155
isPaused: boolean
5256
variables?: TVariables
53-
context?: TScope
57+
scope?: TScope
5458
}
5559

5660
interface SuccessAction<TData> {
@@ -212,14 +216,14 @@ export class Mutation<
212216
variables,
213217
this as Mutation<unknown, unknown, unknown, unknown>,
214218
)
215-
const context = await this.options.onMutate?.(
219+
const scope = await this.options.onMutate?.(
216220
variables,
217221
mutationFnContext,
218222
)
219-
if (context !== this.state.context) {
223+
if (scope !== this.state.scope) {
220224
this.#dispatch({
221225
type: 'pending',
222-
context,
226+
scope,
223227
variables,
224228
isPaused,
225229
})
@@ -231,22 +235,22 @@ export class Mutation<
231235
await this.#mutationCache.config.onSuccess?.(
232236
data,
233237
variables,
234-
this.state.context,
238+
this.state.scope,
235239
this as Mutation<unknown, unknown, unknown, unknown>,
236240
)
237241

238-
await this.options.onSuccess?.(data, variables, this.state.context!)
242+
await this.options.onSuccess?.(data, variables, this.state.scope!)
239243

240244
// Notify cache callback
241245
await this.#mutationCache.config.onSettled?.(
242246
data,
243247
null,
244248
this.state.variables,
245-
this.state.context,
249+
this.state.scope,
246250
this as Mutation<unknown, unknown, unknown, unknown>,
247251
)
248252

249-
await this.options.onSettled?.(data, null, variables, this.state.context)
253+
await this.options.onSettled?.(data, null, variables, this.state.scope)
250254

251255
this.#dispatch({ type: 'success', data })
252256
return data
@@ -256,30 +260,30 @@ export class Mutation<
256260
await this.#mutationCache.config.onError?.(
257261
error as any,
258262
variables,
259-
this.state.context,
263+
this.state.scope,
260264
this as Mutation<unknown, unknown, unknown, unknown>,
261265
)
262266

263267
await this.options.onError?.(
264268
error as TError,
265269
variables,
266-
this.state.context,
270+
this.state.scope,
267271
)
268272

269273
// Notify cache callback
270274
await this.#mutationCache.config.onSettled?.(
271275
undefined,
272276
error as any,
273277
this.state.variables,
274-
this.state.context,
278+
this.state.scope,
275279
this as Mutation<unknown, unknown, unknown, unknown>,
276280
)
277281

278282
await this.options.onSettled?.(
279283
undefined,
280284
error as TError,
281285
variables,
282-
this.state.context,
286+
this.state.scope,
283287
)
284288
throw error
285289
} finally {
@@ -314,7 +318,8 @@ export class Mutation<
314318
case 'pending':
315319
return {
316320
...state,
317-
context: action.context,
321+
context: action.scope,
322+
scope: action.scope,
318323
data: undefined,
319324
failureCount: 0,
320325
failureReason: null,
@@ -369,6 +374,7 @@ export function getDefaultState<
369374
>(): MutationState<TData, TError, TVariables, TScope> {
370375
return {
371376
context: undefined,
377+
scope: undefined,
372378
data: undefined,
373379
error: null,
374380
failureCount: 0,

packages/query-core/src/mutationObserver.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ export class MutationObserver<
146146
// First trigger the mutate callbacks
147147
if (this.#mutateOptions && this.hasListeners()) {
148148
const variables = this.#currentResult.variables!
149-
const context = this.#currentResult.context
149+
const scope = this.#currentResult.scope
150150

151151
if (action?.type === 'success') {
152-
this.#mutateOptions.onSuccess?.(action.data, variables, context!)
153-
this.#mutateOptions.onSettled?.(action.data, null, variables, context)
152+
this.#mutateOptions.onSuccess?.(action.data, variables, scope!)
153+
this.#mutateOptions.onSettled?.(action.data, null, variables, scope)
154154
} else if (action?.type === 'error') {
155-
this.#mutateOptions.onError?.(action.error, variables, context)
155+
this.#mutateOptions.onError?.(action.error, variables, scope)
156156
this.#mutateOptions.onSettled?.(
157157
undefined,
158158
action.error,
159159
variables,
160-
context,
160+
scope,
161161
)
162162
}
163163
}

packages/query-devtools/src/Devtools.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,7 @@ const MutationDetails = () => {
24272427
<Explorer
24282428
label="Context"
24292429
defaultExpanded={['Context']}
2430-
value={activeMutation()!.state.context}
2430+
value={activeMutation()!.state.scope}
24312431
/>
24322432
</div>
24332433
<div class={cx(styles().detailsHeader, 'tsqd-query-details-header')}>

0 commit comments

Comments
 (0)