Skip to content
Myr

immer.js

This file

Path

src/immer.js

77 lines · 2.7 KiB · JavaScript

Last changed by

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

History of this file

Take it

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

lines 1–6why, one step back
1export {
2 setAutoFreeze,3 setUseProxies,4 original,5 isProxy as isDraft6} from "./common"

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

line 7why, one step back
7

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

line 9why, one step back
9import {isProxy, isProxyable, getUseProxies, NOTHING} from "./common"

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

lines 10–20why, one step back
10import {produceProxy} from "./proxy"
11import {produceEs5} from "./es5"1213/**14 * produce takes a state, and runs a function against it.15 * That function can freely mutate the state, as it will create copies-on-write.16 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned17 *18 * @export19 * @param {any} baseState - the state to start with20 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 21why, one step back
21 * @param {Function} patchListener - optional function that will be called with all the patches produced here

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

lines 22–23why, one step back
22 * @returns {any} a new state, or the base state if nothing was modified
23 */

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 26why, one step back
26 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 27why, one step back
27

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

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

fix(produce): never curry when 2nd argument is a functionaleclarsonbefore adoption9a0f7d1

line 29why, one step back
29 if (typeof baseState === "function" && typeof producer !== "function") {

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

lines 30–32why, one step back
30 const initialState = producer
31 const recipe = baseState32

Merge pull request #237 from mweststrate/curryMichel Weststratebefore adoption7d2f308

lines 33–36why, one step back
33 return function(currentState = initialState, ...args) {
34 return produce(currentState, draft =>35 recipe.call(draft, draft, ...args)36 )

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

lines 37–41why, one step back
37 }
38 }3940 // prettier-ignore41 {

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

line 42why, one step back
42 if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 43why, one step back
43 if (patchListener !== undefined && typeof patchListener !== "function") throw new Error("the third argument of a producer should not be set or a function")

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

lines 44–45why, one step back
44 }
45

Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417

lines 46–47why, one step back
46 // avoid proxying anything except plain objects and arrays
47 if (!isProxyable(baseState)) {

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

line 48why, one step back
48 const returnValue = producer(baseState)

nit: dedupe some codealeclarsonbefore adoptionc9e1d98

lines 49–56why, one step back
49 return returnValue === undefined
50 ? baseState51 : normalizeResult(returnValue)52 }5354 // See #100, don't nest producers55 if (isProxy(baseState)) {56 const returnValue = producer.call(baseState, baseState)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 57–59why, one step back
57 return returnValue === undefined
58 ? baseState59 : normalizeResult(returnValue)

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

lines 60–61why, one step back
60 }
61

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 62–66why, one step back
62 return normalizeResult(
63 getUseProxies()64 ? produceProxy(baseState, producer, patchListener)65 : produceEs5(baseState, producer, patchListener)66 )

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

line 67why, one step back
67}

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

line 68why, one step back
68

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 69–72why, one step back
69function normalizeResult(result) {
70 return result === NOTHING ? undefined : result71}72

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

line 73why, one step back
73export default produce

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 74–75why, one step back
74
75export const applyPatches = produce(applyPatchesImpl)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 76–77why, one step back
76
77export const nothing = NOTHING