Skip to content

Commit 02c3d51

Browse files
fix(query-core): unpause restored mutations (TanStack#9015)
* fix(query-core): unpause restored mutations ensure that restored mutations have their isPaused state set to false when continued. * Update packages/query-core/src/__tests__/mutations.test.tsx --------- Co-authored-by: Dominik Dorfmeister <[email protected]>
1 parent 727230f commit 02c3d51

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ describe('mutations', () => {
252252
const onSettled = vi.fn()
253253

254254
queryClient.setMutationDefaults(key, {
255-
mutationFn: (text: string) => Promise.resolve(text),
255+
mutationFn: (text: string) => sleep(10).then(() => text),
256256
onMutate,
257257
onSuccess,
258258
onSettled,
@@ -290,7 +290,23 @@ describe('mutations', () => {
290290
submittedAt: 1,
291291
})
292292

293-
await queryClient.resumePausedMutations()
293+
void queryClient.resumePausedMutations()
294+
await vi.advanceTimersByTimeAsync(0)
295+
296+
// check that the mutation is correctly resumed
297+
expect(mutation.state).toEqual({
298+
context: 'todo',
299+
data: undefined,
300+
error: null,
301+
failureCount: 1,
302+
failureReason: 'err',
303+
isPaused: false,
304+
status: 'pending',
305+
variables: 'todo',
306+
submittedAt: 1,
307+
})
308+
309+
await vi.advanceTimersByTimeAsync(20)
294310

295311
expect(mutation.state).toEqual({
296312
context: 'todo',

packages/query-core/src/mutation.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ export class Mutation<
162162
}
163163

164164
async execute(variables: TVariables): Promise<TData> {
165+
const onContinue = () => {
166+
this.#dispatch({ type: 'continue' })
167+
}
168+
165169
this.#retryer = createRetryer({
166170
fn: () => {
167171
if (!this.options.mutationFn) {
@@ -175,9 +179,7 @@ export class Mutation<
175179
onPause: () => {
176180
this.#dispatch({ type: 'pause' })
177181
},
178-
onContinue: () => {
179-
this.#dispatch({ type: 'continue' })
180-
},
182+
onContinue,
181183
retry: this.options.retry ?? 0,
182184
retryDelay: this.options.retryDelay,
183185
networkMode: this.options.networkMode,
@@ -188,7 +190,10 @@ export class Mutation<
188190
const isPaused = !this.#retryer.canStart()
189191

190192
try {
191-
if (!restored) {
193+
if (restored) {
194+
// Dispatch continue action to unpause restored mutation
195+
onContinue()
196+
} else {
192197
this.#dispatch({ type: 'pending', variables, isPaused })
193198
// Notify cache callback
194199
await this.#mutationCache.config.onMutate?.(

0 commit comments

Comments
 (0)