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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to simplify a producer + patches #400

Closed
mweststrate opened this issue Jul 31, 2019 · 0 comments
Closed

API to simplify a producer + patches #400

mweststrate opened this issue Jul 31, 2019 · 0 comments
Labels

Comments

@mweststrate
Copy link
Collaborator

mweststrate commented Jul 31, 2019

馃殌 Feature Proposal

Introduce API's to capture the next state and patches directly:

  • produceWithPatches(baseState, recipe) => [nextState, patches, inversePatches]
  • produceWithPatches(recipe) => (baseState, ...args) => [nextState, patches, inversePaches] (curried form)

Motivation

Currently it is quite annoying to grab the patches from a producer, for two reasons:

First, directly returning is often impossible, and a lot of temporarily local vars enter the game, in order to be able to wire up the receiving both the next state and the patches. The following pattern I encounter quite reqularly:

const myCoolFunc(state) => produce(state, draft => { /* something */ })

// to capture patches, it must be destructred to :
// (this is even worse if you used a curried produce, as it has to be uncurried to be able to write the following)
const myCoolFuncWithPatches(state) => {
   let patches
   const nextState = produce(state, draft => { /* something */ }, p => { patches = p })
   return [nextState, patches]
}

// with this proposal it becomes
const myCoolFunc(state) => produceWithPatches(state, draft => { /* something */ })

cc @aleclarson

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

No branches or pull requests

2 participants