some code cleanup / more reuse
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 7d0b7e8 · Browse files · 3 files·+56 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+56 added−40 removed
- other:+56 added−40 removed
Provenance
- Commit
9d393e3- Tree
1960eed- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
7d0b7e8
3 files changed+56 added−40 removed
modified:src/common.js+16 added
34 unmodified lines3536373839404142434445464748495017 unmodified lines68697071727374757677787934 unmodified linesexport function shallowCopy(value) { return Array.isArray(value) ? value.slice() : Object.assign({}, value) // TODO: eliminate those isArray checks?}
export function each(value, cb) { if (Array.isArray(value)) { for (let i = 0; i < value.length; i++) cb(i, value[i]) } else { for (let key in value) cb(key, value[key]) }}
export function finalizeNonProxiedObject(parent, finalizer) { // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well if (!isProxyable(parent)) return17 unmodified lines } } if (modified) freeze(parent)}
export function verifyReturnValue(value) { //values either than undefined will trigger warning; if (value !== undefined) console.warn( `Immer callback expects no return value. However ${typeof value} was returned` )}modified:src/es5.js+20 added-18 removed
6 unmodified lines7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject,11- shallowCopy11+ shallowCopy,12+ verifyReturnValue,13+ each14 } from "./common"15 16 const descriptors = {}17 let states = null76 unmodified lines94 }95 96 function createObjectProxy(base) {97 const proxy = Object.assign({}, base)96- Object.keys(base).forEach(prop =>98+ each(base, prop => {99 Object.defineProperty(proxy, prop, createPropertyProxy(prop))98- )100+ })101 return proxy102 }103 104 function createArrayProxy(base) {105 const proxy = new Array(base.length)104- for (let i = 0; i < base.length; i++)106+ each(base, i => {107 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))108+ })109 return proxy110 }111 112 function assertUnfinished(state) {46 unmodified lines159 160 function finalizeObject(proxy, state) {161 const res = Object.assign({}, proxy)162 const base = state.base160- for (let prop in res) {161- if (proxy[prop] !== base[prop]) res[prop] = finalize(res[prop])162- }163+ each(res, (prop, value) => {164+ if (value !== base[prop]) res[prop] = finalize(value)165+ })166 return freeze(res)167 }168 169 function finalizeArray(proxy, state) {170 const res = proxy.slice()171 const base = state.base169- for (let i = 0; i < res.length; i++) {170- if (res[i] !== base[i]) res[i] = finalize(res[i])171- }172+ each(res, (i, value) => {173+ if (value !== base[i]) res[i] = finalize(value)174+ })175 return freeze(res)176 }177 178 export function produceEs5(baseState, producer) {2 unmodified lines181 try {182 // create proxy for root183 const rootClone = createProxy(undefined, baseState)184 // execute the thunk182- const maybeVoidReturn = producer(rootClone)183- //values either than undefined will trigger warning;184- !Object.is(maybeVoidReturn, undefined) &&185- console.warn(186- `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`187- )185+ verifyReturnValue(producer(rootClone))186 // and finalize the modified proxy189- for (let i = 0; i < states.length; i++) states[i].finalizing = true187+ each(states, (_, state) => {188+ state.finalizing = true189+ })190 // find and mark all changes (for parts not done yet)191 markChanges()192 const res = finalize(rootClone)193 // make sure all proxies become unusable194- for (let i = 0; i < states.length; i++) states[i].finished = true194+ each(states, (_, state) => {195+ state.finished = true196+ })197 return res198 } finally {199 states = prevStates200 }34 unmodified linesmodified:src/proxy.js+20 added-22 removed
6 unmodified lines7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject,11- shallowCopy11+ shallowCopy,12+ verifyReturnValue,13+ each14 } from "./common"15 14-let revocableProxies = null16+let proxies = null17 18 const objectTraps = {19 get,20 has(target, prop) {11 unmodified lines32 }33 }34 35 const arrayTraps = {}34-for (let key in objectTraps) {36+each(objectTraps, (key, fn) => {37 arrayTraps[key] = function() {38 arguments[0] = arguments[0][0]37- return objectTraps[key].apply(this, arguments)39+ return fn.apply(this, arguments)40 }39-}41+})42 43 function createState(parent, base) {44 return {45 modified: false,63 unmodified lines109 function markChanged(state) {110 if (!state.modified) {111 state.modified = true112 state.copy = shallowCopy(state.base)113+ // copy the proxies over the base-copy114 Object.assign(state.copy, state.proxies) // yup that works for arrays as well115 if (state.parent) markChanged(state.parent)116 }117 }6 unmodified lines124 proxy = Proxy.revocable([state], arrayTraps)125 } else {126 proxy = Proxy.revocable(state, objectTraps)127 }125- revocableProxies.push(proxy)128+ proxies.push(proxy)129 return proxy.proxy130 }131 132 // given a base object, returns it if unmodified, or return the changed cloned if modified14 unmodified lines147 148 function finalizeObject(state) {149 const copy = state.copy150 const base = state.base148- for (var prop in copy) {149- if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop])150- }151+ each(copy, (prop, value) => {152+ if (value !== base[prop]) copy[prop] = finalize(value)153+ })154 return freeze(copy)155 }156 157 function finalizeArray(state) {158 const copy = state.copy159 const base = state.base157- for (let i = 0; i < copy.length; i++) {158- if (copy[i] !== base[i]) copy[i] = finalize(copy[i])159- }160+ each(copy, (i, value) => {161+ if (value !== base[i]) copy[i] = finalize(value)162+ })163 return freeze(copy)164 }165 166 export function produceProxy(baseState, producer) {164- const previousProxies = revocableProxies165- revocableProxies = []167+ const previousProxies = proxies168+ proxies = []169 try {170 // create proxy for root171 const rootClone = createProxy(undefined, baseState)172 // execute the thunk170- const maybeVoidReturn = producer(rootClone)171- //values either than undefined will trigger warning;172- !Object.is(maybeVoidReturn, undefined) &&173- console.warn(174- `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`175- )173+ verifyReturnValue(producer(rootClone))174 // and finalize the modified proxy175 const res = finalize(rootClone)176 // revoke all proxies179- revocableProxies.forEach(p => p.revoke())177+ each(proxies, (_, p) => p.revoke())178 return res179 } finally {182- revocableProxies = previousProxies180+ proxies = previousProxies181 }182 }