Skip to content
Myr

immer.js

This file

Path

src/immer.js

65 lines · 2.5 KiB · JavaScript

Last changed by

Moved to single file setupMichel Weststratebefore adoption160038b

History of this file

Take it

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

lines 1–2why, one step back
1export {setAutoFreeze, setUseProxies} from "./common"
2

Moved to single file setupMichel Weststratebefore adoption160038b

line 3why, one step back
3import {applyPatches as applyPatchesImpl} from "./patches"

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

lines 4–17why, one step back
4import {isProxyable, getUseProxies} from "./common"
5import {produceProxy} from "./proxy"6import {produceEs5} from "./es5"78/**9 * produce takes a state, and runs a function against it.10 * That function can freely mutate the state, as it will create copies-on-write.11 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned12 *13 * @export14 * @param {any} baseState - the state to start with15 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified16 * @returns {any} a new state, or the base state if nothing was modified17 */

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 18why, one step back
18export default function produce(baseState, producer, patchListener) {

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

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

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 20why, one step back
20 if (arguments.length < 1 && arguments.length > 3) throw new Error("produce expects 1 to 3 arguments, got " + arguments.length)

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

line 21why, one step back
21

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

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

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

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

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

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

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

lines 25–29why, one step back
25 if (typeof producer === "function") throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function")
2627 const initialState = producer28 const recipe = baseState29

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

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

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

lines 32–38why, one step back
32
33 const currentState =34 args[0] === undefined && initialState !== undefined35 ? initialState36 : args[0]3738 return produce(currentState, draft => {

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

line 39why, one step back
39 args[0] = draft // blegh!

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

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

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

lines 41–46why, one step back
41 })
42 }43 }4445 // prettier-ignore46 {

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

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

Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637

lines 50–55why, one step back
50 // if state is a primitive, don't bother proxying at all
51 if (typeof baseState !== "object" || baseState === null) {52 const returnValue = producer(baseState)53 return returnValue === undefined ? baseState : returnValue54 }55

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

lines 56–59why, one step back
56 if (!isProxyable(baseState))
57 throw new Error(58 `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"`59 )

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

line 60why, one step back
60 return getUseProxies()

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 61–62why, one step back
61 ? produceProxy(baseState, producer, patchListener)
62 : produceEs5(baseState, producer, patchListener)

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

line 63why, one step back
63}

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

line 64why, one step back
64

Moved to single file setupMichel Weststratebefore adoption160038b

line 65why, one step back
65export const applyPatches = produce(applyPatchesImpl)