Killed enumerations -145 (!)
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 530d81b · Browse files · 10 files·+128 added−97 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+128 added−97 removed
- other:+128 added−97 removed
Provenance
- Commit
fdc78db- Tree
0eb3be0- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
530d81b
10 files changed+128 added−97 removed
modified:src/plugins/es5.ts+8 added-7 removed
10 unmodified lines11 isEnumerable,12 shallowCopy,13 latest,14 createHiddenProperty,15- ProxyType,15 DRAFT_STATE,16 assertUnrevoked,17 is,18 loadPlugin,19 ImmerScope,21- createProxy20+ createProxy,21+ ProxyTypeES5Array,22+ ProxyTypeES5Object23 } from "../internal"24 25 type ES5State = ES5ArrayState | ES5ObjectState26 34 unmodified lines61 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))62 })63 64 const state: ES5ObjectState | ES5ArrayState = {64- type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),65+ type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),66 scope_: parent ? parent.scope_ : ImmerScope.current_!,67 modified_: false,68 finalizing_: false,69 finalized_: false,109 unmodified lines179 for (let i = drafts.length - 1; i >= 0; i--) {180 const state: ES5State = drafts[i][DRAFT_STATE]181 if (!state.modified_) {182 switch (state.type_) {182- case ProxyType.ES5Array:183+ case ProxyTypeES5Array:184 if (hasArrayChanges(state)) markChangedES5(state)185 break185- case ProxyType.ES5Object:186+ case ProxyTypeES5Object:187 if (hasObjectChanges(state)) markChangedES5(state)188 break189 }190 }4 unmodified lines195 if (!object || typeof object !== "object") return196 const state: ES5State | undefined = object[DRAFT_STATE]197 if (!state) return198 const {base_, draft_, assigned_, type_} = state198- if (type_ === ProxyType.ES5Object) {199+ if (type_ === ProxyTypeES5Object) {200 // Look for added keys.201 // TODO: looks quite duplicate to hasObjectChanges,202 // probably there is a faster way to detect changes, as sweep + recurse seems to do some203 // unnecessary work.16 unmodified lines220 assigned_[key] = false221 markChangedES5(state)222 }223 })223- } else if (type_ === ProxyType.ES5Array) {224+ } else if (type_ === ProxyTypeES5Array) {225 if (hasArrayChanges(state as ES5ArrayState)) {226 markChangedES5(state)227 assigned_.length = true228 }72 unmodified linesmodified:src/plugins/mapset.ts+5 added-4 removed
4 unmodified lines5 AnySet,6 MapState,7 SetState,8 DRAFT_STATE,9- ProxyType,9 ImmerScope,10 latest,11 assertUnrevoked,12 iteratorSymbol,13 isDraftable,14 createProxy,15 loadPlugin,17- markChanged16+ markChanged,17+ ProxyTypeMap,18+ ProxyTypeSet19 } from "../internal"20 import invariant from "tiny-invariant"21 22 export function enableMapSet() {27 unmodified lines50 __extends(DraftMap, _super)51 // Create class manually, cause #50252 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {53 this[DRAFT_STATE] = {53- type_: ProxyType.Map,54+ type_: ProxyTypeMap,55 parent_: parent,56 scope_: parent ? parent.scope_ : ImmerScope.current_!,57 modified_: false,58 finalized_: false,147 unmodified lines206 __extends(DraftSet, _super)207 // Create class manually, cause #502208 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {209 this[DRAFT_STATE] = {209- type_: ProxyType.Set,210+ type_: ProxyTypeSet,211 parent_: parent,212 scope_: parent ? parent.scope_ : ImmerScope.current_!,213 modified_: false,214 finalized_: false,126 unmodified linesmodified:src/plugins/patches.ts+24 added-17 removed
11 unmodified lines12 get,13 each,14 has,15 getArchtype,16- ProxyType,17- Archtype,16 isSet,17 isMap,20- loadPlugin18+ loadPlugin,19+ ProxyTypeProxyObject,20+ ProxyTypeES5Object,21+ ProxyTypeMap,22+ ProxyTypeES5Array,23+ ProxyTypeProxyArray,24+ ProxyTypeSet,25+ ArchtypeMap,26+ ArchtypeSet,27+ ArchtypeArray28 } from "../internal"29 import invariant from "tiny-invariant"30 31 export function enablePatches() {3 unmodified lines35 patches: Patch[],36 inversePatches: Patch[]37 ): void {38 switch (state.type_) {32- case ProxyType.ProxyObject:33- case ProxyType.ES5Object:34- case ProxyType.Map:39+ case ProxyTypeProxyObject:40+ case ProxyTypeES5Object:41+ case ProxyTypeMap:42 return generatePatchesFromAssigned(43 state,44 basePath,45 patches,46 inversePatches47 )41- case ProxyType.ES5Array:42- case ProxyType.ProxyArray:48+ case ProxyTypeES5Array:49+ case ProxyTypeProxyArray:50 return generateArrayPatches(state, basePath, patches, inversePatches)44- case ProxyType.Set:51+ case ProxyTypeSet:52 return generateSetPatches(53 (state as any) as SetState,54 basePath,55 patches,150 unmodified lines206 const key = path[path.length - 1]207 switch (op) {208 case "replace":209 switch (type) {203- case Archtype.Map:210+ case ArchtypeMap:211 return base.set(key, value)212 /* istanbul ignore next */206- case Archtype.Set:213+ case ArchtypeSet:214 invariant(false, 'Sets cannot have "replace" patches.')215 default:216 // if value is an object, then it's assigned by reference217 // in the following add or remove ops, the value field inside the patch will also be modifyed2 unmodified lines220 return (base[key] = value)221 }222 case "add":223 switch (type) {217- case Archtype.Array:224+ case ArchtypeArray:225 return base.splice(key as any, 0, value)219- case Archtype.Map:226+ case ArchtypeMap:227 return base.set(key, value)221- case Archtype.Set:228+ case ArchtypeSet:229 return base.add(value)230 default:231 return (base[key] = value)232 }233 case "remove":234 switch (type) {228- case Archtype.Array:235+ case ArchtypeArray:236 return base.splice(key as any, 1)230- case Archtype.Map:237+ case ArchtypeMap:238 return base.delete(key)232- case Archtype.Set:239+ case ArchtypeSet:240 return base.delete(patch.value)241 default:242 return delete base[key]243 }27 unmodified linesmodified:src/common.ts+33 added-25 removed
7 unmodified lines8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12- ProxyType,13- Archtype,14- hasMap12+ hasMap,13+ ArchtypeObject,14+ ProxyTypeES5Object,15+ ProxyTypeProxyObject,16+ ProxyTypeES5Array,17+ ProxyTypeProxyArray,18+ ArchtypeArray,19+ ProxyTypeMap,20+ ArchtypeMap,21+ ProxyTypeSet,22+ ArchtypeSet23 } from "./internal"24 import invariant from "tiny-invariant"25 26 /** Returns true if the given value is an Immer draft */48 unmodified lines75 obj: T,76 iter: (key: string | number, value: any, source: T) => void77 ): void78 export function each(obj: any, iter: any) {71- if (getArchtype(obj) === Archtype.Object) {79+ if (getArchtype(obj) === ArchtypeObject) {80 ownKeys(obj).forEach(key => iter(key, obj[key], obj))81 } else {82 obj.forEach((entry: any, index: any) => iter(index, entry, obj))83 }5 unmodified lines89 return desc && desc.enumerable ? true : false90 }91 92 /*#__PURE__*/85-export function getArchtype(thing: any): Archtype {93+export function getArchtype(thing: any): 0 | 1 | 2 | 3 {94 /* istanbul ignore next */95 if (thing[DRAFT_STATE]) {96 switch ((thing as Drafted)[DRAFT_STATE].type) {89- case ProxyType.ES5Object:90- case ProxyType.ProxyObject:91- return Archtype.Object92- case ProxyType.ES5Array:93- case ProxyType.ProxyArray:94- return Archtype.Array95- case ProxyType.Map:96- return Archtype.Map97- case ProxyType.Set:98- return Archtype.Set97+ 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)102- ? Archtype.Array110+ ? ArchtypeArray111 : isMap(thing)104- ? Archtype.Map112+ ? ArchtypeMap113 : isSet(thing)106- ? Archtype.Set107- : Archtype.Object114+ ? ArchtypeSet115+ : ArchtypeObject116 }117 118 /*#__PURE__*/119 export function has(thing: any, prop: PropertyKey): boolean {112- return getArchtype(thing) === Archtype.Map120+ return getArchtype(thing) === ArchtypeMap121 ? thing.has(prop)122 : Object.prototype.hasOwnProperty.call(thing, prop)123 }124 125 /*#__PURE__*/126 export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {127 // @ts-ignore120- return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]128+ return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]129 }130 131 /*#__PURE__*/132 export function set(thing: any, propOrOldValue: PropertyKey, value: any) {133 switch (getArchtype(thing)) {126- case Archtype.Map:134+ case ArchtypeMap:135 thing.set(propOrOldValue, value)136 break129- case Archtype.Set:137+ case ArchtypeSet:138 thing.delete(propOrOldValue)139 thing.add(value)140 break141 default:59 unmodified lines201 202 export function freeze(obj: any, deep: boolean): void {203 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return204 const type = getArchtype(obj)197- if (type === Archtype.Set) {205+ if (type === ArchtypeSet) {206 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any199- } else if (type === Archtype.Map) {207+ } else if (type === ArchtypeMap) {208 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any209 }210 Object.freeze(obj)211 if (deep) each(obj, (_, value) => freeze(value, true))26 unmodified linesmodified:src/finalize.ts+7 added-5 removed
2 unmodified lines34567789103 unmodified lines1415161719181920212223242556 unmodified lines8283848584868788899026 unmodified lines11711811912011912112212312412517 unmodified lines1431441451461451471481491501512 unmodified lines DRAFT_STATE, isDraftable, NOTHING, PatchPath, ProxyType, each, has, freeze, generatePatches,3 unmodified lines SetState, set, is, get, willFinalize willFinalize, ProxyTypeES5Object, ProxyTypeES5Array, ProxyTypeSet} from "./internal"import invariant from "tiny-invariant"
export function processResult(result: any, scope: ImmerScope) {56 unmodified lines if (!state.finalized_) { state.finalized_ = true const result = // For ES5, create a good copy from the draft first, with added keys and without deleted keys. state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array ? (state.copy_ = shallowCopy(state.draft_, true)) : state.copy_ // finalize all children of the copy each(result as any, (key, childValue) =>26 unmodified lines if (isDraft(childValue)) { const path = rootPath && parentState && parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys. parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys. !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys. ? rootPath!.concat(prop) : undefined // Drafts owned by `scope` are finalized here.17 unmodified lines maybeFreeze(rootScope, childValue) }}
export function maybeFreeze(scope: ImmerScope, value: any, deep = false) {function maybeFreeze(scope: ImmerScope, value: any, deep = false) { if (scope.immer_.autoFreeze_ && scope.canAutoFreeze_) { freeze(value, deep) }}modified:src/plugins.ts+8 added-5 removed
6 unmodified lines7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11- ProxyType11+ ProxyTypeES5Array,12+ ProxyTypeES5Object,13+ ProxyTypeMap,14+ ProxyTypeSet15 } from "./internal"16 17 /** Plugin utilities */18 const plugins: {45 unmodified lines64 revoked_: boolean65 }66 67 export interface ES5ObjectState extends ES5BaseState {65- type_: ProxyType.ES5Object68+ type_: typeof ProxyTypeES5Object69 draft_: Drafted<AnyObject, ES5ObjectState>70 base_: AnyObject71 copy_: AnyObject | null72 }73 74 export interface ES5ArrayState extends ES5BaseState {72- type_: ProxyType.ES5Array75+ type_: typeof ProxyTypeES5Array76 draft_: Drafted<AnyObject, ES5ArrayState>77 base_: AnyArray78 copy_: AnyArray | null79 }19 unmodified lines99 100 /** Map / Set plugin */101 102 export interface MapState extends ImmerBaseState {100- type_: ProxyType.Map103+ type_: typeof ProxyTypeMap104 copy_: AnyMap | undefined105 assigned_: Map<any, boolean> | undefined106 base_: AnyMap107 revoked_: boolean108 draft_: Drafted<AnyMap, MapState>109 }110 111 export interface SetState extends ImmerBaseState {109- type_: ProxyType.Set112+ type_: typeof ProxyTypeSet113 copy_: AnySet | undefined114 base_: AnySet115 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set116 revoked_: boolean27 unmodified linesmodified:src/proxy.ts+9 added-8 removed
7 unmodified lines8 latest,9 ImmerBaseState,10 ImmerState,11 Drafted,12- ProxyType,12 AnyObject,13 AnyArray,14 Objectish,15 ImmerScope,16 DRAFT_STATE,18- createProxy17+ createProxy,18+ ProxyTypeProxyObject,19+ ProxyTypeProxyArray20 } from "./internal"21 import invariant from "tiny-invariant"22 23 interface ProxyBaseState extends ImmerBaseState {7 unmodified lines31 revoke_(): void32 }33 34 export interface ProxyObjectState extends ProxyBaseState {34- type_: ProxyType.ProxyObject35+ type_: typeof ProxyTypeProxyObject36 base_: AnyObject37 copy_: AnyObject | null38 draft_: Drafted<AnyObject, ProxyObjectState>39 }40 41 export interface ProxyArrayState extends ProxyBaseState {41- type_: ProxyType.ProxyArray42+ type_: typeof ProxyTypeProxyArray43 base_: AnyArray44 copy_: AnyArray | null45 draft_: Drafted<AnyArray, ProxyArrayState>46 }10 unmodified lines57 parent?: ImmerState58 ): Drafted<T, ProxyState> {59 const isArray = Array.isArray(base)60 const state: ProxyState = {60- type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),61+ type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),62 // Track which produce call this is associated with.63 scope_: parent ? parent.scope_ : ImmerScope.current_!,64 // True for both shallow and deep changes.65 modified_: false,115 unmodified lines181 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)182 if (desc) {183 desc.writable = true184 desc.configurable =184- state.type_ !== ProxyType.ProxyArray || prop !== "length"185+ state.type_ !== ProxyTypeProxyArray || prop !== "length"186 }187 return desc188 },189 defineProperty() {45 unmodified lines235 export function markChangedProxy(state: ImmerState) {236 if (!state.modified_) {237 state.modified_ = true238 if (238- state.type_ === ProxyType.ProxyObject ||239- state.type_ === ProxyType.ProxyArray239+ state.type_ === ProxyTypeProxyObject ||240+ state.type_ === ProxyTypeProxyArray241 ) {242 const copy = (state.copy_ = shallowCopy(state.base_))243 each(state.drafts_!, (key, value) => {244 // @ts-ignore16 unmodified linesmodified:src/scope.ts+7 added-10 removed
1+import {Patch, PatchListener, Drafted, Immer, DRAFT_STATE} from "./internal"2 import {2- Patch,3- PatchListener,4- Drafted,5- ProxyType,6- Immer,7- DRAFT_STATE8-} from "./internal"9-import {ImmerState} from "./types-internal"3+ ImmerState,4+ ProxyTypeProxyObject,5+ ProxyTypeProxyArray6+} from "./types-internal"7 8 /** Each scope represents a `produce` call. */9 // TODO: non-class?10 export class ImmerScope {47 unmodified lines58 59 function revoke(draft: Drafted) {60 const state: ImmerState = draft[DRAFT_STATE]61 if (65- state.type_ === ProxyType.ProxyObject ||66- state.type_ === ProxyType.ProxyArray62+ state.type_ === ProxyTypeProxyObject ||63+ state.type_ === ProxyTypeProxyArray64 )65 state.revoke_()66 else state.revoked_ = true67 }modified:src/types-internal.ts+11 added-14 removed
14 unmodified lines15 export type AnyObject = {[key: string]: any}16 export type AnyArray = Array<any>17 export type AnySet = Set<any>18 export type AnyMap = Map<any, any>19-export enum Archtype {20- Object,21- Array,22- Map,23- Set24-}19 26-export enum ProxyType {27- ProxyObject,28- ProxyArray,29- ES5Object,30- ES5Array,31- Map,32- Set33-}20+export const ArchtypeObject = 021+export const ArchtypeArray = 122+export const ArchtypeMap = 223+export const ArchtypeSet = 324 25+export const ProxyTypeProxyObject = 026+export const ProxyTypeProxyArray = 127+export const ProxyTypeES5Object = 428+export const ProxyTypeES5Array = 529+export const ProxyTypeMap = 230+export const ProxyTypeSet = 331+32 export interface ImmerBaseState {33 parent_?: ImmerState34 scope_: ImmerScope35 modified_: boolean16 unmodified linesmodified:notes.txt+16 added-2 removed
12 unmodified lines13 [ ] plugin tests14 [ ] check if we can create more local vars15 [ ] verify performance16 [ ] kill ownKeys?17-[ ] kill isEnumerable?17+[x] kill enumerations18 [ ] check todos19 [ ] kill marchChangesSweep recursive if possible and no perf impact20-[ ] todo try immer perf on really big tree20 [ ] optimize each closures away? pass in additional args?21 [ ] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue)22+[ ] put some invariants behind __DEV__23 24 Import size report for immer:25 ┌───────────────────────┬───────────┬────────────┬───────────┐26 │ (index) │ just this │ cumulative │ increment │62 unmodified lines89 │ enablePatches │ 4091 │ 5693 │ 692 │90 └───────────────────────┴───────────┴────────────┴───────────┘91 (this report was generated by npmjs.com/package/import-size)92 ]93+94+Killed enumerations: 95+96+Import size report for immer:97+┌───────────────────────┬───────────┬────────────┬───────────┐98+│ (index) │ just this │ cumulative │ increment │99+├───────────────────────┼───────────┼────────────┼───────────┤100+│ import * from 'immer' │ 5750 │ 0 │ 0 │101+│ produce │ 3252 │ 3252 │ 0 │102+│ enableES5 │ 4046 │ 4055 │ 803 │103+│ enableMapSet │ 4071 │ 4847 │ 792 │104+│ enablePatches │ 3936 │ 5528 │ 681 │105+└───────────────────────┴───────────┴────────────┴───────────┘106+(this report was generated by npmjs.com/package/import-size)