immer.js
This file
Path
src/immer.js
62 lines · 2.4 KiB · JavaScript
Last changed by
Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
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) {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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 invocationMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 22why, one step back22 if (typeof baseState === "function") {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 23why, one step back23 // prettier-ignoreMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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 = argumentsMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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!Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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 {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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 #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 49–54why, one step back49 // if state is a primitive, don't bother proxying at all50 if (typeof baseState !== "object" || baseState === null) {51 const returnValue = producer(baseState)52 return returnValue === undefined ? baseState : returnValue53 }54Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 55–58why, one step back55 if (!isProxyable(baseState))56 throw new Error(57 `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"`58 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 59–62why, one step back59 return getUseProxies()60 ? produceProxy(baseState, producer)61 : produceEs5(baseState, producer)62}