Skip to content
Myr

Optimized property creation

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 93d25d3 · Browse files · 2 files+36 added−30 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+36 added−30 removed

  • tests:+1 added−0 removed
  • other:+35 added−30 removed

2 files+36 added−30 removed

modified:__tests__/base.js+1 added
8 unmodified lines
9 origBaseState = baseState = createBaseState()10 })1112 it("should return the original without modifications", () => {13 debugger14 const nextState = immer(baseState, () => {})15 expect(nextState).toBe(baseState)16 })17
231 unmodified lines
modified:immer-es5.js+35 added-30 removed
24 unmodified lines
25 */26function immer(baseState, thunk) {27 let finalizing = false28 let finished = false29 const descriptors = {}3031 // creates a proxy for plain objects / arrays32 function createProxy(base) {33 if (isPlainObject(base)) return createObjectProxy(base)34 if (Array.isArray(base)) return createArrayProxy(base)34 throw "Expected a plain object or array"35 throw new Error("Expected a plain object or array")36 }3738 function assertUnfinished() {39 if (finished)
13 unmodified lines
53 value: value,54 })55 }5656 function createPropertyProxy(proxy, base, prop) {57 // TODO: optimize, reuse property accessors, closures etc58 Object.defineProperty(proxy, prop, {59 configurable: true,60 enumerable: true,61 get() {62 assertUnfinished()63 if (finalizing) return base[prop]64 const value = base[prop]65 if (!isPlainObject(value) && !Array.isArray(value)) return value66 const proxy = createProxy(value)67 Object.defineProperty(this, prop, {68 configurable: true,69 enumerable: true,70 get() {71 return proxy72 },73 set(value) {74 proxySet(this, prop, value)75 },76 })77 return proxy78 },79 set(value) {80 proxySet(this, prop, value)81 },82 })57 function createPropertyProxy(prop) {58 return (59 descriptors[prop] ||60 (descriptors[prop] = {61 configurable: true,62 enumerable: true,63 get() {64 assertUnfinished()65 const target = this[PROXY_TARGET]66 if (finalizing) return target[prop]67 const value = target[prop]68 if (!isPlainObject(value) && !Array.isArray(value))69 return value70 const proxy = createProxy(value)71 Object.defineProperty(this, prop, {72 configurable: true,73 enumerable: true,74 get() {75 return proxy76 },77 set(value) {78 proxySet(this, prop, value)79 },80 })81 return proxy82 },83 set(value) {84 proxySet(this, prop, value)85 },86 })87 )88 }8990 function createObjectProxy(base) {91 const proxy = {}92 createHiddenProperty(proxy, PROXY_TARGET, base)93 createHiddenProperty(proxy, CHANGED_STATE, false)94 Object.keys(base).forEach(prop =>90 createPropertyProxy(proxy, base, prop),95 Object.defineProperty(proxy, prop, createPropertyProxy(prop)),96 )97 return proxy98 }99
1 unmodified line
101 const proxy = []102 createHiddenProperty(proxy, PROXY_TARGET, base)103 createHiddenProperty(proxy, CHANGED_STATE, false)104 for (let i = 0; i < base.length; i++)100 createPropertyProxy(proxy, base, "" + i)105 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))106 return proxy107 }108109 // checks if the given base object has modifications, either because it is modified, or
152 unmodified lines