-
Notifications
You must be signed in to change notification settings - Fork 782
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 update to take a reducer. #347
Conversation
Codecov Report
@@ Coverage Diff @@
## master #347 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 2 2
Lines 148 149 +1
Branches 45 46 +1
=====================================
+ Hits 148 149 +1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this come with an update to the docs and test coverage?
@@ -8,7 +8,7 @@ yarn.lock | |||
package-lock.json | |||
|
|||
# Misc | |||
sandbox | |||
example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not using either of these are we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@okwolf I am locally, but it's not committed to the repo.
2b35ff2
to
a4fe3f1
Compare
Inside a thunk, update usually takes a partial state and merges it with the previous state producing a new state. This commit extends update to also take a function that we will immediately call with the latest state and like an action/reducer, allows you to update the state by returning a partial state. You'd want to use this feature when you need to update the state inside an async process and you are not sure whether the state that was passed to the action that started the async process has become out-of-date or not. A reducer is a function that receives the state and returns a partial state (or a reducer :mindblow:). actions: { someAyncAction(state) { return update => { longTaskWithCallback(someData, () => update(state => ({ value: state.value + 1 })) ) } } } This allows you to get the current & latest state inside the callback of some async call.
a4fe3f1
to
6aeae79
Compare
Updated docs and added 100% coverage. I'd like to merge here and now work on a substantial docs update. 🔥 |
Related zaceno/hyperapp-partial#3 |
Inside a thunk, update usually takes a partial state and merges it with the previous state producing a new state. This commit extends update to also take a function that we will immediately call with the latest state and like an action/reducer, allows you to update the state by returning a partial state. You'd want to use this feature when you need to update the state inside an async process and you are not sure whether the state that was passed to the action that started the async process has become out-of-date or not. A reducer is a function that receives the state and returns a partial state (or a reducer :mindblow:). actions: { someAyncAction(state) { return update => { longTaskWithCallback(someData, () => update(state => ({ value: state.value + 1 })) ) } } } This allows you to get the current & latest state inside the callback of some async call.
A reducer is a function that takes the state and returns a new state or a reducer.
This allows you to get the current & latest state inside the callback of some async call.
The alternative is using the state that was passed to the action that originated the async call. The problem is that this reference to the state may have gone out-of-date if other actions run and finished during the async process and changed the state.