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

split up middleware, add OptionalPromise, add cache entry lifecycle #1034

Merged
merged 21 commits into from
May 13, 2021

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented May 1, 2021

Background on the "cache entry lifecycle": rtk-incubator/rtk-query#215

@phryneas phryneas changed the base branch from master to feature/v1.6-integration May 1, 2021 15:18
@netlify
Copy link

netlify bot commented May 1, 2021

Deploy preview for redux-starter-kit-docs ready!

Built with commit 2b7c65b

https://deploy-preview-1034--redux-starter-kit-docs.netlify.app

@phryneas phryneas force-pushed the feature/lifecycles branch 2 times, most recently from b64b365 to 8bcd7f5 Compare May 1, 2021 15:31
@codesandbox-ci
Copy link

codesandbox-ci bot commented May 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 2b7c65b:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration

@phryneas
Copy link
Member Author

phryneas commented May 1, 2021

what's still left todo here code-wise is removing the existing endpoint lifecycle onStart/etc.

@@ -40,11 +40,14 @@ export const build: SubMiddlewareBuilder = ({
originalArgs
)

const extra = mwApi.dispatch((_, __, extra) => extra)
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

Genius, isn't it? :D

I was like "the old API had it, so we mayybe need it here too?" and then "how on earth do I get that?". Yup.

@phryneas
Copy link
Member Author

phryneas commented May 6, 2021

Btw, Bikeshedding:

we need a discussion if onCacheEntryAdded and onQuery are appropriate names

@phryneas
Copy link
Member Author

phryneas commented May 6, 2021

And internal TODO:

I'm thinking of adding a updateCacheEntry (a pre-bound version of updateQueryResult) as an addition to e2e1fbc#diff-37992e5ad65f35286d92ad23730058800d8a872da260317ff5e9459aa03433a8R17-R41

Also, maybe adding a undo property on the thunk return value of updateQueryResult so that usually, patchQueryResult doesn't need to be dispatched by hand

queryThunk,
mutationThunk,
}) => {
type CacheLifecycle = {

Choose a reason for hiding this comment

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

Can this be moved outside, or is there a reason it's declared here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's only used in a locally scoped variable in the next line and once more to declare a value to add to that variable.

Moving it outside would raise questions like "should this be exported" etc, just keeping it here just circumvents all that and will also 100% remove it on transpilation.

Choose a reason for hiding this comment

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

Cool. Tx for the explanation.

@@ -277,6 +277,26 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
}
x.providesTags ??= x.provides
}
if (x.onStart || x.onSuccess || x.onError) {

Choose a reason for hiding this comment

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

Nice, glad u added a message here!

add `updateCacheEntry` to `QueryLifecycleApi`,
add `.undo` to `updateCacheValue` return type
re-scope middleware variables
tests
Comment on lines 38 to 46
}),
onStart({ id, ...patch }, { dispatch, context }) {
context.undoPost = dispatch(
async onQuery({ id, ...patch }, { dispatch }, { resultPromise }) {
const { undo } = dispatch(
api.util.updateQueryResult('post', id, (draft) => {
Object.assign(draft, patch)
})
).inversePatches
},
onError({ id }, { dispatch, context }) {
dispatch(api.util.patchQueryResult('post', id, context.undoPost))
)
resultPromise.catch(undo)
},
Copy link
Member Author

Choose a reason for hiding this comment

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

It's way too late, but this diff is kinda worth it

@markerikson
Copy link
Collaborator

Since compressed-size-bot doesn't have permissions, here's the size diff:

Size Change: +24.6 kB (+5%) 🔍

Total Size: 533 kB

Filename Size Change
dist/query/react/rtk-query-react.cjs.development.js 17.6 kB +1.81 kB (+11%) ⚠️
dist/query/react/rtk-query-react.cjs.production.min.js 11.1 kB +1.19 kB (+12%) ⚠️
dist/query/react/rtk-query-react.esm.js 17.2 kB +1.81 kB (+12%) ⚠️
dist/query/react/rtk-query-react.modern.development.js 14.1 kB +1.63 kB (+13%) ⚠️
dist/query/react/rtk-query-react.modern.js 14.2 kB +1.62 kB (+13%) ⚠️
dist/query/react/rtk-query-react.modern.production.min.js 9.28 kB +1.11 kB (+14%) ⚠️
dist/query/react/rtk-query-react.umd.js 187 kB +1.84 kB (+1%)
dist/query/react/rtk-query-react.umd.min.js 62.8 kB +1.17 kB (+2%)
dist/query/rtk-query.cjs.development.js 14.9 kB +1.86 kB (+14%) ⚠️
dist/query/rtk-query.cjs.production.min.js 9.44 kB +1.17 kB (+14%) ⚠️
dist/query/rtk-query.esm.js 14.6 kB +1.87 kB (+15%) ⚠️
dist/query/rtk-query.modern.development.js 11.8 kB +1.61 kB (+16%) ⚠️
dist/query/rtk-query.modern.js 11.9 kB +1.64 kB (+16%) ⚠️
dist/query/rtk-query.modern.production.min.js 7.74 kB +1.09 kB (+16%) ⚠️
dist/query/rtk-query.umd.js 27.4 kB +1.94 kB (+8%) 🔍
dist/query/rtk-query.umd.min.js 16.1 kB +1.24 kB (+8%) 🔍

@phryneas phryneas marked this pull request as ready for review May 13, 2021 11:32
@phryneas phryneas merged commit a200459 into reduxjs:feature/v1.6-integration May 13, 2021
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.

3 participants