Skip to content

Commit 03e8c39

Browse files
fix(query-core): start queued scoped mutations while the window is not focused 🤖🤖🤖 (#11679)
fix(query-core): start queued scoped mutations while the window is not focused A mutation queued behind another one in the same scope is started later through retryer.continue(), which only resumes when canContinue() is true. canContinue() requires the window to be focused, so with an unfocused tab the next mutation stayed paused until the window got focus again. Resume the initial start pause with canStart() instead, which is the same check used to start the retryer right away. Retry pauses still use canContinue(). Fixes #11642 Co-authored-by: Dominik Dorfmeister 🔮 <[email protected]>
1 parent 4bb041f commit 03e8c39

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

‎.changeset/quiet-scopes-resume.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
fix(query-core): start queued scoped mutations when the window is not focused

‎packages/query-core/src/__tests__/mutation.test.tsx‎

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { queryKey, sleep } from '@tanstack/query-test-utils'
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3-
import { MutationCache, QueryClient } from '..'
3+
import { MutationCache, QueryClient, focusManager } from '..'
44
import { MutationObserver } from '../mutationObserver'
55
import { executeMutation } from './utils'
66
import type { MutationState } from '../mutation'
@@ -466,6 +466,69 @@ describe('mutation', () => {
466466
'finish-B',
467467
])
468468
})
469+
470+
it('should start the next mutation in the same scope while the window is not focused', async () => {
471+
const key1 = queryKey()
472+
const key2 = queryKey()
473+
474+
const results: Array<string> = []
475+
476+
focusManager.setFocused(false)
477+
478+
try {
479+
executeMutation(
480+
queryClient,
481+
{
482+
mutationKey: key1,
483+
scope: {
484+
id: 'scope',
485+
},
486+
networkMode: 'always',
487+
mutationFn: async () => {
488+
results.push('start-A')
489+
await sleep(10)
490+
results.push('finish-A')
491+
return 'a'
492+
},
493+
},
494+
'vars1',
495+
)
496+
497+
executeMutation(
498+
queryClient,
499+
{
500+
mutationKey: key2,
501+
scope: {
502+
id: 'scope',
503+
},
504+
networkMode: 'always',
505+
mutationFn: async () => {
506+
results.push('start-B')
507+
await sleep(10)
508+
results.push('finish-B')
509+
return 'b'
510+
},
511+
},
512+
'vars2',
513+
)
514+
515+
await vi.advanceTimersByTimeAsync(20)
516+
expect(results).toStrictEqual([
517+
'start-A',
518+
'finish-A',
519+
'start-B',
520+
'finish-B',
521+
])
522+
expect(
523+
queryClient.getMutationCache().find({ mutationKey: key2 })?.state,
524+
).toMatchObject({
525+
status: 'success',
526+
isPaused: false,
527+
})
528+
} finally {
529+
focusManager.setFocused(undefined)
530+
}
531+
})
469532
})
470533

471534
it('should run mutations without scope in parallel', async () => {

‎packages/query-core/src/retryer.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
173173
}
174174
}
175175

176-
const pause = () => {
176+
const pause = (canResume: () => boolean) => {
177177
return new Promise((continueResolve) => {
178178
continueFn = (value) => {
179-
if (isResolved() || canContinue()) {
179+
if (isResolved() || canResume()) {
180180
continueResolve(value)
181181
}
182182
}
@@ -244,7 +244,7 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
244244
sleep(delay)
245245
// Pause if the document is not visible or when the device is offline
246246
.then(() => {
247-
return canContinue() ? undefined : pause()
247+
return canContinue() ? undefined : pause(canContinue)
248248
})
249249
.then(() => {
250250
if (isRetryCancelled) {
@@ -272,7 +272,9 @@ export function createRetryer<TData = unknown, TError = DefaultError>(
272272
if (canStart()) {
273273
run()
274274
} else {
275-
pause().then(run)
275+
// Waiting to start (e.g. queued behind a scope) does not depend on focus,
276+
// only on the same conditions that allow starting in the first place
277+
pause(canStart).then(run)
276278
}
277279
return promise
278280
},

0 commit comments

Comments
 (0)