Skip to content
Myr

Some minor cleanup

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 ebda6f8 · Browse files · 4 files+32 added−53 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+32 added−53 removed

  • other:+32 added−53 removed

4 files+32 added−53 removed

modified:src/plugins/es5.ts+13 added-4 removed
6 unmodified lines
7 each,8 has,9 isDraft,10 isDraftable,11 isEnumerable,11 shallowCopy,12 latest,14 createHiddenProperty,13 DRAFT_STATE,14 assertUnrevoked,15 is,16 loadPlugin,17 ImmerScope,18 createProxy,19 ProxyTypeES5Array,22 ProxyTypeES5Object20 ProxyTypeES5Object,21 AnyObject22} from "../internal"2324type ES5State = ES5ArrayState | ES5ObjectState25
48 unmodified lines
74 revoked_: false,75 isManual_: false76 }7779 createHiddenProperty(draft, DRAFT_STATE, state)78 Object.defineProperty(draft, DRAFT_STATE, {79 value: state,80 // enumerable: false <- the default81 writable: true82 })83 return draft84 }8586 // Access a property without creating an Immer draft.
207 unmodified lines
294 // For all other cases, we don't have to compare, as they would have been picked up by the index setters295 return false296 }297298 /*#__PURE__*/299 function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {300 const desc = Object.getOwnPropertyDescriptor(base, prop)301 return desc && desc.enumerable ? true : false302 }303304 loadPlugin("es5", {305 createES5Proxy_,306 markChangedES5_,307 willFinalizeES5_
2 unmodified lines
modified:src/common.ts+10 added-46 removed
9 unmodified lines
10 AnySet,11 ImmerState,12 hasMap,13 ArchtypeObject,14 ProxyTypeES5Object,15 ProxyTypeProxyObject,16 ProxyTypeES5Array,17 ProxyTypeProxyArray,14 ArchtypeArray,19 ProxyTypeMap,15 ArchtypeMap,21 ProxyTypeSet,16 ArchtypeSet17} from "./internal"18import invariant from "tiny-invariant"19
57 unmodified lines
77 }78}7980/*#__PURE__*/87export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {88 const desc = Object.getOwnPropertyDescriptor(base, prop)89 return desc && desc.enumerable ? true : false90}9192/*#__PURE__*/81export function getArchtype(thing: any): 0 | 1 | 2 | 3 {82 /* istanbul ignore next */95 if (thing[DRAFT_STATE]) {96 switch ((thing as Drafted)[DRAFT_STATE].type) {97 case ProxyTypeES5Object:98 case ProxyTypeProxyObject:99 return ArchtypeObject100 case ProxyTypeES5Array:101 case ProxyTypeProxyArray:102 return ArchtypeArray103 case ProxyTypeMap:104 return ArchtypeMap105 case ProxyTypeSet:106 return ArchtypeSet107 }108 }109 return Array.isArray(thing)83 const state: undefined | ImmerState = thing[DRAFT_STATE]84 return state85 ? state.type_ > 386 ? state.type_ - 4 // cause Object and Array map back from 4 and 587 : (state.type_ as any) // others are the same88 : Array.isArray(thing)89 ? ArchtypeArray90 : isMap(thing)91 ? ArchtypeMap92 : isSet(thing)
85 unmodified lines
178 return clone179}180181export function freeze(obj: any, deep: boolean): void {203 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return204 const type = getArchtype(obj)205 if (type === ArchtypeSet) {206 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any207 } else if (type === ArchtypeMap) {208 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any182 if (isDraft(obj) || Object.isFrozen(obj) || !isDraftable(obj)) return183 if (getArchtype(obj) > 1 /* Map or Set */) {184 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any185 }186 Object.freeze(obj)187 if (deep) each(obj, (_, value) => freeze(value, true))188}
1 unmodified line
190function dontMutateFrozenCollections() {191 invariant(false, "This object has been frozen and should not be mutated")192}193218// TODO: used only once, inline219export function createHiddenProperty(220 target: AnyObject,221 prop: PropertyKey,222 value: any223) {224 Object.defineProperty(target, prop, {225 value: value,226 // enumerable: false <- the default227 writable: true228 })229}230194// TODO: move to ES5 / Map195export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {196 invariant(197 !state.revoked_,198 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
3 unmodified lines
modified:notes.txt+8 added-2 removed
1Things to optimize23[x] extract errors4[x] don't enable all features by default5[ ] "undefined" "object" "function" prototype undefined + buildin FNs ?5[ ] "undefined" "object" "function" prototype undefined bind, getOwnPropertyies, isArray etc + buildin FNs ?6[x] kill hooks7[x] _ postfix all members8[x] clean up finalize9[x] remove die
6 unmodified lines
16[x] kill enumerations17[ ] check todos18[ ] kill marchChangesSweep recursive if possible and no perf impact19[ ] optimize each closures away? pass in additional args?20[ ] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue)20[x] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue) #46521[ ] put some invariants behind __DEV__22[x] check for plugin specific utils23[ ] bind util24[ ] eliminate switches2526Import size report for immer:27┌───────────────────────┬───────────┬────────────┬───────────┐28│ (index) │ just this │ cumulative │ increment │
79 unmodified lines
108(this report was generated by npmjs.com/package/import-size)109110Minimize plugin api's:111 3196112113add draft optimization1143199
modified:tsdx.config.js+1 added-1 removed