Skip to content
Myr

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fn

Before adoption: this commit came in with the history the project adopted, so no landing records it.

resolve #111: allow passing initial state to curried producer function

authored by Michel Weststrate · committed by Michel Weststrate · parents b56f538, 4a619a2 · Browse files · 5 files+80 added−33 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+80 added−33 removed

  • tests:+16 added−4 removed
  • other:+64 added−29 removed

5 files+80 added−33 removed

modified:__tests__/curry.js+16 added-4 removed
modified:src/immer.d.ts+13 added-8 removed
modified:src/immer.js+18 added-7 removed
modified:src/immer.js.flow+13 added-8 removed
1 unmodified line
23/**4 * Immer takes a state, and runs a function against it.5 * That function can freely mutate the state, as it will create copies-on-write.6 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned6 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.7 *8 * If only one argument is passed, this is interpreted as the recipe, and will create a curried function that will execute the recipe9 * any time it is called with a base state8 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe9 * any time it is called with the current state.10 *11 * @param currentState - the state to start with12 * @param thunk - function that receives a proxy of the current state as first argument and which can be freely modified12 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified13 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined14 * @returns The next state: a new state, or the current state if nothing was modified15 */16declare export default function produce<S>(17 currentState: S,18 recipe: (draftState: S) => void19): S20// curried invocations21declare export default function produce<S, A, B, C>(21 recipe: (draftState: S, a: A, b: B, c: C) => void22 recipe: (draftState: S, a: A, b: B, c: C) => void,23 initialState?: S24): (currentState: S, a: A, b: B, c: C) => S25declare export default function produce<S, A, B>(24 recipe: (draftState: S, a: A, b: B) => void26 recipe: (draftState: S, a: A, b: B) => void,27 initialState?: S28): (currentState: S, a: A, b: B) => S29declare export default function produce<S, A>(27 recipe: (draftState: S, a: A) => void30 recipe: (draftState: S, a: A) => void,31 initialState?: S32): (currentState: S) => S33declare export default function produce<S>(30 recipe: (draftState: S, ...extraArgs: any[]) => void34 recipe: (draftState: S, ...extraArgs: any[]) => void,35 initialState?: S36): (currentState: S, ...extraArgs: any[]) => S3738/**39 * Automatically freezes any state trees generated by immer.
11 unmodified lines
modified:readme.md+20 added-6 removed
39 unmodified lines
40The Immer package exposes a default function that does all the work.4142`produce(currentState, producer: (draftState) => void): nextState`4344There is also a curried overload that is explained [below](#currying)44There is also a curried overload that is explained [below](#currying).4546## Example4748```javascript
123 unmodified lines
172```173174## Currying175176`produce` can be called with one or two arguments.177The one argument version is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from.176Passing a function as the first argument to `produce` is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from.177The producer function gets passed in the draft, and any further arguments that were passed to the curried function.178179For example:180
24 unmodified lines
205})206```207208Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).210One think to keep in mind; you cannot use this construction to initialize an uninitialized state. E.g. `draft = {}` doesn't do anything useful.209210If you want to initialize an uninitialized state using this construction, you can do so by passing the initial state as second argument to `produce`:211212```javascript213import produce from 'immer'214215const byId = produce((draft, action) => {216 switch (action.type) {217 case RECEIVE_PRODUCTS:218 action.products.forEach(product => {219 draft[product.id] = product220 })221 })222 }223}, { 1: { id: 1, name: "product-1" } })224```225226## Auto freezing227228 Immer automatically freezes any state trees that are modified using `produce`.229 This protects against accidental modifications of the state tree outside of a producer.
4 unmodified lines
234235## Returning data from producers236237It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway.224However, it allowed to just `return draft`.238However, it is allowed to just `return draft`.239226It is also allowed to return abritrarily other data from the producer function. But _only_ if you didn't modify the draft.240It is also allowed to return arbitrarily other data from the producer function. But _only_ if you didn't modify the draft.241This can be useful to produce an entirely new state. Some examples:242243```javascript244const userReducer = produce((draft, action) => {
195 unmodified lines