Skip to content
Myr

immer.js

This file

Path

src/immer.js

69 lines · 2.5 KiB · JavaScript

Last changed by

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

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"

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

line 9why, one step back
9import {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)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 49–51why, one step back
49 return returnValue === undefined
50 ? baseState51 : normalizeResult(returnValue)

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

lines 52–53why, one step back
52 }
53

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 54–58why, one step back
54 return normalizeResult(
55 getUseProxies()56 ? produceProxy(baseState, producer, patchListener)57 : produceEs5(baseState, producer, patchListener)58 )

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

line 59why, one step back
59}

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

line 60why, one step back
60

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 61–64why, one step back
61function normalizeResult(result) {
62 return result === NOTHING ? undefined : result63}64

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

line 65why, one step back
65export default produce

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 66–67why, one step back
66
67export const applyPatches = produce(applyPatchesImpl)

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 68–69why, one step back
68
69export const nothing = NOTHING