`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
Provenance
- Commit
05e3015- Tree
dd10ad0- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
b618bf9
6 files changed+184 added−125 removed
modified:__tests__/flow/flow.js.flow+13 added-1 removed
1 // @flow2-import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"2+import produce, {setAutoFreeze, setUseProxies, produce as produce2} from "../../src/immer"3 4 setAutoFreeze(true)5 setUseProxies(true)6 66 unmodified lines73 74 // $ExpectError75 f2(undefined, 3)76 }77+78+{79+ // $ExpectError80+ produce2({x: 3})81+82+ const result = produce2({x: 3}, draft => {83+ draft.x = 484+85+ // $ExpectError86+ console.log(draft.y)87+ })88+}modified:__tests__/types.ts+18 added-1 removed
11234559 unmodified lines656667686970717273747576777879808182838485import produce from '../src/immer';import produce, { produce as produce2} from '../src/immer';
interface State { readonly num: number; readonly foo?: string;59 unmodified lines })(state); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});
it('can update use the non-default export', () => { const newState = produce2<State>(draft => { draft.num++; draft.foo = 'bar'; draft.bar = 'foo'; draft.baz.x++; draft.baz.y++; draft.arr[0].value = 'foo'; draft.arr.push({ value: 'asf' }); draft.arr2[0].value = 'foo'; draft.arr2.push({ value: 'asf' }); })(state); expect(newState).not.toBe(state); expect(newState).toEqual(expectedState);});modified:src/immer.d.ts+74 added-68 removed
8 unmodified lines9101112131314151617181920212223242526272829141516171819202122232425262728293031313233343536373839404142434445464748495051525354555657583233343536373839404142434445464748495051525354555657585960606162636465666768697071727374757677787980818261626364656667686970717273747576777879808182838485868788899091929 unmodified lines8 unmodified lines T extends ReadonlyArray<any> ? DraftArray<T[number]> : T extends object ? DraftObject<T> : T;
export interface IProduce {/** * Immer takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned. * * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe * any time it is called with the current state. * * @param currentState - the state to start with * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified * @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 undefined * @returns The next state: a new state, or the current state if nothing was modified */export default function<S = any>( currentState: S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): S /** * Immer takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned. * * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe * any time it is called with the current state. * * @param currentState - the state to start with * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified * @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 undefined * @returns The next state: a new state, or the current state if nothing was modified */ <S = any>( currentState: S, recipe: (this: Draft<S>, draftState: Draft<S>) => void | S ): S
// curried invocations with default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, initialState: S): (currentState: S | undefined) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S, initialState: S): (currentState: S | undefined, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S, initialState: S): (currentState: S | undefined, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S, initialState: S): (currentState: S | undefined, ...extraArgs: any[]) => S // curried invocations with default initial state // 0 additional arguments <S = any>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | S, initialState: S ): (currentState: S | undefined) => S // 1 additional argument of type A <S = any, A = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S, initialState: S ): (currentState: S | undefined, a: A) => S // 2 additional arguments of types A and B <S = any, A = any, B = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S, initialState: S ): (currentState: S | undefined, a: A, b: B) => S // 3 additional arguments of types A, B and C <S = any, A = any, B = any, C = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S, initialState: S ): (currentState: S | undefined, a: A, b: B, c: C) => S // any number of additional arguments, but with loss of type safety // this may be alleviated if "variadic kinds" makes it into Typescript: // https://github.com/Microsoft/TypeScript/issues/5453 <S = any>( recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S, initialState: S ): (currentState: S | undefined, ...extraArgs: any[]) => S
// curried invocations without default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | S): (currentState: S) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S): (currentState: S, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S): (currentState: S, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S): (currentState: S, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S): (currentState: S, ...extraArgs: any[]) => S // curried invocations without default initial state // 0 additional arguments <S = any>( recipe: (this: Draft<S>, draftState: Draft<S>) => void | S ): (currentState: S) => S // 1 additional argument of type A <S = any, A = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S ): (currentState: S, a: A) => S // 2 additional arguments of types A and B <S = any, A = any, B = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | S ): (currentState: S, a: A, b: B) => S // 3 additional arguments of types A, B and C <S = any, A = any, B = any, C = any>( recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | S ): (currentState: S, a: A, b: B, c: C) => S // any number of additional arguments, but with loss of type safety // this may be alleviated if "variadic kinds" makes it into Typescript: // https://github.com/Microsoft/TypeScript/issues/5453 <S = any>( recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S ): (currentState: S, ...extraArgs: any[]) => S}
export const produce: IProduceexport default produce
/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production.9 unmodified linesmodified:src/immer.js+3 added-1 removed
12 unmodified lines13 * @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 */17-export default function produce(baseState, producer) {17+export 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)20 21 // curried invocation37 unmodified lines59 return getUseProxies()60 ? produceProxy(baseState, producer)61 : produceEs5(baseState, producer)62 }63+64+export default producemodified:src/immer.js.flow+60 added-54 removed
1 // @flow2 3+interface 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+}59+60+declare export var produce: IProduce61+declare export default IProduce62+63 /**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- */16-declare export default function produce<S>(17- currentState: S,18- recipe: (draftState: S) => S | void19-): S20-// curried invocations with inital state21-declare 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) => S25-declare 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) => S29-declare export default function produce<S, A>(30- recipe: (draftState: S, a: A) => S | void,31- initialState: S32-): (currentState: S | void, a: A) => S33-declare export default function produce<S>(34- recipe: (draftState: S) => S | void,35- initialState: S36-): (currentState: S | void) => S37-declare 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 state42-declare 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) => S45-declare export default function produce<S, A, B>(46- recipe: (draftState: S, a: A, b: B) => S | void47-): (currentState: S, a: A, b: B) => S48-declare export default function produce<S, A>(49- recipe: (draftState: S, a: A) => S | void50-): (currentState: S, a: A) => S51-declare export default function produce<S>(52- recipe: (draftState: S) => S | void53-): (currentState: S) => S54-declare 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 linesmodified:readme.md+16 added
316 unmodified lines317 ## Immer on older JavaScript environments?318 319 By 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.320 321+## Importing immer322+323+`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:324+325+```javascript326+import produce from "immer"327+import { produce } from "immer"328+329+const { produce } = require("immer")330+const produce = require("immer").produce331+const produce = require("immer").default332+333+import unleashTheMagic from "immer"334+import { produce as unleashTheMagic } from "immer"335+```336+337 ## Pitfalls338 339 1. 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).340 1. Currently, Immer only supports plain objects and arrays. PRs are welcome for more language built-in types like `Map` and `Set`.128 unmodified lines