Skip to content
Myr

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

4 files+81 added−91 removed

modified:__tests__/base.js+50 added
504 unmodified lines
505 room: {elephant: {kiddo: {age: 10, legs: 5}}}506 })507 })508509 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 })544545 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 })558559 it("should handle dates correctly", () => {560 const data = {date: new Date()}561 const next = produce(data, draft => {562 draft.x = true
176 unmodified lines
modified:src/common.js+21 added-34 removed
modified:src/es5.js+7 added-38 removed
35 unmodified lines
3637function 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 value
23 unmodified lines
70}7172// creates a proxy for plain objects / arrays73function 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 proxy
14 unmodified lines
96 })97 )98}9997function createObjectProxy(base) {98 const proxy = Object.assign({}, base)99 each(base, prop => {100 Object.defineProperty(proxy, prop, createPropertyProxy(prop))101 })102 return proxy103}104105function createArrayProxy(base) {106 const proxy = new Array(base.length)107 each(base, i => {108 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))109 })110 return proxy111}112100function 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 lines
130function hasArrayChanges(state) {131 return state.proxy.length !== state.base.length132}133147export 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}155156export 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}164134export function produceEs5(baseState, producer) {135 const prevStates = states136 states = []137 try {
54 unmodified lines
modified:src/proxy.js+3 added-19 removed
57 unmodified lines
58function 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 lines
127 proxies.push(proxy)128 return proxy.proxy129}130129export 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}137138export 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}146131export function produceProxy(baseState, producer) {132 const previousProxies = proxies133 proxies = []134 try {
13 unmodified lines