immer.js
This file
Path
src/immer.js
59 lines · 2.3 KiB · JavaScript
Last changed by
resolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
History of this fileTake it
Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 1–17why, one step back1export {setAutoFreeze, setUseProxies} from "./common"23import {isProxyable, getUseProxies} from "./common"4import {produceProxy} from "./proxy"5import {produceEs5} from "./es5"67/**8 * produce takes a state, and runs a function against it.9 * That function can freely mutate the state, as it will create copies-on-write.10 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned11 *12 * @export13 * @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) {resolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
lines 18–20why, one step back18 // prettier-ignore19 if (arguments.length !== 1 && arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)20Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 21why, one step back21 // curried invocationresolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
line 22why, one step back22 if (typeof baseState === "function") {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 23why, one step back23 // prettier-ignoreresolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
lines 24–28why, one step back24 if (typeof producer === "function") throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function")2526 const initialState = producer27 const recipe = baseState28Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 29–30why, one step back29 return function() {30 const args = argumentsresolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
lines 31–37why, one step back3132 const currentState =33 args[0] === undefined && initialState !== undefined34 ? initialState35 : args[0]3637 return produce(currentState, draft => {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 38why, one step back38 args[0] = draft // blegh!resolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
line 39why, one step back39 return recipe.apply(draft, args)Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 40–45why, one step back40 })41 }42 }4344 // prettier-ignore45 {resolve #111: allow passing initial state to curried producer functionPatrick Kerschbaumbefore adoption202d54c
line 46why, one step back46 if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 47–48why, one step back47 }48Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3
line 49why, one step back49 // if state is a primitive, don't bother proxying at all and just return whatever the producer returns on that valueImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 50–55why, one step back50 if (typeof baseState !== "object" || baseState === null)51 return producer(baseState)52 if (!isProxyable(baseState))53 throw new Error(54 `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"`55 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 56–59why, one step back56 return getUseProxies()57 ? produceProxy(baseState, producer)58 : produceEs5(baseState, producer)59}