immer.js
This file
Path
src/immer.js
59 lines · 2.3 KiB · JavaScript
Last changed by
Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960export {setAutoFreeze, setUseProxies} from "./common"
import {isProxyable, getUseProxies} from "./common"import {produceProxy} from "./proxy"import {produceEs5} from "./es5"
/** * produce 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 * * @export * @param {any} baseState - the state to start with * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @returns {any} a new state, or the base state if nothing was modified */export default function produce(baseState, producer) { // prettier-ignore if (arguments.length !== 1 && arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)
// curried invocation if (typeof baseState === "function") { // prettier-ignore if (typeof producer === "function") throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function")
const initialState = producer const recipe = baseState
return function() { const args = arguments
const currentState = args[0] === undefined && initialState !== undefined ? initialState : args[0]
return produce(currentState, draft => { args[0] = draft // blegh! return recipe.apply(draft, args) }) } }
// prettier-ignore { if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function") }
// if state is a primitive, don't bother proxying at all and just return whatever the producer returns on that value if (typeof baseState !== "object" || baseState === null) return producer(baseState) if (!isProxyable(baseState)) throw new Error( `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"` ) return getUseProxies() ? produceProxy(baseState, producer) : produceEs5(baseState, producer)}