immer.js
This file
Path
src/immer.js
75 lines · 2.8 KiB · JavaScript
Last changed by
fix(produce): avoid throwing on unproxyable objectsaleclarsonbefore adoptionea81dfe
History of this fileTake it
feat: export `isDraft` function (#220)Alec Larsonbefore adoption172d76a
line 1why, one step back1export {setAutoFreeze, setUseProxies, original, isProxy as isDraft} 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 invocationMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 24why, one step back24 if (typeof baseState === "function") {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 25why, one step back25 // prettier-ignoreMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 26–30why, one step back26 if (typeof producer === "function") throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function")2728 const initialState = producer29 const recipe = baseState30Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 31–32why, one step back31 return function() {32 const args = argumentsMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 33–39why, one step back3334 const currentState =35 args[0] === undefined && initialState !== undefined36 ? initialState37 : args[0]3839 return produce(currentState, draft => {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 40why, one step back40 args[0] = draft // blegh!Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 41why, one step back41 return recipe.apply(draft, args)Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 42–47why, one step back42 })43 }44 }4546 // prettier-ignore47 {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 48why, one step back48 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 49why, one step back49 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 50–51why, one step back50 }51fix(produce): avoid throwing on unproxyable objectsaleclarsonbefore adoptionea81dfe
lines 52–53why, one step back52 // avoid proxying anything except plain objects and arrays53 if (!isProxyable(baseState)) {Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
line 54why, one step back54 const returnValue = producer(baseState)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 55–57why, one step back55 return returnValue === undefined56 ? baseState57 : normalizeResult(returnValue)Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 58–59why, one step back58 }59Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 60–64why, one step back60 return normalizeResult(61 getUseProxies()62 ? produceProxy(baseState, producer, patchListener)63 : produceEs5(baseState, producer, patchListener)64 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 65why, one step back65}`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 66why, one step back66Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 67–70why, one step back67function normalizeResult(result) {68 return result === NOTHING ? undefined : result69}70`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015
line 71why, one step back71export default produceMerge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 72–73why, one step back7273export const applyPatches = produce(applyPatchesImpl)Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2
lines 74–75why, one step back7475export const nothing = NOTHING