Skip to content
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

Merged
merged 1 commit into from
Jul 9, 2021

Conversation

aulneau
Copy link
Collaborator

@aulneau aulneau commented Jul 7, 2021

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).

@aulneau aulneau requested a review from dai-shi July 7, 2021 20:16
@vercel
Copy link

vercel bot commented Jul 7, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pmndrs/jotai/BqeKtpj9GPgSHtB2quKjnAU15tn1
✅ Preview: https://jotai-git-fix-ignore-cancel-error-pmndrs.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 7, 2021

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:

Sandbox Source
React Configuration
React Typescript Configuration

Copy link
Member

@dai-shi dai-shi left a 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?

Comment on lines 96 to 97
if (prevData) settlePromise(prevData)
settlePromise = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (prevData) settlePromise(prevData)
settlePromise = null
if (prevData) {
settlePromise(prevData)
settlePromise = null
}

settlePromise should be cleared only if it's invoked.

Copy link
Member

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?

Copy link
Collaborator Author

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)

Copy link
Member

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.

src/query/atomWithInfiniteQuery.ts Outdated Show resolved Hide resolved
@aulneau
Copy link
Collaborator Author

aulneau commented Jul 8, 2021

Is it the issue with atomWithQuery too?

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.

@aulneau aulneau requested a review from dai-shi July 8, 2021 00:55
Copy link
Member

@dai-shi dai-shi left a 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.

@dai-shi dai-shi merged commit 6dc98f5 into master Jul 9, 2021
@dai-shi dai-shi deleted the fix/ignore-cancel-error branch July 9, 2021 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants