Skip to content
Myr

Tried to make stuff faster (doesn't really seem to help)

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 855603d · Browse files · 2 files+23 added−14 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+23 added−14 removed

  • tests:+2 added−0 removed
  • other:+21 added−14 removed

2 files+23 added−14 removed

modified:__tests__/base.js+2 added
31 unmodified lines
32 expect(warning).toHaveBeenCalledTimes(4)33 })3435 it("should return a copy when modifying stuff", () => {36 debugger37 const nextState = immer(baseState, s => {38 s.aProp = "hello world"39 })40 expect(nextState).not.toBe(baseState)
3 unmodified lines
44 expect(nextState.nested).toBe(baseState.nested)45 })4647 it("deep change bubbles up", () => {48 debugger49 const nextState = immer(baseState, s => {50 s.anObject.nested.yummie = false51 })52 expect(nextState).not.toBe(baseState)
197 unmodified lines
modified:immer-es5.js+21 added-14 removed
109 unmodified lines
110 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))111 return proxy112 }113114 // this sounds very expensive, but actually it is not that extensive in practice115 // as it will only visit proxies, and only do key-based change detection for objects for116 // which it is not already know that they are changed (that is, only object for which no known key was changed)114 function hasChanges(proxy) {117 function markChanges(proxy) {118 if (!isProxy(proxy)) return false116 if (proxy[CHANGED_STATE]) return true // some property was modified117 if (isPlainObject(proxy)) return objectHasChanges(proxy)118 if (Array.isArray(proxy)) return arrayHasChanges(proxy)119 if (isPlainObject(proxy)) return markObjectChanges(proxy)120 if (Array.isArray(proxy)) return markArrayChanges(proxy)121 }122121 function objectHasChanges(proxy) {123 function markObjectChanges(proxy) {124 const baseKeys = Object.keys(proxy[PROXY_TARGET])125 const keys = Object.keys(proxy)124 if (!shallowEqual(baseKeys, keys)) return true126 let hasChanges = proxy[CHANGED_STATE]127 // look deeper, this object was not modified, but maybe some of its children are128 for (let i = 0; i < keys.length; i++) {127 if (hasChanges(proxy[keys[i]])) return true129 if (markChanges(proxy[keys[i]])) hasChanges = true130 }129 return false131 // all children are the same, but maybe there are items added / removed132 if (!hasChanges) hasChanges = !shallowEqual(baseKeys, keys)133 return (proxy[CHANGED_STATE] = hasChanges)134 }135132 function arrayHasChanges(proxy) {136 function markArrayChanges(proxy) {137 const target = proxy[PROXY_TARGET]134 if (target.length !== proxy.length) return true135 // look deeper, this object was not modified, but maybe some of its children are138 let hasChanges = proxy[CHANGED_STATE]139 for (let i = 0; i < proxy.length; i++) {137 if (hasChanges(proxy[i])) return true140 if (markChanges(proxy[i])) hasChanges = true141 }139 return false142 // all children are the same, but maybe there are items added / removed143 if (!hasChanges) hasChanges = target.length !== proxy.length144 return (proxy[CHANGED_STATE] = hasChanges)145 }146147 function finalize(proxy) {148 // given a base object, returns it if unmodified, or return the changed cloned if modified149 if (!isProxy(proxy)) return proxy145 if (!hasChanges(proxy)) return proxy[PROXY_TARGET] // return the original target150 if (!proxy[CHANGED_STATE]) return proxy[PROXY_TARGET] // return the original target151 if (isPlainObject(proxy)) return finalizeObject(proxy)152 if (Array.isArray(proxy)) return finalizeArray(proxy)153 throw new Error("Illegal state")154 }
20 unmodified lines
175 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`176 )177 // and finalize the modified proxy178 finalizing = true179 // find and mark all changes (for parts not done yet)180 markChanges(rootClone)181 const res = finalize(rootClone)182 // make sure all proxies become unusable183 finished = true184 return res
67 unmodified lines