Repository navigation
feat(cancellation): Provide AbortSignal to queryFn - #2782
Conversation
All queryFn calls will now have an AbortSignal instance passed to them
as part of the QueryFunctionContext. In environments that support the
AbortController/AbortSignal the consumer may use this `signal` as a
way to get notified about the query being cancelled. Consumers no
longer need to add a `.cancel()` function to the result of `queryFn()`
```ts
const query = useQuery('todos', async ({ signal }) => {
const res = await fetch('/todos', {
method: 'get',
// Pass the signal to your request
signal,
});
const json = await res.json();
let data = null;
if (!signal.aborted) {
// do expensive CPU transformation of data
data = expensiveTransform(json);
}
return data;
});
```
The previous behavior for cancellation will continue to work. If a
`.cancel()` function is provided as the result of calling `queryFn()`
the AbortSignal will not be notified when the cancellation happens.
If the runtime environment does not support AbortController/AbortSignal
the old behavior will continue to work, but the `signal` provided to
the `queryFn()` will not be notified when the cancellation happens.
Ref TanStack#1671 (comment)
Ref TanStack#1671 (comment)
Fix TanStack#2775
Fix TanStack#1893
Fix TanStack#1265
Fix TanStack#886
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tanstack/react-query/EP7LTfYpn1gxJSY8SYdAUyRN9RxD |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 16233b0:
|
|
Regarding the check failure below I thought the |
|
it looks like we only need |
|
@TkDodo I already fixed the issue with |
TkDodo
left a comment
There was a problem hiding this comment.
Thank you 🙏. I'm still missing:
- tests that actually use the provided
signal, for bothuseQueryanduseInfiniteQuery. - documentation of the feature, likely on this page: https://react-query.tanstack.com/guides/query-cancellation. we could separate it into the old approach and the new one, preferring the new one, and also stating why that is. You've already shown some usage examples in the discussion, which we could copy over I guess?
TkDodo
left a comment
There was a problem hiding this comment.
I think this is great work, thank you 🙏 . It will be exciting to ship this
|
Despite the tests passing I'm fairly sure that this will fail in old browsers that since the |
very old browsers :) WeakMap has pretty good compatibility: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap#browser_compatibility Nevertheless, I now think not providing a signal at all is better as its less code. |
|
Okay. Huge change. I've switched entirely away from trying to provide a minimal interface and just made the |
TkDodo
left a comment
There was a problem hiding this comment.
wow, this looks really slick now 🙌
|
sorry for the conflicts - we had another PR that added something to the queryFunctionContext. I think it means you'll also need to adapt the test to account for that extra field |
|
@TkDodo merged |
|
🎉 This PR is included in version 3.30.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
Thank you for this great contribution @jcready . I've announced it on twitter: https://twitter.com/TkDodo/status/1454521789938294787 please let me know if you have a twitter handle so I can add you to the shoutout: https://twitter.com/TkDodo/status/1454521795529355265 |
|
Thank you @TkDodo! It was a pleasure working with you on this. I don't have Twitter, but I appreciate the link to my GH. |
|
I don't really understand, the |
|
@caub queries will be cancelled automatically by RQ in some situations, like when the query goes to inactive state (=you’re not using it anymore) and there is a fetch pending. For manual cancellation, see the docs link above |
|
Ah ok thanks guys, so in all cases, whether we plan to cancel queries manually or not, it's better to pass down |
|
@caub that is debatable :) though personally I will be using the signal in every query function. One tradeoff is that for any query that is cancelled where either a
|
|
@TkDodo It might be helpful to mention the version number after which this feature is available in the docs. I was getting type errors with the destructured signal argument, and it took a bit of digging to realize this is a brand new feature (I was on 3.27.0). I would submit a PR with the update, but am not sure I have enough context to get the details right. Just a thought. Thanks for the great feature, @jcready -- very cool. |
|
@telekstr good idea. The version where we introduced it is 3.30.0: https://github.com/tannerlinsley/react-query/releases/tag/v3.30.0 Please contribute it to the Query cancellation docs page, thank you. |
Sure thing, here you go: #2887 |
All queryFn calls will now have an AbortSignal instance passed to them
as part of the QueryFunctionContext. In environments that support the
AbortController/AbortSignal the consumer may use this
signalas away to get notified about the query being cancelled. Consumers no
longer need to add a
.cancel()function to the result ofqueryFn()The previous behavior for cancellation will continue to work. If a
.cancel()function is provided as the result of callingqueryFn()the AbortSignal will not be notified when the cancellation happens.
If the runtime environment does not support AbortController/AbortSignal
the old behavior will continue to work, but the
signalprovided tothe
queryFn()will not be notified when the cancellation happens.Ref #1671 (comment)
Ref #1671 (comment)
Fix #2775
Fix #1893
Fix #1265
Fix #886