Skip to content
Myr

Preserve object identify in ES5 impl

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 8b6d3ea · Browse files · 2 files+26 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−3 removed

  • tests:+20 added−0 removed
  • other:+6 added−3 removed

2 files+26 added−3 removed

modified:__tests__/base.js+20 added
610 unmodified lines
611 expect(Object.isFrozen(next.x.y)).toBe(true)612 expect(Object.isFrozen(next.x.y[0].z)).toBe(true)613 })614615 it("should structurally share identical objects in the tree", () => {616 const base = {bear: {legs: 4}, eagle: {legs: 3}}617 const next = produce(base, draft => {618 const animal = draft.bear619 animal.legs = animal.legs + 1620 draft.bear = animal621 draft.eagle = animal622 draft.cow = animal623 draft.kiddo = animal624 })625 expect(next).toEqual({626 bear: {legs: 5},627 eagle: {legs: 5},628 cow: {legs: 5},629 kiddo: {legs: 5}630 })631 expect(next.bear).toBe(next.cow)632 expect(next.kiddo).toBe(next.cow)633 })634635 afterEach(() => {636 expect(baseState).toBe(origBaseState)637 expect(baseState).toEqual(createBaseState())638 })
21 unmodified lines
modified:src/es5.js+6 added-3 removed
23 unmodified lines
24 base,25 proxy,26 copy: undefined,27 finished: false,28 finalizing: false28 finalizing: false,29 finalized: false30 }31}3233function source(state) {
114 unmodified lines
148 // TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup149 if (isProxy(proxy)) {150 const state = proxy[PROXY_STATE]151 if (state.modified === true) {152 if (state.finalized === true) return state.copy153 state.finalized = true154 if (Array.isArray(state.base)) return finalizeArray(proxy, state)155 return finalizeObject(proxy, state)156 } else return state.base157 } else if (proxy !== null && typeof proxy === "object") {
2 unmodified lines
160 return proxy161}162163function finalizeObject(proxy, state) {161 const res = Object.assign({}, proxy)164 const res = (state.copy = shallowCopy(proxy))165 const base = state.base166 each(res, (prop, value) => {167 if (value !== base[prop]) res[prop] = finalize(value)168 })169 return freeze(res)170}171172function finalizeArray(proxy, state) {170 const res = proxy.slice()173 const res = (state.copy = shallowCopy(proxy))174 const base = state.base175 each(res, (i, value) => {176 if (value !== base[i]) res[i] = finalize(value)177 })
61 unmodified lines