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

feat: support network request aborting and refetching #34

Merged
merged 11 commits into from
Aug 15, 2019
Merged

Conversation

amcgee
Copy link
Member

@amcgee amcgee commented Aug 8, 2019

This automatically aborts requests when the component is unmounted, the query changed, or a refetch initiated (new feature 👍).

This also adds eslint-plugin-react-hooks to eslint, allowing us to better check hooks. **We should probably add these to cli-style!

This does not expose an imperative abort interface, though that could be added later if there's a need for it. As it stands, I think it's better to let the component lifecycle handle the inflight requests, especially since we might not always have 1-to-1 query-to-request relationships.

We were missing dependencies for the useEffect in useDataQuery, which I've added here, so now the data will be re-requested if the query changes.

@amcgee amcgee requested a review from varl August 8, 2019 17:55
@amcgee
Copy link
Member Author

amcgee commented Aug 8, 2019

@varl is there a reason we aren't using any eslint plugins (most notable eslint-plugin-react) in cli-style? Are we trying to keep it uber-generic?

@varl
Copy link
Contributor

varl commented Aug 9, 2019

@varl is there a reason we aren't using any eslint plugins (most notable eslint-plugin-react) in cli-style? Are we trying to keep it uber-generic?

Keeping it "JavaScript" generic:

The intended scope for d2-style is that JavaScript code standards should apply to all JavaScript code, not just React.

We could add a group for React though, so you get a different ESLint configuration by doing e.g. d2-style setup project/react.

The "base" ESLint should be kept agnostic and only enforce vanilla JavaScript standards.

@amcgee
Copy link
Member Author

amcgee commented Aug 13, 2019

Keeping it "JavaScript" generic

Cool cool, though so. I think having a React group might make sense, but for now I've just added some eslint React rules here.

@varl thoughts this change and particularly whether or not to include an imperative abort() option in the hook output?

@amcgee amcgee marked this pull request as ready for review August 13, 2019 11:01
@varl
Copy link
Contributor

varl commented Aug 13, 2019

Keeping it "JavaScript" generic

Cool cool, though so. I think having a React group might make sense, but for now I've just added some eslint React rules here.

Agreed. The reason we don't have it yet is this:

One thing to think about in that case is how to install the required dependencies.

Do we want those in cli-style (can lead to bloat if we support a lot of custom projects)? Maybe the tool will tell you want deps you need to satisfy in your project instead.

@varl
Copy link
Contributor

varl commented Aug 13, 2019

@varl thoughts this change and particularly whether or not to include an imperative abort() option in the hook output?

Generally it would be nice to not have to think about it, but there might be scenarios where we do need to manually abort a request. Unless we actually need it now, my vote goes to only aborting when the component unmounts for now and adding it when we have use case we want to support.

@amcgee
Copy link
Member Author

amcgee commented Aug 13, 2019

@varl thoughts this change and particularly whether or not to include an imperative abort() option in the hook output?

Generally it would be nice to not have to think about it, but there might be scenarios where we do need to manually abort a request. Unless we actually need it now, my vote goes to only aborting when the component unmounts for now and adding it when we have use case we want to support.

Cool, I think I agree. I'll leave it out for now, will be adding imperative refetch shortly which is probably more useful and has actually been requested

@amcgee
Copy link
Member Author

amcgee commented Aug 13, 2019

@varl updated to remove the imperative abort for now. Also refactored the hook implementation to be cleaner and prepare for refetch

varl
varl previously approved these changes Aug 13, 2019
Copy link
Contributor

@varl varl left a comment

Choose a reason for hiding this comment

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

Looks good except for the typo. Fix that and :shipit:.

@amcgee amcgee requested a review from varl August 13, 2019 16:45
@amcgee
Copy link
Member Author

amcgee commented Aug 13, 2019

There's a known issue with duplicate typings in react/react-dom with yarn, fixed by adding @types/**/node_modules to .yarnclean

@amcgee amcgee changed the title feat: support network request aborting feat: support network request aborting and refetching Aug 13, 2019
@amcgee amcgee dismissed varl’s stale review August 13, 2019 17:13

Added refetch feature

@amcgee
Copy link
Member Author

amcgee commented Aug 13, 2019

@varl thanks for the reviews! Fixed the typo.

I went ahead and added the refetch feature here so that we don't need a second minor version bump, so I dismissed your approval.

I added an imperative refetch interface, cleaned up several sections, added tests, and upgraded the devDep on React/React-Dom to 16.9 so we don't have warnings flooding test output when testing async logic with act. I've left the peerDep in the runtime package at ^16.8.6 though, which 16.9 satisfies.

Copy link
Contributor

@varl varl left a comment

Choose a reason for hiding this comment

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

LGTM

@amcgee
Copy link
Member Author

amcgee commented Aug 14, 2019

@HendrikThePendric made a good point about AbortController in IE11 - we'll either need to polyfill it (not difficult) or guarantee that this will only be used in 2.33+

@amcgee
Copy link
Member Author

amcgee commented Aug 14, 2019

(Note: IE 11 also doesn't have fetch, so we're presumably polyfilling that already in cases when this is used for pre-2.33 apps/libs like Headerbar? @varl?)

Copy link
Contributor

@HendrikThePendric HendrikThePendric left a comment

Choose a reason for hiding this comment

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

Left one comment reg. IE11 support

}, [])

return state
const controller = new AbortController()
Copy link
Contributor

@HendrikThePendric HendrikThePendric Aug 14, 2019

Choose a reason for hiding this comment

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

IE11 lacks support for the AbortController. From DHIS 2 version 2.33 we will drop support for IE 11, so most apps using this hook won't suffer from this problem. However, there may be some exceptions:

  • Apps that transitioned to feature toggling prior to 2.33: if at a later stage a developer decides to introduce the DataQuery component, this could cause problems (they can probably be avoided somehow)
  • The new HeaderBar component: I believe this uses the DataQuery too, and it is being used in a lot of v2.32 apps. So this could be another potential source of problems.

My preferred solution would be to do the following:

  • Keep the code as it is
  • Add a section to the README (preferably fairly prominently displayed) mentioning:
    • That this lib relies on the Fetch, AbortController and AbortSignal APIs which are unsupported in IE11
    • If your app requires IE11 support, add a polyfill for all these APIs
    • Include some instructions on how to do so, i.e. linking to whatwg-fetch and abortcontroller-polyfill

This way each app can handle things as it sees fit.

@varl
Copy link
Contributor

varl commented Aug 14, 2019

@HendrikThePendric @amcgee We use a similar approach in ui-core where the App needs to provide polyfills: https://github.com/dhis2/ui-core#requires-polyfills

@amcgee
Copy link
Member Author

amcgee commented Aug 14, 2019

Great thanks @varl @HendrikThePendric! I’ll update the readme and merge tomorrow

Would be SO COOL to formally expose lib poly fill reqs to consuming apps! One day...

@amcgee
Copy link
Member Author

amcgee commented Aug 15, 2019

Merging abort and refetch for a 1.5 release!

@amcgee amcgee merged commit dcb4a70 into master Aug 15, 2019
dhis2-bot pushed a commit that referenced this pull request Aug 15, 2019
# [1.5.0](v1.4.3...v1.5.0) (2019-08-15)

### Bug Fixes

* don't silently ignore test failures, fix data reduce bug ([95fd038](95fd038))

### Features

* support network request aborting and refetching ([#34](#34)) ([dcb4a70](dcb4a70))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants