immer.js
This file
Path
src/immer.js
103 lines · 3.8 KiB · JavaScript
Last changed by
Implemented inverse patchesMichel Weststratebefore adoptiond203f56
History of this fileTake it
Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 1–16why, one step back1export {setAutoFreeze, setUseProxies} from "./common"23import {isProxyable, getUseProxies} from "./common"4import {produceProxy} from "./proxy"5import {produceEs5} from "./es5"67/**8 * produce takes a state, and runs a function against it.9 * That function can freely mutate the state, as it will create copies-on-write.10 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned11 *12 * @export13 * @param {any} baseState - the state to start with14 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified15 * @returns {any} a new state, or the base state if nothing was modified16 */Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 17why, one step back17export default function produce(baseState, producer, patchListener) {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 18why, one step back18 // prettier-ignoreInitial work on supporting patchesMichel Weststratebefore adoptiond821dda
line 19why, one step back19 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 20why, one step back20Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 21why, one step back21 // curried invocationMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 22why, one step back22 if (typeof baseState === "function") {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 23why, one step back23 // prettier-ignoreMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 24–28why, one step back24 if (typeof producer === "function") throw new Error("if first argument is a function (curried invocation), the second argument to produce cannot be a function")2526 const initialState = producer27 const recipe = baseState28Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 29–30why, one step back29 return function() {30 const args = argumentsMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 31–37why, one step back3132 const currentState =33 args[0] === undefined && initialState !== undefined34 ? initialState35 : args[0]3637 return produce(currentState, draft => {Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 38why, one step back38 args[0] = draft // blegh!Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 39why, one step back39 return recipe.apply(draft, args)Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 40–45why, one step back40 })41 }42 }4344 // prettier-ignore45 {Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 46why, one step back46 if (typeof producer !== "function") throw new Error("if first argument is not a function, the second argument to produce should be a function")Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
lines 47–48why, one step back47 }48Merge pull request #157 from stefanwille/fix-148-default-returned-valueMichel Weststratebefore adoptiond87d637
lines 49–54why, one step back49 // if state is a primitive, don't bother proxying at all50 if (typeof baseState !== "object" || baseState === null) {51 const returnValue = producer(baseState)52 return returnValue === undefined ? baseState : returnValue53 }54Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 55–58why, one step back55 if (!isProxyable(baseState))56 throw new Error(57 `the first argument to an immer producer should be a primitive, plain object or array, got ${typeof baseState}: "${baseState}"`58 )Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 59why, one step back59 return getUseProxies()Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 60–61why, one step back60 ? produceProxy(baseState, producer, patchListener)61 : produceEs5(baseState, producer, patchListener)Renamed `index.js` -> `immer.js`Michel Weststratebefore adoptionc4f5667
line 62why, one step back62}Implemented inverse patchesMichel Weststratebefore adoptiond203f56
lines 63–103why, one step back6364export function applyPatches(base, patches) {65 return produce(base, draft => {66 debugger67 for (let i = 0; i < patches.length; i++) {68 const patch = patches[i]69 if (patch.path.length === 0 && patch.op === "replace") {70 draft = patch.value71 } else {72 const path = patch.path.slice()73 const key = path.pop()74 const base = path.reduce((current, part) => {75 if (!current)76 throw new Error(77 "Cannot apply patch, path doesn't resolve: " +78 patch.path.join("/")79 )80 return current[part]81 }, draft)82 if (!base)83 throw new Error(84 "Cannot apply patch, path doesn't resolve: " +85 patch.path.join("/")86 )87 switch (patch.op) {88 case "replace":89 base[key] = patch.value90 break91 case "remove":92 delete base[key]93 break94 default:95 throw new Error(96 "Unsupported patch operation: " + patch.op97 )98 }99 }100 }101 return draft102 })103}