Skip to content
Myr

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

3 files+56 added−40 removed

modified:src/common.js+16 added
modified:src/es5.js+20 added-18 removed
6 unmodified lines
7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject,11 shallowCopy11 shallowCopy,12 verifyReturnValue,13 each14} from "./common"1516const descriptors = {}17let states = null
76 unmodified lines
94}9596function 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}103104function 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}111112function assertUnfinished(state) {
46 unmodified lines
159160function 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}168169function 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}177178export function produceEs5(baseState, producer) {
2 unmodified lines
181 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 lines
modified:src/proxy.js+20 added-22 removed
6 unmodified lines
7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject,11 shallowCopy11 shallowCopy,12 verifyReturnValue,13 each14} from "./common"1514let revocableProxies = null16let proxies = null1718const objectTraps = {19 get,20 has(target, prop) {
11 unmodified lines
32 }33}3435const arrayTraps = {}34for (let key in objectTraps) {36each(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})4243function createState(parent, base) {44 return {45 modified: false,
63 unmodified lines
109function 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 lines
124 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}131132// given a base object, returns it if unmodified, or return the changed cloned if modified
14 unmodified lines
147148function 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}156157function 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}165166export 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}