Skip to content
Myr

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 file

Take it

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

lines 1–17why, one step back
1export {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 back
18 // prettier-ignore
19 if (arguments.length !== 1 && arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)20

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 21why, one step back
21 // curried invocation

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 22why, one step back
22 if (typeof baseState === "function") {

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

line 23why, one step back
23 // prettier-ignore

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 24–28why, one step back
24 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 = baseState28

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

lines 29–30why, one step back
29 return function() {
30 const args = arguments

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 31–37why, one step back
31
32 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 back
38 args[0] = draft // blegh!

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 39why, one step back
39 return recipe.apply(draft, args)

Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667

lines 40–45why, one step back
40 })
41 }42 }4344 // prettier-ignore45 {

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 46why, one step back
46 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 back
47 }
48

Merge pull request #113 from christiangenco/patch-1benbraoubefore adoption233e1d3

line 49why, one step back
49 // if state is a primitive, don't bother proxying at all and just return whatever the producer returns on that value

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 50–55why, one step back
50 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 back
56 return getUseProxies()
57 ? produceProxy(baseState, producer)58 : produceEs5(baseState, producer)59}