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 update to take a reducer. #347

Merged
merged 2 commits into from
Sep 1, 2017
Merged

Allow update to take a reducer. #347

merged 2 commits into from
Sep 1, 2017

Conversation

jorgebucaran
Copy link
Owner

A reducer is a function that takes the state and returns a new state or a reducer.

update(state => {
  return { value: state.value + 1 }
}

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.

@codecov
Copy link

codecov bot commented Aug 31, 2017

Codecov Report

Merging #347 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #347   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         148    149    +1     
  Branches       45     46    +1     
=====================================
+ Hits          148    149    +1
Impacted Files Coverage Δ
src/app.js 100% <100%> (ø) ⬆️

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 118f645...6aeae79. Read the comment docs.

Copy link
Contributor

@okwolf okwolf left a 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
Copy link
Contributor

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?

Copy link
Owner Author

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.

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.
@jorgebucaran
Copy link
Owner Author

jorgebucaran commented Sep 1, 2017

Updated docs and added 100% coverage.

I'd like to merge here and now work on a substantial docs update. 🔥

@jorgebucaran jorgebucaran merged commit 5889ee9 into master Sep 1, 2017
@jorgebucaran jorgebucaran deleted the thunks/state branch September 1, 2017 05:20
@jorgebucaran jorgebucaran added the enhancement New feature or request label Sep 1, 2017
@jorgebucaran
Copy link
Owner Author

Related zaceno/hyperapp-partial#3

@jorgebucaran jorgebucaran self-assigned this Sep 1, 2017
jorgebucaran added a commit that referenced this pull request Jan 7, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants