Skip to content

Commit 255f7c6

Browse files
authored
feat(queryClient): respect cancelRefetch flag (#2798)
1 parent 35b3836 commit 255f7c6

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

docs/src/pages/reference/QueryClient.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ await queryClient.invalidateQueries('posts', {
276276
exact,
277277
refetchActive: true,
278278
refetchInactive: false
279-
}, { throwOnError })
279+
}, { throwOnError, cancelRefetch })
280280
```
281281

282282
**Options**
@@ -292,9 +292,11 @@ await queryClient.invalidateQueries('posts', {
292292
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
293293
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
294294
- Use this function to specify which pages should be refetched
295-
- `refetchOptions?: RefetchOptions`:
295+
- `options?: InvalidateOptions`:
296296
- `throwOnError?: boolean`
297297
- When set to `true`, this method will throw if any of the query refetch tasks fail.
298+
- cancelRefetch?: boolean
299+
- When set to `true`, then the current request will be cancelled before a new request is made
298300

299301
## `queryClient.refetchQueries`
300302

@@ -323,9 +325,11 @@ await queryClient.refetchQueries(['posts', 1], { active: true, exact: true })
323325
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
324326
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
325327
- Use this function to specify which pages should be refetched
326-
- `refetchOptions?: RefetchOptions`:
328+
- `options?: RefetchOptions`:
327329
- `throwOnError?: boolean`
328330
- When set to `true`, this method will throw if any of the query refetch tasks fail.
331+
- cancelRefetch?: boolean
332+
- When set to `true`, then the current request will be cancelled before a new request is made
329333

330334
**Returns**
331335

@@ -389,9 +393,11 @@ queryClient.resetQueries(queryKey, { exact: true })
389393
- `refetchPage: (page: TData, index: number, allPages: TData[]) => boolean`
390394
- Only for [Infinite Queries](../guides/infinite-queries#refetchpage)
391395
- Use this function to specify which pages should be refetched
392-
- `resetOptions?: ResetOptions`:
396+
- `options?: ResetOptions`:
393397
- `throwOnError?: boolean`
394398
- When set to `true`, this method will throw if any of the query refetch tasks fail.
399+
- cancelRefetch?: boolean
400+
- When set to `true`, then the current request will be cancelled before a new request is made
395401

396402
**Returns**
397403

src/core/queryClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ export class QueryClient {
289289
const promises = notifyManager.batch(() =>
290290
this.queryCache.findAll(filters).map(query =>
291291
query.fetch(undefined, {
292+
...options,
292293
meta: { refetchPage: filters?.refetchPage },
293294
})
294295
)

src/core/tests/queryClient.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,30 @@ describe('queryClient', () => {
847847
expect(queryFn1).toHaveBeenCalledTimes(1)
848848
expect(queryFn2).toHaveBeenCalledTimes(1)
849849
})
850+
851+
test('should cancel ongoing fetches if cancelRefetch option is passed', async () => {
852+
const key = queryKey()
853+
const cancelFn = jest.fn()
854+
const observer = new QueryObserver(queryClient, {
855+
queryKey: key,
856+
enabled: false,
857+
initialData: 1,
858+
})
859+
observer.subscribe()
860+
861+
queryClient.fetchQuery(key, () => {
862+
const promise = new Promise(resolve => {
863+
setTimeout(() => resolve(5), 10)
864+
})
865+
// @ts-expect-error
866+
promise.cancel = cancelFn
867+
return promise
868+
})
869+
870+
await queryClient.refetchQueries(undefined, { cancelRefetch: true })
871+
observer.destroy()
872+
expect(cancelFn).toHaveBeenCalledTimes(1)
873+
})
850874
})
851875

852876
describe('resetQueries', () => {

src/core/types.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,8 @@ export interface ResetQueryFilters<TPageData = unknown>
273273
extends QueryFilters,
274274
RefetchPageFilters<TPageData> {}
275275

276-
export interface InvalidateOptions {
277-
throwOnError?: boolean
278-
}
279-
280-
export interface ResetOptions {
281-
throwOnError?: boolean
282-
}
276+
export interface InvalidateOptions extends RefetchOptions {}
277+
export interface ResetOptions extends RefetchOptions {}
283278

284279
export interface FetchNextPageOptions extends ResultOptions {
285280
cancelRefetch?: boolean

0 commit comments

Comments
 (0)