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

allow passing a callback to useFetch's run to modify init #76

Merged
merged 12 commits into from
Aug 23, 2019

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Aug 9, 2019

Heyo :)

This adresses #75.

Instead of using a parameter to be spread to init, I chose for a callback function.
This has two reasons

  • if run is just passed as an onClick handler, a React SyntheticEvent would have been passed and possibly spread on to init. Detecting all kinds of events would be a pain, but typeof x === "function" is a simple test that it is NOT an event.
  • this way, existsing properties can be modified (like adding a new header) without introducing an additional dependency to something like deep-merge

I also split up the method definition between deferFnand promiseFn because the handling of 2 parameters vs 3 parameters was already confusing enough (sometimes props is ctrl, sometimes ctrl is ctrl).
I think this improves readability without impacting code size too much.

Also, I reduced boolean logic at two places to better handle the undefined case of defer and json.

Of, and of course there's a new unit test and a bit more README :)

Looking forward to your feedback. 🙋

@phryneas phryneas force-pushed the run-argument branch 4 times, most recently from 1b66fb5 to ec58f61 Compare August 9, 2019 10:02
@phryneas
Copy link
Member Author

phryneas commented Aug 9, 2019

Okay, now eslint should be happy, too.

@phryneas
Copy link
Member Author

phryneas commented Aug 9, 2019

Okay, I've tried the failing ./node_modules/.bin/lerna run --scope '*-example' test -- --passWithNoTests --watchAll=false locally and it's working. I have no idea what is failing there, could you take a look?

@phryneas
Copy link
Member Author

phryneas commented Aug 9, 2019

Seems like the tests are generally breaking with react 16.9 and something yarn ci does causes the newest react version to be installed.
So nothing associated with this PR :/

@phryneas
Copy link
Member Author

phryneas commented Aug 9, 2019

I opened #77 to adress the test issue.

As I'll be gone for vacation tomorrow, I can only do any changes you request today during the day - otherwise feel free to just push any modifications to the top of my branch. (Due to #77, I fear this might take a little longer unti you get to this :/ )

@ghengeveld
Copy link
Member

I'm very happy with where you're going with this. I'm in doubt about the callback though. I like the flexibility it provides but I still would prefer to have the option to just pass an object. We can detect a SyntheticEvent using one of the attributes they have, such as preventDefault.

My preference would be:

  • if function, use it to determine init
  • if object and not SyntheticEvent, spread it after init
  • else do nothing with init

@phryneas
Copy link
Member Author

phryneas commented Aug 10, 2019 via email

@phryneas phryneas force-pushed the run-argument branch 2 times, most recently from bae616c to db67da9 Compare August 15, 2019 14:48
@phryneas
Copy link
Member Author

Okay, that should do it for the functionality. I also pushed a commit that gives run in the context of useFetch better typings.

And I added an example. But unfortunately, I can't get the examples to run - are these examples currently runnable? It seems as if it can't parse the jsx tokens in packages/react-async/Async.jsx.

@phryneas phryneas force-pushed the run-argument branch 2 times, most recently from 0e5ccae to 169c75a Compare August 15, 2019 19:20
@phryneas
Copy link
Member Author

I just rebased this on master and it seems to fail again - I guess this is due to some change on master?

@ghengeveld
Copy link
Member

Yes the introduction of React v16.9 is causing some issues. On CircleCI things work fine but on Travis they fail. I suspect updating the examples to 16.9 might help.

@ghengeveld
Copy link
Member

That seems to work. Should be fixed now.

@codecov
Copy link

codecov bot commented Aug 16, 2019

Codecov Report

Merging #76 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #76      +/-   ##
==========================================
+ Coverage   98.91%   98.93%   +0.01%     
==========================================
  Files           8        8              
  Lines         370      375       +5     
  Branches      136      140       +4     
==========================================
+ Hits          366      371       +5     
  Misses          4        4
Impacted Files Coverage Δ
packages/react-async/src/useAsync.js 99.05% <100%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dda874a...becc9c3. Read the comment docs.

@phryneas
Copy link
Member Author

Okay, tests are now passing.
Also, I found the problem with my example: my IDE had imported useFetch from react-async/src instead of react-async and I had missed that.
Now everything should be good.

@ghengeveld ghengeveld merged commit bca0187 into async-library:master Aug 23, 2019
@ghengeveld
Copy link
Member

Released in v8.0.0

@ghengeveld
Copy link
Member

@all-contributors please add @phryneas for ideas

@allcontributors
Copy link
Contributor

@ghengeveld

I've put up a pull request to add @phryneas! 🎉

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