Skip to content
Myr

Implemented currying and argument checking

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 9cb4046 · Browse files · 4 files+82 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+82 added−16 removed

  • tests:+42 added−3 removed
  • other:+40 added−13 removed

4 files+82 added−16 removed

added:__tests__/curry.js+39 added
modified:__tests__/frozen.js+3 added-3 removed
modified:es5.js+25 added-4 removed
10 unmodified lines
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} thunk - function that receives a proxy of the base state as first argument and which can be freely modified15 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified16 * @returns {any} a new state, or the base state if nothing was modified17 */18function immer(baseState, thunk) {18function produce(baseState, producer) {19 // curried invocation20 if (arguments.length === 1) {21 const producer = baseState22 // prettier-ignore23 if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")24 return function() {25 const args = arguments26 return produce(args[0], draft => {27 args[0] = draft // blegh!28 baseState.apply(null, args)29 })30 }31 }3233 // prettier-ignore34 {35 if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)36 if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))37 if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")38 }3940 let finalizing = false41 let finished = false42 const descriptors = {}43 const states = []
147 unmodified lines
191192 // create proxy for root193 const rootClone = createProxy(undefined, baseState)194 // execute the thunk174 const maybeVoidReturn = thunk(rootClone)195 const maybeVoidReturn = producer(rootClone)196 //values either than undefined will trigger warning;197 !Object.is(maybeVoidReturn, undefined) &&198 console.warn(199 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`
72 unmodified lines
272 autoFreeze = enableAutoFreeze273}274275createHiddenProperty(exports, "__esModule", true)255module.exports.default = immer276module.exports.default = produce277module.exports.setAutoFreeze = setAutoFreeze
modified:immer.js+15 added-9 removed
70 unmodified lines
71 }72}7374/**75 * Immer takes a state, and runs a function against it.75 * produce takes a state, and runs a function against it.76 * That function can freely mutate the state, as it will create copies-on-write.77 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned78 *79 * @export80 * @param {any} baseState - the state to start with81 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified82 * @returns {any} a new state, or the base state if nothing was modified83 */84function immer(baseState, thunk) {84function produce(baseState, producer) {85 // curried invocation86 if (arguments.length === 1) {87 // TODO: check arg is fn87 const producer = baseState88 // prettier-ignore89 if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")90 return function() {89 // TODO: check arg1 is object / array91 const args = arguments91 return immer(args[0], draft => {92 return produce(args[0], draft => {93 args[0] = draft // blegh!94 baseState.apply(null, args)95 })96 }97 }9898 // TODO: check args99 // prettier-ignore100 {101 if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)102 if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))103 if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")104 }105 const revocableProxies = []106107 class State {108 constructor(parent, base) {
111 unmodified lines
220221 // create proxy for root222 const rootClone = createProxy(undefined, baseState)223 // execute the thunk218 const maybeVoidReturn = thunk(rootClone)224 const maybeVoidReturn = producer(rootClone)225 //values either than undefined will trigger warning;226 !Object.is(maybeVoidReturn, undefined) &&227 console.warn(228 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`
42 unmodified lines
271272Object.defineProperty(exports, "__esModule", {273 value: true274})269module.exports.default = immer275module.exports.default = produce276module.exports.setAutoFreeze = setAutoFreeze