Removed some duplicate code
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 4ad622f · Browse files · 3 files·+6 added−49 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+6 added−49 removed
- other:+6 added−49 removed
Provenance
- Commit
ee544cc- Tree
1902a7e- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
4ad622f
3 files changed+6 added−49 removed
modified:src/common.js+2 added-12 removed
1234567812345659 unmodified lines66676869767778797071727337 unmodified linesimport { finalizeArray as finalizeArrayProxy, finalizeObject as finalizeObjectProxy} from "./proxy"import { finalizeArray as finalizeArrayEs5, finalizeObject as finalizeObjectEs5} from "./es5"import {finalizeObject as finalizeObjectProxy} from "./proxy"import {finalizeObject as finalizeObjectEs5} from "./es5"
export const PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state")59 unmodified lines const state = base[PROXY_STATE] if (state.modified === true) { if (state.finalized === true) return state.copy state.finalized = true if (Array.isArray(state.base)) return useProxies ? finalizeArrayProxy(state) : finalizeArrayEs5(base, state) return useProxies ? finalizeObjectProxy(state) : finalizeObjectEs5(base, state) } else {37 unmodified linesmodified:src/es5.js+4 added-29 removed
69 unmodified lines70 }71 72 // creates a proxy for plain objects / arrays73 function createProxy(parent, base) {74- const proxy = Array.isArray(base)75- ? createArrayProxy(base)76- : createObjectProxy(base)74+ const proxy = shallowCopy(base)75+ each(base, i => {76+ Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))77+ })78 const state = createState(parent, proxy, base)79 createHiddenProperty(proxy, PROXY_STATE, state)80 states.push(state)81 return proxy14 unmodified lines96 })97 )98 }99 99-function createObjectProxy(base) {100- const proxy = Object.assign({}, base)101- each(base, prop => {102- Object.defineProperty(proxy, prop, createPropertyProxy(prop))103- })104- return proxy105-}106-107-function createArrayProxy(base) {108- const proxy = new Array(base.length)109- each(base, i => {110- Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))111- })112- return proxy113-}114-100 function assertUnfinished(state) {101 if (state.finished === true)102 throw new Error(103 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?"35 unmodified lines139 })140 return freeze(res)141 }142 158-// TODO: unduplicate159-export function finalizeArray(proxy, state) {160- const res = (state.copy = shallowCopy(proxy))161- const base = state.base162- each(res, (i, value) => {163- if (value !== base[i]) res[i] = finalize(value)164- })165- return freeze(res)166-}167-143 export function produceEs5(baseState, producer) {144 const prevStates = states145 states = []146 try {54 unmodified linesmodified:src/proxy.js-8 removed
136 unmodified lines137 })138 return freeze(copy)139 }140 141-export function finalizeArray(state) {142- const copy = state.copy143- each(copy, (i, value) => {144- if (state.base[i] !== value) copy[i] = finalize(value)145- })146- return freeze(copy)147-}148-141 export function produceProxy(baseState, producer) {142 const previousProxies = proxies143 proxies = []144 try {13 unmodified lines