Merge branch 'iruca3-fix/finalize'
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 · parents 74c1fec, 29262e3 · Browse files · 4 files·+81 added−91 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+81 added−91 removed
- tests:+50 added−0 removed
- other:+31 added−91 removed
Provenance
- Commit
425f092- Tree
e8e7609- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent 1
74c1fec- Parent 2
29262e3
4 files changed+81 added−91 removed
modified:__tests__/base.js+50 added
504 unmodified lines505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508 509+ it("should not throw error #78", () => {510+ const base = {511+ arr: [512+ {513+ id: "100",514+ arr: [{id: "1", no: 1}, {id: "2", no: 2}]515+ },516+ {id: "3", no: 3}517+ ]518+ }519+ const result = produce(base, draft => {520+ const base = draft.arr[0]521+ draft.arr = draft.arr.slice(0, 1)522+ const newArr = {523+ id: "101",524+ arr: [base, {id: "3"}]525+ }526+ draft.arr.splice(0, 1, newArr)527+ draft.arr[0].arr[1].no = 3528+ })529+ expect(result).toEqual({530+ arr: [531+ {532+ id: "101",533+ arr: [534+ {535+ id: "100",536+ arr: [{id: "1", no: 1}, {id: "2", no: 2}]537+ },538+ {id: "3", no: 3}539+ ]540+ }541+ ]542+ })543+ })544+545+ it("should not throw error #78 - 2", () => {546+ const base = {547+ arr: [{count: 1}, {count: 2}]548+ }549+ const result = produce(base, draft => {550+ debugger551+ draft.arr = [draft.arr[1]]552+ draft.arr[0].count = 1553+ })554+ expect(result).toEqual({555+ arr: [{count: 1}]556+ })557+ })558+559 it("should handle dates correctly", () => {560 const data = {date: new Date()}561 const next = produce(data, draft => {562 draft.x = true176 unmodified linesmodified:src/common.js+21 added-34 removed
123456789123458 unmodified lines6364656676777879808182838485676869707172737475767778798081828384858687888990919596979899100101929394959610210310410510610710810997989910016 unmodified linesimport { finalizeArray as finalizeArrayProxy, finalizeObject as finalizeObjectProxy} from "./proxy"import { finalizeArray as finalizeArrayEs5, finalizeObject as finalizeObjectEs5} from "./es5"
export const PROXY_STATE = typeof Symbol !== "undefined" ? Symbol("immer-proxy-state") : "__$immer_state"58 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 return state.base } else if (base !== null && typeof base === "object") { finalizeNonProxiedObject(base) return finalizeObject( useProxies ? state.copy : (state.copy = shallowCopy(base)), state ) } else { return state.base } } finalizeNonProxiedObject(base) return base}
function finalizeObject(copy, state) { const base = state.base each(copy, (prop, value) => { if (value !== base[prop]) copy[prop] = finalize(value) }) return freeze(copy)}
function finalizeNonProxiedObject(parent) { // 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)) return if (Object.isFrozen(parent)) return if (Array.isArray(parent)) { for (let i = 0; i < parent.length; i++) { const child = parent[i] if (isProxy(child)) { parent[i] = finalize(child) } else finalizeNonProxiedObject(child) } each(parent, (i, child) => { if (isProxy(child)) { parent[i] = finalize(child) } else finalizeNonProxiedObject(child) }) } else { for (let key in parent) { const child = parent[key] if (isProxy(child)) { parent[key] = finalize(child) } else finalizeNonProxiedObject(child) } } // always freeze completely new data freeze(parent)}
16 unmodified linesmodified:src/es5.js+7 added-38 removed
35 unmodified lines36 37 function get(state, prop) {38 assertUnfinished(state)39 const value = source(state)[prop]40- if (!state.finalizing && !isProxy(value) && isProxyable(value)) {40+ if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {41+ // only create a proxy if the value is proxyable, and the value was in the base state42+ // if it wasn't in the base state, the object is already modified and we will process it in finalize43 prepareCopy(state)44 return (state.copy[prop] = createProxy(state, value))45 }46 return value23 unmodified lines70 }71 72 // creates a proxy for plain objects / arrays73 function createProxy(parent, base) {72- const proxy = Array.isArray(base)73- ? createArrayProxy(base)74- : 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 97-function createObjectProxy(base) {98- const proxy = Object.assign({}, base)99- each(base, prop => {100- Object.defineProperty(proxy, prop, createPropertyProxy(prop))101- })102- return proxy103-}104-105-function createArrayProxy(base) {106- const proxy = new Array(base.length)107- each(base, i => {108- Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))109- })110- return proxy111-}112-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?"26 unmodified lines130 function hasArrayChanges(state) {131 return state.proxy.length !== state.base.length132 }133 147-export function finalizeObject(proxy, state) {148- const res = (state.copy = shallowCopy(proxy))149- const base = state.base150- each(res, (prop, value) => {151- if (value !== base[prop]) res[prop] = finalize(value)152- })153- return freeze(res)154-}155-156-export function finalizeArray(proxy, state) {157- const res = (state.copy = shallowCopy(proxy))158- const base = state.base159- each(res, (i, value) => {160- if (value !== base[i]) res[i] = finalize(value)161- })162- return freeze(res)163-}164-134 export function produceEs5(baseState, producer) {135 const prevStates = states136 states = []137 try {54 unmodified linesmodified:src/proxy.js+3 added-19 removed
57 unmodified lines58 function get(state, prop) {59 if (prop === PROXY_STATE) return state60 if (state.modified) {61 const value = state.copy[prop]62- if (!isProxy(value) && isProxyable(value))62+ if (value === state.base[prop] && isProxyable(value))63+ // only create proxy if it is not yet a proxy, and not a new object64+ // (new objects don't need proxying, they will be processed in finalize anyway)65 return (state.copy[prop] = createProxy(state, value))66 return value67 } else {68 if (prop in state.proxies) return state.proxies[prop]58 unmodified lines127 proxies.push(proxy)128 return proxy.proxy129 }130 129-export function finalizeObject(state) {130- const copy = state.copy131- const base = state.base132- each(copy, (prop, value) => {133- if (value !== base[prop]) copy[prop] = finalize(value)134- })135- return freeze(copy)136-}137-138-export function finalizeArray(state) {139- const copy = state.copy140- const base = state.base141- each(copy, (i, value) => {142- if (value !== base[i]) copy[i] = finalize(value)143- })144- return freeze(copy)145-}146-131 export function produceProxy(baseState, producer) {132 const previousProxies = proxies133 proxies = []134 try {13 unmodified lines