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

Override unwrap behavior for buildInitiateQuery, update tests #1786

Merged
merged 6 commits into from
Dec 8, 2021

Conversation

msutkowski
Copy link
Member

@msutkowski msutkowski commented Dec 1, 2021

This is an attempt at solving the issue where if you execute multiple lazy queries and unwrap them, anything after the first would throw with a ConditionError as requests 2+ would be condition: false due to a request being in flight.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 1, 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 c8221fe:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration

@github-actions
Copy link

github-actions bot commented Dec 1, 2021

size-limit report 📦

Path Size
1. entry point: @reduxjs/toolkit (cjs.production.min.js) 12.93 KB (0%)
1. entry point: @reduxjs/toolkit (esm.js) 10.79 KB (0%)
1. entry point: @reduxjs/toolkit/query (cjs.production.min.js) 22.59 KB (+0.15% 🔺)
1. entry point: @reduxjs/toolkit/query (esm.js) 18.94 KB (+0.16% 🔺)
1. entry point: @reduxjs/toolkit/query/react (cjs.production.min.js) 24.83 KB (+0.13% 🔺)
1. entry point: @reduxjs/toolkit/query/react (esm.js) 21.63 KB (+0.16% 🔺)
2. entry point: @reduxjs/toolkit (without dependencies) (cjs.production.min.js) 5.71 KB (0%)
2. entry point: @reduxjs/toolkit (without dependencies) (esm.js) 5.67 KB (0%)
2. entry point: @reduxjs/toolkit/query (without dependencies) (cjs.production.min.js) 10.04 KB (+0.35% 🔺)
2. entry point: @reduxjs/toolkit/query (without dependencies) (esm.js) 10.47 KB (+0.33% 🔺)
2. entry point: @reduxjs/toolkit/query/react (without dependencies) (cjs.production.min.js) 2.8 KB (0%)
2. entry point: @reduxjs/toolkit/query/react (without dependencies) (esm.js) 3.21 KB (0%)
3. createSlice (esm.js) 5.03 KB (0%)
3. createEntityAdapter (esm.js) 6.28 KB (0%)
3. configureStore (esm.js) 5.65 KB (0%)
3. createApi (esm.js) 17.18 KB (+0.22% 🔺)
3. createApi (react) (esm.js) 19.89 KB (+0.23% 🔺)
3. fetchBaseQuery (esm.js) 11.61 KB (0%)
3. setupListeners (esm.js) 10.42 KB (0%)
3. ApiProvider (esm.js) 18.51 KB (+0.23% 🔺)

Comment on lines 296 to 307
subscriptionOptions,
queryCacheKey,
abort,
unwrap,
unwrap() {
return aggregatePromise.then<any>((result) => {
if (result.isError) {
throw result.error
}

return result.data
})
},
Copy link
Member Author

Choose a reason for hiding this comment

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

The only thing that I think that could be interesting (or confusing?) here is what happens regarding unwrap and abort. If abort is called, the initial request will be aborted, but a consumer will still be able to unwrap this. At the same time, I wouldn't want the abort to ignore the aggregate result, if that makes sense. I don't think there is any real issue with this implementation, but it could be confusing. I don't have any better ideas or solutions for this though.

Copy link
Member

Choose a reason for hiding this comment

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

.abort of the originally running promise should set the query into an error state, so I think this would result in an error here, which seems reasonable.

You bring up another problem though: .abort() on this would do nothing if it just piggy-backs on another promise that started earlier. But otoh, I guess that's okay, too - you would not want to abort something else by accident I guess?

Copy link
Member

Choose a reason for hiding this comment

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

Can we make this function async? The .then usage here irritates me for some reason 😅

packages/toolkit/src/query/core/buildInitiate.ts Outdated Show resolved Hide resolved
Comment on lines 296 to 307
subscriptionOptions,
queryCacheKey,
abort,
unwrap,
unwrap() {
return aggregatePromise.then<any>((result) => {
if (result.isError) {
throw result.error
}

return result.data
})
},
Copy link
Member

Choose a reason for hiding this comment

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

.abort of the originally running promise should set the query into an error state, so I think this would result in an error here, which seems reasonable.

You bring up another problem though: .abort() on this would do nothing if it just piggy-backs on another promise that started earlier. But otoh, I guess that's okay, too - you would not want to abort something else by accident I guess?

Comment on lines 296 to 307
subscriptionOptions,
queryCacheKey,
abort,
unwrap,
unwrap() {
return aggregatePromise.then<any>((result) => {
if (result.isError) {
throw result.error
}

return result.data
})
},
Copy link
Member

Choose a reason for hiding this comment

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

Can we make this function async? The .then usage here irritates me for some reason 😅

packages/toolkit/src/query/tests/buildHooks.test.tsx Outdated Show resolved Hide resolved
Co-authored-by: Lenz Weber <mail@lenzw.de>
@msutkowski msutkowski merged commit 4bb0c5c into v1.7.0-integration Dec 8, 2021
@msutkowski msutkowski deleted the fix-buildInitiateQuery-unwrap branch December 8, 2021 20:56
fireEvent.click(fetchButton) // This technically dispatches a ConditionError, but we don't want to see that here. We want the real result to resolve and ignore the error.

await waitFor(() => {
const dataResult = screen.getByTestId('error')?.textContent

Choose a reason for hiding this comment

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

should be

const dataResult = screen.getByTestId('result')?.textContent

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.

4 participants