nit: dedupe some code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent d83f4b5 · Browse files · 3 files·+11 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
3 files+11 added−16 removed
- other:+11 added−16 removed
Provenance
- Commit
c9e1d98- Tree
d747880- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
d83f4b5
3 files changed+11 added−16 removed
modified:src/es5.js-6 removed
2 unmodified lines3 4 import {5 is,6 isProxyable,7- isProxy,7 PROXY_STATE,8 shallowCopy,9 RETURNED_AND_MODIFIED_ERROR,10 has,193 unmodified lines204 return false205 }206 207 export function produceEs5(baseState, producer, patchListener) {209- if (isProxy(baseState)) {210- // See #100, don't nest producers211- const returnValue = producer.call(baseState, baseState)212- return returnValue === undefined ? baseState : returnValue213- }208 const prevStates = states209 states = []210 const patches = patchListener && []211 const inversePatches = patchListener && []43 unmodified linesmodified:src/immer.js+9 added-1 removed
4 unmodified lines5678991011121336 unmodified lines5051525354555657585960616263646512 unmodified lines4 unmodified lines isProxy as isDraft} from "./common"
import {applyPatches as applyPatchesImpl} from "./patches"import {isProxyable, getUseProxies, NOTHING} from "./common"import {isProxy, isProxyable, getUseProxies, NOTHING} from "./common"import {produceProxy} from "./proxy"import {produceEs5} from "./es5"
/**36 unmodified lines ? baseState : normalizeResult(returnValue) }
// See #100, don't nest producers if (isProxy(baseState)) { const returnValue = producer.call(baseState, baseState) return returnValue === undefined ? baseState : normalizeResult(returnValue) }
return normalizeResult( getUseProxies() ? produceProxy(baseState, producer, patchListener) : produceEs5(baseState, producer, patchListener)12 unmodified linesmodified:src/proxy.js+2 added-9 removed
149 unmodified lines150 return proxy.proxy151 }152 153 export function produceProxy(baseState, producer, patchListener) {154- if (isProxy(baseState)) {155- // See #100, don't nest producers156- const returnValue = producer.call(baseState, baseState)157- return returnValue === undefined ? baseState : returnValue158- }154 const previousProxies = proxies155 proxies = []156 const patches = patchListener && []157 const inversePatches = patchListener && []158 try {159 // create proxy for root160 const rootProxy = createProxy(undefined, baseState)166- // execute the thunk161+ // execute the producer function162 const returnValue = producer.call(rootProxy, rootProxy)163 // and finalize the modified proxy164 let result165 // check whether the draft was modified and/or a value was returned1 unmodified line167 // something was returned, and it wasn't the proxy itself168 if (rootProxy[PROXY_STATE].modified)169 throw new Error(RETURNED_AND_MODIFIED_ERROR)170 176- // See #117177- // Should we just throw when returning a proxy which is not the root, but a subset of the original state?178- // Looks like a wrongly modeled reducer171+ // we need to finalize the return value in case it's a subset of the draft172 result = finalize(returnValue)173 if (patches) {174 patches.push({op: "replace", path: [], value: result})175 inversePatches.push({op: "replace", path: [], value: baseState})12 unmodified lines