Skip to content
Myr

`produce` is now exported as both default and name export. Implements #136

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

authored by Michel Weststrate · committed by Michel Weststrate · parent b618bf9 · Browse files · 6 files+184 added−125 removed
This commit

State

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

Diff stats

6 files+184 added−125 removed

  • tests:+31 added−2 removed
  • other:+153 added−123 removed

6 files+184 added−125 removed

modified:__tests__/flow/flow.js.flow+13 added-1 removed
1// @flow2import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"2import produce, {setAutoFreeze, setUseProxies, produce as produce2} from "../../src/immer"34setAutoFreeze(true)5setUseProxies(true)6
66 unmodified lines
7374 // $ExpectError75 f2(undefined, 3)76}7778{79 // $ExpectError80 produce2({x: 3})8182 const result = produce2({x: 3}, draft => {83 draft.x = 48485 // $ExpectError86 console.log(draft.y)87 })88}
modified:__tests__/types.ts+18 added-1 removed
modified:src/immer.d.ts+74 added-68 removed
modified:src/immer.js+3 added-1 removed
12 unmodified lines
13 * @param {any} baseState - the state to start with14 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified15 * @returns {any} a new state, or the base state if nothing was modified16 */17export default function produce(baseState, producer) {17export function produce(baseState, producer) {18 // prettier-ignore19 if (arguments.length !== 1 && arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)2021 // curried invocation
37 unmodified lines
59 return getUseProxies()60 ? produceProxy(baseState, producer)61 : produceEs5(baseState, producer)62}6364export default produce
modified:src/immer.js.flow+60 added-54 removed
1// @flow23interface IProduce {4 /**5 * Immer takes a state, and runs a function against it.6 * That function can freely mutate the state, as it will create copies-on-write.7 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.8 *9 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe10 * any time it is called with the current state.11 *12 * @param currentState - the state to start with13 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified14 * @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 undefined15 * @returns The next state: a new state, or the current state if nothing was modified16 */17 <S>(18 currentState: S,19 recipe: (draftState: S) => S | void20 ): S;21 // curried invocations with inital state22 <S, A, B, C>(23 recipe: (draftState: S, a: A, b: B, c: C) => S | void,24 initialState: S25 ): (currentState: S | void, a: A, b: B, c: C) => S;26 <S, A, B>(27 recipe: (draftState: S, a: A, b: B) => S | void,28 initialState: S29 ): (currentState: S | void, a: A, b: B) => S;30 <S, A>(31 recipe: (draftState: S, a: A) => S | void,32 initialState: S33 ): (currentState: S | void, a: A) => S;34 <S>(35 recipe: (draftState: S) => S | void,36 initialState: S37 ): (currentState: S | void) => S;38 <S>(39 recipe: (draftState: S, ...extraArgs: any[]) => S | void,40 initialState: S41 ): (currentState: S | void, ...extraArgs: any[]) => S;42 // curried invocations without inital state43 <S, A, B, C>(44 recipe: (draftState: S, a: A, b: B, c: C) => S | void45 ): (currentState: S, a: A, b: B, c: C) => S;46 <S, A, B>(47 recipe: (draftState: S, a: A, b: B) => S | void48 ): (currentState: S, a: A, b: B) => S;49 <S, A>(50 recipe: (draftState: S, a: A) => S | void51 ): (currentState: S, a: A) => S;52 <S>(53 recipe: (draftState: S) => S | void54 ): (currentState: S) => S;55 <S>(56 recipe: (draftState: S, ...extraArgs: any[]) => S | void57 ): (currentState: S, ...extraArgs: any[]) => S;58}5960declare export var produce: IProduce61declare export default IProduce6263/**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 returned.7 *8 * 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 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) => S | void19): S20// curried invocations with inital state21declare export default function produce<S, A, B, C>(22 recipe: (draftState: S, a: A, b: B, c: C) => S | void,23 initialState: S24): (currentState: S | void, a: A, b: B, c: C) => S25declare export default function produce<S, A, B>(26 recipe: (draftState: S, a: A, b: B) => S | void,27 initialState: S28): (currentState: S | void, a: A, b: B) => S29declare export default function produce<S, A>(30 recipe: (draftState: S, a: A) => S | void,31 initialState: S32): (currentState: S | void, a: A) => S33declare export default function produce<S>(34 recipe: (draftState: S) => S | void,35 initialState: S36): (currentState: S | void) => S37declare export default function produce<S>(38 recipe: (draftState: S, ...extraArgs: any[]) => S | void,39 initialState: S40): (currentState: S | void, ...extraArgs: any[]) => S41// curried invocations without inital state42declare export default function produce<S, A, B, C>(43 recipe: (draftState: S, a: A, b: B, c: C) => S | void44): (currentState: S, a: A, b: B, c: C) => S45declare export default function produce<S, A, B>(46 recipe: (draftState: S, a: A, b: B) => S | void47): (currentState: S, a: A, b: B) => S48declare export default function produce<S, A>(49 recipe: (draftState: S, a: A) => S | void50): (currentState: S, a: A) => S51declare export default function produce<S>(52 recipe: (draftState: S) => S | void53): (currentState: S) => S54declare export default function produce<S>(55 recipe: (draftState: S, ...extraArgs: any[]) => S | void56): (currentState: S, ...extraArgs: any[]) => S57/**64 * Automatically freezes any state trees generated by immer.65 * This protects against accidental modifications of the state tree outside of an immer function.66 * This comes with a performance impact, so it is recommended to disable this option in production.67 * By default it is turned on during local development, and turned off in production.
8 unmodified lines
modified:readme.md+16 added
316 unmodified lines
317## Immer on older JavaScript environments?318319By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.320321## Importing immer322323`produce` is exposed as the default export, but optionally it can be used as name import as well, as this benefits some older project setups. So the following imports are all correct, where the first is recommend:324325```javascript326import produce from "immer"327import { produce } from "immer"328329const { produce } = require("immer")330const produce = require("immer").produce331const produce = require("immer").default332333import unleashTheMagic from "immer"334import { produce as unleashTheMagic } from "immer"335```336337## Pitfalls3383391. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).3401. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.
128 unmodified lines