Skip to content
Myr

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

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

authored by Patrick Kerschbaum · committed by Michel Weststrate · parent 3664e8b · 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
34 unmodified lines
35The Immer package exposes a default function that does all the work.3637`produce(currentState, producer: (draftState) => void): nextState`3839There is also a curried overload that is explained [below](#currying)39There is also a curried overload that is explained [below](#currying).4041## Example4243```javascript
123 unmodified lines
167```168169## Currying170171`produce` can be called with one or two arguments.172The 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.171Passing 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.172The producer function gets passed in the draft, and any further arguments that were passed to the curried function.173174For example:175
24 unmodified lines
200})201```202203Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).205One think to keep in mind; you cannot use this construction to initialize an uninitialized state. E.g. `draft = {}` doesn't do anything useful.204205If you want to initialize an uninitialized state using this construction, you can do so by passing the initial state as second argument to `produce`:206207```javascript208import produce from 'immer'209210const byId = produce((draft, action) => {211 switch (action.type) {212 case RECEIVE_PRODUCTS:213 action.products.forEach(product => {214 draft[product.id] = product215 })216 })217 }218}, { 1: { id: 1, name: "product-1" } })219```220221## Auto freezing222223 Immer automatically freezes any state trees that are modified using `produce`.224 This protects against accidental modifications of the state tree outside of a producer.
4 unmodified lines
229230## Returning data from producers231232It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway.219However, it allowed to just `return draft`.233However, it is allowed to just `return draft`.234221It is also allowed to return abritrarily other data from the producer function. But _only_ if you didn't modify the draft.235It is also allowed to return arbitrarily other data from the producer function. But _only_ if you didn't modify the draft.236This can be useful to produce an entirely new state. Some examples:237238```javascript239const userReducer = produce((draft, action) => {
194 unmodified lines