Repository navigation
Expand file tree
/
Copy pathmutation.ts
More file actions
556 lines (511 loc) · 15.8 KB
/
Copy pathmutation.ts
File metadata and controls
556 lines (511 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import { notifyManager } from './notifyManager'
import { Removable } from './removable'
import { createRetryer } from './retryer'
import type {
DefaultError,
MutationFunctionContext,
MutationMeta,
MutationOptions,
MutationStatus,
} from './types'
import type { MutationCache } from './mutationCache'
import type { MutationObserver } from './mutationObserver'
import type { Retryer } from './retryer'
import type { QueryClient } from './queryClient'
// TYPES
interface MutationConfig<TData, TError, TVariables, TOnMutateResult> {
client: QueryClient
mutationId: number
mutationCache: MutationCache
options: MutationOptions<TData, TError, TVariables, TOnMutateResult>
state?: MutationState<TData, TError, TVariables, TOnMutateResult>
}
/**
* The raw state stored on a `Mutation` instance. This is the underlying state
* that observer results (e.g. `MutationObserverResult`) are derived from.
*/
export interface MutationState<
TData = unknown,
TError = DefaultError,
TVariables = unknown,
TOnMutateResult = unknown,
> {
/**
* The value returned by `onMutate`, if defined. Passed to `onSuccess`,
* `onError` and `onSettled` as the mutation's context.
*/
context: TOnMutateResult | undefined
/**
* The last successfully resolved data for the mutation.
*/
data: TData | undefined
/**
* The error object for the mutation, if the last attempt resulted in an error.
* - Defaults to `null`.
*/
error: TError | null
/**
* The number of times the mutation function has failed for the current attempt.
*/
failureCount: number
/**
* The reason the current attempt failed, as reported by the retryer.
*/
failureReason: TError | null
/**
* Whether the mutation is currently paused (see network mode), or is
* waiting for another mutation with the same `scope` to finish.
*/
isPaused: boolean
/**
* The status of the mutation.
*/
status: MutationStatus
/**
* The variables the mutation was last called with.
*/
variables: TVariables | undefined
/**
* The timestamp for when the mutation was submitted.
*/
submittedAt: number
}
interface FailedAction<TError> {
type: 'failed'
failureCount: number
error: TError | null
}
interface PendingAction<TVariables, TOnMutateResult> {
type: 'pending'
isPaused: boolean
variables?: TVariables
context?: TOnMutateResult
}
interface SuccessAction<TData> {
type: 'success'
data: TData
}
interface ErrorAction<TError> {
type: 'error'
error: TError
}
interface PauseAction {
type: 'pause'
}
interface ContinueAction {
type: 'continue'
}
/**
* The actions a mutation dispatches to update its state, e.g. when it starts, fails, pauses, or
* succeeds.
*/
export type Action<TData, TError, TVariables, TOnMutateResult> =
| ContinueAction
| ErrorAction<TError>
| FailedAction<TError>
| PendingAction<TVariables, TOnMutateResult>
| PauseAction
| SuccessAction<TData>
// CLASS
/**
* Represents a single mutation attempt. A `Mutation` holds the mutation's
* options, state (data/error/status), and the `MutationObserver`s currently
* subscribed to it.
*
* Instances are created and managed internally by `MutationCache`; application
* code typically interacts with mutations indirectly through `QueryClient` or
* a framework hook like `useMutation`. Direct access to a `Mutation` instance
* is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.
* @example
* ```ts
* const mutationCache = queryClient.getMutationCache()
*
* const mutation = mutationCache.find({ mutationKey: ['addPost'] })
* ```
*/
export class Mutation<
TData = unknown,
TError = DefaultError,
TVariables = unknown,
TOnMutateResult = unknown,
> extends Removable {
state: MutationState<TData, TError, TVariables, TOnMutateResult>
options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>
readonly mutationId: number
#client: QueryClient
#observers: Array<
MutationObserver<TData, TError, TVariables, TOnMutateResult>
>
#mutationCache: MutationCache
#retryer?: Retryer<TData>
constructor(
config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,
) {
super()
this.#client = config.client
this.mutationId = config.mutationId
this.#mutationCache = config.mutationCache
this.#observers = []
this.state = config.state || getDefaultState()
this.setOptions(config.options)
this.scheduleGc()
}
/**
* Replaces the mutation's options and applies their `gcTime`.
* @internal
* @param options - The new mutation options.
*/
setOptions(
options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,
): void {
this.options = options
this.updateGcTime(this.options.gcTime)
}
/**
* The `meta` object passed in the mutation's options, if any.
* @returns The mutation's `meta`, or `undefined` if none was set.
*/
get meta(): MutationMeta | undefined {
return this.options.meta
}
/**
* Subscribes an observer to the mutation and stops its garbage collection.
* @internal
* @param observer - The observer to add. Adding an observer that is already subscribed does
* nothing.
*/
addObserver(observer: MutationObserver<any, any, any, any>): void {
if (!this.#observers.includes(observer)) {
this.#observers.push(observer)
// Stop the mutation from being garbage collected
this.clearGcTimeout()
this.#mutationCache.notify({
type: 'observerAdded',
mutation: this,
observer,
})
}
}
/**
* Unsubscribes an observer from the mutation.
* @internal
* @param observer - The observer to remove. Garbage collection is scheduled afterwards.
*/
removeObserver(observer: MutationObserver<any, any, any, any>): void {
this.#observers = this.#observers.filter((x) => x !== observer)
this.scheduleGc()
this.#mutationCache.notify({
type: 'observerRemoved',
mutation: this,
observer,
})
}
protected override optionalRemove() {
if (!this.#observers.length) {
if (this.state.status === 'pending') {
this.scheduleGc()
} else {
this.#mutationCache.remove(this)
}
}
}
/**
* Resumes a mutation that is currently paused or was restored from a
* dehydrated, still-`pending` state.
*
* - If this mutation has an active retryer (it paused mid-attempt, e.g. due
* to the network mode or scope-based queuing), its retryer is resumed.
* - Otherwise, if the mutation's status is still `pending` (e.g. it was
* dehydrated while an attempt was in flight and never got a retryer in
* this instance), `execute` is called again with the last known variables.
* - Otherwise the mutation has already settled and this resolves immediately
* without running anything again.
* @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.
* It resolves immediately if the mutation has already settled.
* @see {@link Mutation#execute}
* @example
* ```ts
* // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()
* const mutation = mutationCache.find({ mutationKey: ['addPost'] })
* await mutation?.continue()
* ```
*/
continue(): Promise<unknown> {
return (
this.#retryer?.continue() ??
// continuing a mutation assumes that variables are set, mutation must have been dehydrated before.
// a settled mutation has no retryer to continue and must not run again
(this.state.status === 'pending'
? this.execute(this.state.variables!)
: Promise.resolve())
)
}
/**
* Runs the mutation function for the given variables through a retryer, and
* drives the mutation's state and lifecycle callbacks through to settlement.
*
* If this mutation's state is already `pending` when `execute` is called
* (i.e. it was restored, still in-flight, from a dehydrated state), the
* `onMutate` step is skipped and a `continue` action is dispatched to
* unpause it; otherwise a `pending` action is dispatched first, then the
* mutation cache's `onMutate` and the mutation's own `onMutate` option are
* awaited in that order, and the resulting context is stored.
*
* The mutation function is then run (subject to `retry`/`retryDelay`/
* `networkMode`, and to the mutation cache's scope-based serialization).
* On success, the cache's `onSuccess`/`onSettled` callbacks run before the
* mutation's own `onSuccess`/`onSettled` options, a `success` action is
* dispatched, and the resolved data is returned. On failure, the same
* cache-then-option ordering is used for `onError`/`onSettled`, but each of
* those four callbacks is individually caught so that a throwing callback
* cannot mask the original error; an `error` action is then dispatched and
* the original error is re-thrown.
* @param variables - The variables passed to the `mutationFn`.
* @returns A promise that resolves with the mutation's data, or rejects with its error.
* @see {@link Mutation#continue}
* @example
* ```ts
* // Called internally by `MutationObserver.mutate` and `Mutation.continue` —
* // applications normally trigger mutations through those, not this method.
* const data = await mutation.execute(variables)
* ```
*/
async execute(variables: TVariables): Promise<TData> {
const onContinue = () => {
this.#dispatch({ type: 'continue' })
}
const mutationFnContext = {
client: this.#client,
meta: this.options.meta,
mutationKey: this.options.mutationKey,
} satisfies MutationFunctionContext
const retryer = (this.#retryer = createRetryer({
fn: () => {
if (!this.options.mutationFn) {
return Promise.reject(new Error('No mutationFn found'))
}
return this.options.mutationFn(variables, mutationFnContext)
},
onFail: (failureCount, error) => {
this.#dispatch({ type: 'failed', failureCount, error })
},
onPause: () => {
this.#dispatch({ type: 'pause' })
},
onContinue,
retry: this.options.retry ?? 0,
retryDelay: this.options.retryDelay,
networkMode: this.options.networkMode,
canRun: () => this.#mutationCache.canRun(this),
}))
const restored = this.state.status === 'pending'
const isPaused = !retryer.canStart()
try {
if (restored) {
// Dispatch continue action to unpause restored mutation
onContinue()
} else {
this.#dispatch({ type: 'pending', variables, isPaused })
// Notify cache callback
if (this.#mutationCache.config.onMutate) {
await this.#mutationCache.config.onMutate(
variables,
this as Mutation<unknown, unknown, unknown, unknown>,
mutationFnContext,
)
}
const context = await this.options.onMutate?.(
variables,
mutationFnContext,
)
if (context !== this.state.context) {
this.#dispatch({
type: 'pending',
context,
variables,
isPaused,
})
}
}
const data = await retryer.start()
// Notify cache callback
await this.#mutationCache.config.onSuccess?.(
data,
variables,
this.state.context,
this as Mutation<unknown, unknown, unknown, unknown>,
mutationFnContext,
)
await this.options.onSuccess?.(
data,
variables,
this.state.context!,
mutationFnContext,
)
// Notify cache callback
await this.#mutationCache.config.onSettled?.(
data,
null,
this.state.variables,
this.state.context,
this as Mutation<unknown, unknown, unknown, unknown>,
mutationFnContext,
)
await this.options.onSettled?.(
data,
null,
variables,
this.state.context,
mutationFnContext,
)
this.#dispatch({ type: 'success', data })
return data
} catch (error) {
try {
// Notify cache callback
await this.#mutationCache.config.onError?.(
error as any,
variables,
this.state.context,
this as Mutation<unknown, unknown, unknown, unknown>,
mutationFnContext,
)
} catch (e) {
void Promise.reject(e)
}
try {
await this.options.onError?.(
error as TError,
variables,
this.state.context,
mutationFnContext,
)
} catch (e) {
void Promise.reject(e)
}
try {
// Notify cache callback
await this.#mutationCache.config.onSettled?.(
undefined,
error as any,
this.state.variables,
this.state.context,
this as Mutation<unknown, unknown, unknown, unknown>,
mutationFnContext,
)
} catch (e) {
void Promise.reject(e)
}
try {
await this.options.onSettled?.(
undefined,
error as TError,
variables,
this.state.context,
mutationFnContext,
)
} catch (e) {
void Promise.reject(e)
}
this.#dispatch({ type: 'error', error: error as TError })
throw error
} finally {
// The settled retryer's promise would otherwise pin this mutation's
// result, variables and context for as long as the cache keeps it
if (this.#retryer === retryer) {
this.#retryer = undefined
}
this.#mutationCache.runNext(this)
}
}
#dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {
const reducer = (
state: MutationState<TData, TError, TVariables, TOnMutateResult>,
): MutationState<TData, TError, TVariables, TOnMutateResult> => {
switch (action.type) {
case 'failed':
return {
...state,
failureCount: action.failureCount,
failureReason: action.error,
}
case 'pause':
return {
...state,
isPaused: true,
}
case 'continue':
return {
...state,
isPaused: false,
}
case 'pending':
return {
...state,
context: action.context,
data: undefined,
failureCount: 0,
failureReason: null,
error: null,
isPaused: action.isPaused,
status: 'pending',
variables: action.variables,
submittedAt: Date.now(),
}
case 'success':
return {
...state,
data: action.data,
failureCount: 0,
failureReason: null,
error: null,
status: 'success',
isPaused: false,
}
case 'error':
return {
...state,
data: undefined,
error: action.error,
failureCount: state.failureCount + 1,
failureReason: action.error,
isPaused: false,
status: 'error',
}
}
}
this.state = reducer(this.state)
notifyManager.batch(() => {
this.#observers.forEach((observer) => {
observer.onMutationUpdate(action)
})
this.#mutationCache.notify({
mutation: this,
type: 'updated',
action,
})
})
}
}
/**
* Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.
* @returns The initial mutation state.
*/
export function getDefaultState<
TData,
TError,
TVariables,
TOnMutateResult,
>(): MutationState<TData, TError, TVariables, TOnMutateResult> {
return {
context: undefined,
data: undefined,
error: null,
failureCount: 0,
failureReason: null,
isPaused: false,
status: 'idle',
variables: undefined,
submittedAt: 0,
}
}