-
-
Notifications
You must be signed in to change notification settings - Fork 642
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
fix(query): gracefully handle CancelledError #582
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/pmndrs/jotai/BqeKtpj9GPgSHtB2quKjnAU15tn1 |
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 6ccdf01:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't aware about the cancellation of promise. I think it's better if we can simply ignore it.
Is it the issue with atomWithQuery too?
src/query/atomWithInfiniteQuery.ts
Outdated
if (prevData) settlePromise(prevData) | ||
settlePromise = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (prevData) settlePromise(prevData) | |
settlePromise = null | |
if (prevData) { | |
settlePromise(prevData) | |
settlePromise = null | |
} |
settlePromise should be cleared only if it's invoked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolving with prevData is required for refetch? hm but this file is atomWithInfiniteQuery, not atomWithQuery.
I feel this a bit weird because settlePromise and setData are always paired.
Can we do
if (result.error && !isCancelledError(result.error)) {
in L91?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that works too, wasn't sure if we needed to do the settledPromise stuff (tbh not clear on what it does exactly lol)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know. This is because of very tricky suspense behavior. I'm not even 100% sure, so adding tests to check the behavior would be important.
e43a6c2
to
e5b96c0
Compare
e5b96c0
to
6ccdf01
Compare
I have not run into it, and looking at the source code of react query, it seems that there isn't this same kind of CancelledError usage in a non-infinite query. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. This looks just fine.
So this was a strange bug that I came upon. It would happen where I had an
atomWithInfiniteQuery
already loaded, and then navigated to a page that had that same query. It seems there is some default cancellation behavior in the way infiniteQueries work in react query.I think this solution makes sense, and it fixes my bug (without this, the application breaks).