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 fileTake it
Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 1–6why, one step back1export {2 setAutoFreeze,3 setUseProxies,4 original,5 isProxy as isDraft6} from "./common"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 7why, one step back7Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 8why, one step back8import {applyPatches as applyPatchesImpl} from "./patches"nit: dedupe some codealeclarsonbefore adoptionc9e1d98
line 9why, one step back9import {isProxy, isProxyable, getUseProxies, NOTHING} from "./common"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 10–20why, one step back10import {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 modifiedMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 21why, one step back21 * @param {Function} patchListener - optional function that will be called with all the patches produced hereRenamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 22–23why, one step back22 * @returns {any} a new state, or the base state if nothing was modified23 */Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 24why, one step back24export function produce(baseState, producer, patchListener) {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 25why, one step back25 // prettier-ignoreMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 26why, one step back26 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 back27Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 28why, one step back28 // curried invocationfix(produce): never curry when 2nd argument is a functionaleclarsonbefore adoption9a0f7d1
line 29why, one step back29 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 back30 const initialState = producer31 const recipe = baseState32Merge pull request #237 from mweststrate/curryMichel Weststratebefore adoption7d2f308
lines 33–36why, one step back33 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 back37 }38 }3940 // prettier-ignore41 {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 42why, one step back42 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 back43 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 back44 }45Merge remote-tracking branch 'origin/v1.8'Michel Weststratebefore adoption7790417
lines 46–47why, one step back46 // avoid proxying anything except plain objects and arrays47 if (!isProxyable(baseState)) {Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 48why, one step back48 const returnValue = producer(baseState)nit: dedupe some codealeclarsonbefore adoptionc9e1d98
lines 49–56why, one step back49 return returnValue === undefined50 ? 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 back57 return returnValue === undefined58 ? baseState59 : normalizeResult(returnValue)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 60–61why, one step back60 }61Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 62–66why, one step back62 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 back67}`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 68why, one step back68Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 69–72why, one step back69function 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 back73export default produceMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 74–75why, one step back7475export const applyPatches = produce(applyPatchesImpl)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 76–77why, one step back7677export const nothing = NOTHING