Skip to content
Myr

nit: minor refactoring

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

authored by Alec Larson · committed by Alec Larson · parent ddf5dbb · Browse files · 2 files+32 added−36 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+32 added−36 removed

  • other:+32 added−36 removed

2 files+32 added−36 removed

modified:src/immer.js+17 added-18 removed
193 unmodified lines
194 if (this.onDelete && !isSet(state.base)) {195 // The `assigned` object is unreliable with ES5 drafts.196 if (this.useProxies) {197 const {assigned} = state198 each(assigned, (prop, assignedValue) => {199 if (!assignedValue) this.onDelete(state, prop)198 each(assigned, (prop, exists) => {199 if (!exists) this.onDelete(state, prop)200 })201 } else {202 // TODO: Figure it out for Maps and Sets if we need to support ES5203 const {base, copy} = state
50 unmodified lines
254 if (isDraft(value)) {255 const path =256 isDraftProp &&257 needPatches &&258 !isSetMember &&259 !has(state.assigned, prop)258 !isSetMember && // Set objects are atomic since they have no keys.259 !has(state.assigned, prop) // Skip deep patches for assigned keys.260 ? rootPath.concat(prop)261 : null262263 // Drafts owned by `scope` are finalized here.264 value = this.finalize(value, path, scope)265 replace(parent, prop, value)266267 // Drafts from another scope must prevent auto-freezing.268 if (isDraft(value)) {269 scope.canAutoFreeze = false270 }271271 setProperty(parent, prop, value)272272 // Unchanged drafts are never passed to the `onAssign` hook.273 if (isDraftProp && value === get(state.base, prop)) return274 }275 // Unchanged draft properties are ignored.
15 unmodified lines
291 return root292 }293}294296function setProperty(parent, prop, value) {297 // Preserve non-enumerable properties.298 if (Array.isArray(parent) || isEnumerable(parent, prop)) {299 parent[prop] = value300 return301 }295function replace(parent, prop, value) {296 if (isMap(parent)) {297 parent.set(prop, value)304 return305 }306 if (isSet(parent)) {307 // Here prop is a proxied value298 } else if (isSet(parent)) {299 // In this case, the `prop` is actually a draft.300 parent.delete(prop)301 parent.add(value)310 return302 } else if (Array.isArray(parent) || isEnumerable(parent, prop)) {303 // Preserve non-enumerable properties.304 parent[prop] = value305 } else {306 Object.defineProperty(parent, prop, {307 value,308 writable: true,309 configurable: true310 })311 }312 Object.defineProperty(parent, prop, {value})312}
modified:src/proxy.js+15 added-18 removed
47 unmodified lines
48 // Called by the `produce` function.49 revoke: null50 }5152 let proxyTarget = state52 let target = state53 let traps = objectTraps54 if (Array.isArray(base)) {55 proxyTarget = [state]55 target = [state]56 traps = arrayTraps57 }58 // Map drafts must support object keys, so we use Map objects to track changes.57 } else if (isMap(base)) {59 else if (isMap(base)) {60 traps = mapTraps59 // Maps need to support non-primitive keys and objects do not support other objects as keys61 state.drafts = new Map()62 state.assigned = new Map()63 }64 // Set drafts use a Map object to track which of its values are drafted.65 // And we don't need the "assigned" property, because Set objects have no keys.62 } else if (isSet(base)) {66 else if (isSet(base)) {67 traps = setTraps64 // We use values of a Set as keys and objects do not support other objects as keys68 state.drafts = new Map()66 // Sets do not need .assigned override since you cannot really assign (read as 'replace') any value in s Set. You can only add or remove values69 }7069 const {revoke, proxy} = Proxy.revocable(proxyTarget, traps)71 const {revoke, proxy} = Proxy.revocable(target, traps)7273 state.draft = proxy74 state.revoke = revoke75
180 unmodified lines
256})257258/** Map.prototype.values _-or-_ Map.prototype.entries */259function iterateMapValues(state, prop, receiver) {258 const getYieldable =259 prop === "values" ? (key, value) => value : (key, value) => [key, value]260260 const isEntries = prop !== "values"261 return () => {262 const iterator = latest(state)[Symbol.iterator]()263 return makeIterable(() => {264 const result = iterator.next()265 if (!result.done) {266 const [key] = result.value267 const value = receiver.get(key)268 result.value = getYieldable(key, value)268 result.value = isEntries ? [key, value] : value269 }270 return result271 })272 }
36 unmodified lines
309 [Symbol.iterator]: iterateSetValues310})311312function iterateSetValues(state, prop) {313 const isEntries = prop === "entries"314 return () => {315 const iterator = latest(state)[Symbol.iterator]()315316 return makeIterable(() => {317 const result = iterator.next()318 if (!result.done) {319 const valueWrapped = wrapSetValue(state, result.value)320 result.value = valueWrapped321 if (prop === "entries") {322 result.value = [valueWrapped, valueWrapped]323 }319 const value = wrapSetValue(state, result.value)320 result.value = isEntries ? [value, value] : value321 }322 return result323 })324 }
91 unmodified lines