From 3ed8e4f4b0c4c2fe10c7437a136745bd8ea0f291 Mon Sep 17 00:00:00 2001 From: Dominik Dorfmeister Date: Sun, 23 Jan 2022 18:14:38 +0100 Subject: [PATCH] refactor(mutation): remove mutation.cancel as it wasn't really aborting the request - there is no AbortSignal for Mutations atm. --- .../guides/migrating-to-react-query-4.md | 15 +++-------- src/core/mutation.ts | 9 ------- src/core/mutationCache.ts | 1 - src/core/tests/mutations.test.tsx | 27 ------------------- 4 files changed, 3 insertions(+), 49 deletions(-) diff --git a/docs/src/pages/guides/migrating-to-react-query-4.md b/docs/src/pages/guides/migrating-to-react-query-4.md index a2ac3d02ef..4856dbbeb6 100644 --- a/docs/src/pages/guides/migrating-to-react-query-4.md +++ b/docs/src/pages/guides/migrating-to-react-query-4.md @@ -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 { -- 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< @@ -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 diff --git a/src/core/mutation.ts b/src/core/mutation.ts index cc0f1e80d5..6d303b91bc 100644 --- a/src/core/mutation.ts +++ b/src/core/mutation.ts @@ -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 @@ -150,14 +149,6 @@ export class Mutation< } } - cancel(): Promise { - if (this.retryer) { - this.retryer.cancel() - return this.retryer.promise.then(noop).catch(noop) - } - return Promise.resolve() - } - continue(): Promise { if (this.retryer) { this.retryer.continue() diff --git a/src/core/mutationCache.ts b/src/core/mutationCache.ts index be44e44b7b..a101013d3f 100644 --- a/src/core/mutationCache.ts +++ b/src/core/mutationCache.ts @@ -106,7 +106,6 @@ export class MutationCache extends Subscribable { remove(mutation: Mutation): void { this.mutations = this.mutations.filter(x => x !== mutation) - mutation.cancel() this.notify({ type: 'removed', mutation }) } diff --git a/src/core/tests/mutations.test.tsx b/src/core/tests/mutations.test.tsx index 14260c38db..8f624ad64e 100644 --- a/src/core/tests/mutations.test.tsx +++ b/src/core/tests/mutations.test.tsx @@ -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'],