immer.js
This file
Path
src/immer.js
68 lines · 2.7 KiB · JavaScript
Last changed by
fix(produce): never curry when 2nd argument is a functionaleclarsonbefore adoption9a0f7d1
History of this fileTake it
Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2
line 1why, one step back1export {setAutoFreeze, setUseProxies, original} from "./common"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 2why, one step back2Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 3why, one step back3import {applyPatches as applyPatchesImpl} from "./patches"Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
line 4why, one step back4import {isProxyable, getUseProxies, NOTHING} from "./common"Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 5–15why, one step back5import {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 modifiedMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 16why, one step back16 * @param {Function} patchListener - optional function that will be called with all the patches produced hereRenamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 17–18why, one step back17 * @returns {any} a new state, or the base state if nothing was modified18 */Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 19why, one step back19export function produce(baseState, producer, patchListener) {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 20why, one step back20 // prettier-ignoreMerge branch 'patches'Michel Weststratebefore adoption2513fb8
line 21why, one step back21 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 22why, one step back22Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 23why, one step back23 // curried invocationfix(produce): never curry when 2nd argument is a functionaleclarsonbefore adoption9a0f7d1
line 24why, one step back24 if (typeof baseState === "function" && typeof producer !== "function") {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 25–27why, one step back25 const initialState = producer26 const recipe = baseState27Merge pull request #237 from mweststrate/curryMichel Weststratebefore adoption7d2f308
lines 28–31why, one step back28 return function(currentState = initialState, ...args) {29 return produce(currentState, draft =>30 recipe.call(draft, draft, ...args)31 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 32–36why, one step back32 }33 }3435 // prettier-ignore36 {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 37why, one step back37 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 38why, one step back38 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 39–40why, one step back39 }40Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 41–43why, one step back41 // if state is a primitive, don't bother proxying at all42 if (typeof baseState !== "object" || baseState === null) {43 const returnValue = producer(baseState)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 44–46why, one step back44 return returnValue === undefined45 ? baseState46 : normalizeResult(returnValue)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 47–48why, one step back47 }48Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 49–52why, one step back49 if (!isProxyable(baseState))50 throw new Error(51 `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"`52 )Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 53–57why, one step back53 return normalizeResult(54 getUseProxies()55 ? produceProxy(baseState, producer, patchListener)56 : produceEs5(baseState, producer, patchListener)57 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 58why, one step back58}`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 59why, one step back59Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 60–63why, one step back60function normalizeResult(result) {61 return result === NOTHING ? undefined : result62}63`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 64why, one step back64export default produceMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 65–66why, one step back6566export const applyPatches = produce(applyPatchesImpl)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 67–68why, one step back6768export const nothing = NOTHING