Skip to content

refactor(mutation): remove mutation.cancel #3225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions docs/src/pages/guides/migrating-to-react-query-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,9 @@ The `useQueries` hook now accepts an object with a `queries` prop as its input.
```


### Removed undocumented methods from the `queryClient` and `query`
### Removed undocumented methods from the `queryClient`, `query` and `mutation`

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.

```diff
- cancelMutations(): Promise<void> {
- const promises = notifyManager.batch(() =>
- this.mutationCache.getAll().map(mutation => mutation.cancel())
- )
- return Promise.all(promises).then(noop).catch(noop)
- }
```
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`

```diff
- executeMutation<
Expand All @@ -243,7 +234,7 @@ The methods `cancelMutatations` and `executeMutation` on the `QueryClient` were
- }
```

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

### TypeScript

Expand Down
9 changes: 0 additions & 9 deletions src/core/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { getLogger } from './logger'
import { notifyManager } from './notifyManager'
import { Removable } from './removable'
import { canFetch, Retryer, createRetryer } from './retryer'
import { noop } from './utils'

// TYPES

Expand Down Expand Up @@ -150,14 +149,6 @@ export class Mutation<
}
}

cancel(): Promise<void> {
if (this.retryer) {
this.retryer.cancel()
return this.retryer.promise.then(noop).catch(noop)
}
return Promise.resolve()
}

continue(): Promise<TData> {
if (this.retryer) {
this.retryer.continue()
Expand Down
1 change: 0 additions & 1 deletion src/core/mutationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ export class MutationCache extends Subscribable<MutationCacheListener> {

remove(mutation: Mutation<any, any, any, any>): void {
this.mutations = this.mutations.filter(x => x !== mutation)
mutation.cancel()
this.notify({ type: 'removed', mutation })
}

Expand Down
27 changes: 0 additions & 27 deletions src/core/tests/mutations.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,33 +369,6 @@ describe('mutations', () => {
consoleMock.mockRestore()
})

test('cancel mutation should not call mutationFn if the current retrier is undefined', async () => {
const mutationFn = jest.fn().mockImplementation(async () => {
await sleep(20)
return 'data'
})

const observer = new MutationObserver(queryClient, {
mutationKey: ['key'],
mutationFn,
})

observer.mutate()
const mutation = queryClient
.getMutationCache()
.find({ mutationKey: ['key'] })!
await sleep(10)

// Force current mutation retryer to be undefined
// because not use case has been found
mutation['retryer'] = undefined
mutationFn.mockReset()
await mutation.cancel()

await sleep(30)
expect(mutationFn).toHaveBeenCalledTimes(0)
})

test('reducer should return the state for an unknown action type', async () => {
const observer = new MutationObserver(queryClient, {
mutationKey: ['key'],
Expand Down