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 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"Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 9why, one step back9import {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)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 49–51why, one step back49 return returnValue === undefined50 ? baseState51 : normalizeResult(returnValue)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 52–53why, one step back52 }53Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 54–58why, one step back54 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 back59}`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 60why, one step back60Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 61–64why, one step back61function 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 back65export default produceMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 66–67why, one step back6667export const applyPatches = produce(applyPatchesImpl)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 68–69why, one step back6869export const nothing = NOTHING