Skip to content

Commit 52ad9cc

Browse files
authored
refactor(mutation): remove mutation.cancel (#3225)
as it wasn't really aborting the request - there is no AbortSignal for Mutations atm.
1 parent c0fc916 commit 52ad9cc

File tree

4 files changed

+3
-49
lines changed

4 files changed

+3
-49
lines changed

docs/src/pages/guides/migrating-to-react-query-4.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,9 @@ The `useQueries` hook now accepts an object with a `queries` prop as its input.
217217
```
218218

219219

220-
### Removed undocumented methods from the `queryClient` and `query`
220+
### Removed undocumented methods from the `queryClient`, `query` and `mutation`
221221

222-
The methods `cancelMutatations` and `executeMutation` on the `QueryClient` were undocumented and unused internally, so we removed them. Since they were just wrappers around methods available on the `mutationCache`, you can still use the functionality.
223-
224-
```diff
225-
- cancelMutations(): Promise<void> {
226-
- const promises = notifyManager.batch(() =>
227-
- this.mutationCache.getAll().map(mutation => mutation.cancel())
228-
- )
229-
- return Promise.all(promises).then(noop).catch(noop)
230-
- }
231-
```
222+
The methods `cancelMutatations` and `executeMutation` on the `QueryClient` were undocumented and unused internally, so we removed them. Since it was just a wrapper around a method available on the `mutationCache`, you can still use the functionality of `executeMutation`
232223

233224
```diff
234225
- executeMutation<
@@ -243,7 +234,7 @@ The methods `cancelMutatations` and `executeMutation` on the `QueryClient` were
243234
- }
244235
```
245236

246-
Additionally, `query.setDefaultOptions` was removed because it was also unused.
237+
Additionally, `query.setDefaultOptions` was removed because it was also unused. `mutation.cancel` was removed because it didn't actually cancel the outgoing request.
247238

248239
### TypeScript
249240

src/core/mutation.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getLogger } from './logger'
55
import { notifyManager } from './notifyManager'
66
import { Removable } from './removable'
77
import { canFetch, Retryer, createRetryer } from './retryer'
8-
import { noop } from './utils'
98

109
// TYPES
1110

@@ -150,14 +149,6 @@ export class Mutation<
150149
}
151150
}
152151

153-
cancel(): Promise<void> {
154-
if (this.retryer) {
155-
this.retryer.cancel()
156-
return this.retryer.promise.then(noop).catch(noop)
157-
}
158-
return Promise.resolve()
159-
}
160-
161152
continue(): Promise<TData> {
162153
if (this.retryer) {
163154
this.retryer.continue()

src/core/mutationCache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export class MutationCache extends Subscribable<MutationCacheListener> {
106106

107107
remove(mutation: Mutation<any, any, any, any>): void {
108108
this.mutations = this.mutations.filter(x => x !== mutation)
109-
mutation.cancel()
110109
this.notify({ type: 'removed', mutation })
111110
}
112111

src/core/tests/mutations.test.tsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -369,33 +369,6 @@ describe('mutations', () => {
369369
consoleMock.mockRestore()
370370
})
371371

372-
test('cancel mutation should not call mutationFn if the current retrier is undefined', async () => {
373-
const mutationFn = jest.fn().mockImplementation(async () => {
374-
await sleep(20)
375-
return 'data'
376-
})
377-
378-
const observer = new MutationObserver(queryClient, {
379-
mutationKey: ['key'],
380-
mutationFn,
381-
})
382-
383-
observer.mutate()
384-
const mutation = queryClient
385-
.getMutationCache()
386-
.find({ mutationKey: ['key'] })!
387-
await sleep(10)
388-
389-
// Force current mutation retryer to be undefined
390-
// because not use case has been found
391-
mutation['retryer'] = undefined
392-
mutationFn.mockReset()
393-
await mutation.cancel()
394-
395-
await sleep(30)
396-
expect(mutationFn).toHaveBeenCalledTimes(0)
397-
})
398-
399372
test('reducer should return the state for an unknown action type', async () => {
400373
const observer = new MutationObserver(queryClient, {
401374
mutationKey: ['key'],

0 commit comments

Comments
 (0)