Skip to content
Myr

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

10 files+128 added−97 removed

modified:src/plugins/es5.ts+8 added-7 removed
10 unmodified lines
11 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"2425type ES5State = ES5ArrayState | ES5ObjectState26
34 unmodified lines
61 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))62 })6364 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 lines
179 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 lines
195 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 lines
220 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 lines
modified:src/plugins/mapset.ts+5 added-4 removed
4 unmodified lines
5 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"20import invariant from "tiny-invariant"2122export function enableMapSet() {
27 unmodified lines
50 __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 lines
206 __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 lines
modified:src/plugins/patches.ts+24 added-17 removed
11 unmodified lines
12 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"29import invariant from "tiny-invariant"3031export function enablePatches() {
3 unmodified lines
35 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 lines
206 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 modifyed
2 unmodified lines
220 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 lines
modified:src/common.ts+33 added-25 removed
7 unmodified lines
8 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"24import invariant from "tiny-invariant"2526/** Returns true if the given value is an Immer draft */
48 unmodified lines
75 obj: T,76 iter: (key: string | number, value: any, source: T) => void77): void78export 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 lines
89 return desc && desc.enumerable ? true : false90}9192/*#__PURE__*/85export function getArchtype(thing: any): Archtype {93export 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}117118/*#__PURE__*/119export 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}124125/*#__PURE__*/126export 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}130131/*#__PURE__*/132export 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 lines
201202export 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 lines
modified:src/finalize.ts+7 added-5 removed
modified:src/plugins.ts+8 added-5 removed
6 unmodified lines
7 ImmerBaseState,8 AnyArray,9 AnyMap,10 AnySet,11 ProxyType11 ProxyTypeES5Array,12 ProxyTypeES5Object,13 ProxyTypeMap,14 ProxyTypeSet15} from "./internal"1617/** Plugin utilities */18const plugins: {
45 unmodified lines
64 revoked_: boolean65}6667export interface ES5ObjectState extends ES5BaseState {65 type_: ProxyType.ES5Object68 type_: typeof ProxyTypeES5Object69 draft_: Drafted<AnyObject, ES5ObjectState>70 base_: AnyObject71 copy_: AnyObject | null72}7374export interface ES5ArrayState extends ES5BaseState {72 type_: ProxyType.ES5Array75 type_: typeof ProxyTypeES5Array76 draft_: Drafted<AnyObject, ES5ArrayState>77 base_: AnyArray78 copy_: AnyArray | null79}
19 unmodified lines
99100/** Map / Set plugin */101102export 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}110111export 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_: boolean
27 unmodified lines
modified:src/proxy.ts+9 added-8 removed
7 unmodified lines
8 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"21import invariant from "tiny-invariant"2223interface ProxyBaseState extends ImmerBaseState {
7 unmodified lines
31 revoke_(): void32}3334export interface ProxyObjectState extends ProxyBaseState {34 type_: ProxyType.ProxyObject35 type_: typeof ProxyTypeProxyObject36 base_: AnyObject37 copy_: AnyObject | null38 draft_: Drafted<AnyObject, ProxyObjectState>39}4041export interface ProxyArrayState extends ProxyBaseState {41 type_: ProxyType.ProxyArray42 type_: typeof ProxyTypeProxyArray43 base_: AnyArray44 copy_: AnyArray | null45 draft_: Drafted<AnyArray, ProxyArrayState>46}
10 unmodified lines
57 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 lines
181 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 lines
235export 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-ignore
16 unmodified lines
modified:src/scope.ts+7 added-10 removed
1import {Patch, PatchListener, Drafted, Immer, DRAFT_STATE} from "./internal"2import {2 Patch,3 PatchListener,4 Drafted,5 ProxyType,6 Immer,7 DRAFT_STATE8} from "./internal"9import {ImmerState} from "./types-internal"3 ImmerState,4 ProxyTypeProxyObject,5 ProxyTypeProxyArray6} from "./types-internal"78/** Each scope represents a `produce` call. */9// TODO: non-class?10export class ImmerScope {
47 unmodified lines
5859function 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 lines
15export type AnyObject = {[key: string]: any}16export type AnyArray = Array<any>17export type AnySet = Set<any>18export type AnyMap = Map<any, any>19export enum Archtype {20 Object,21 Array,22 Map,23 Set24}1926export enum ProxyType {27 ProxyObject,28 ProxyArray,29 ES5Object,30 ES5Array,31 Map,32 Set33}20export const ArchtypeObject = 021export const ArchtypeArray = 122export const ArchtypeMap = 223export const ArchtypeSet = 32425export const ProxyTypeProxyObject = 026export const ProxyTypeProxyArray = 127export const ProxyTypeES5Object = 428export const ProxyTypeES5Array = 529export const ProxyTypeMap = 230export const ProxyTypeSet = 33132export interface ImmerBaseState {33 parent_?: ImmerState34 scope_: ImmerScope35 modified_: boolean
16 unmodified lines
modified:notes.txt+16 added-2 removed
12 unmodified lines
13[ ] 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__2324Import size report for immer:25┌───────────────────────┬───────────┬────────────┬───────────┐26│ (index) │ just this │ cumulative │ increment │
62 unmodified lines
89│ enablePatches │ 4091 │ 5693 │ 692 │90└───────────────────────┴───────────┴────────────┴───────────┘91(this report was generated by npmjs.com/package/import-size)92]9394Killed enumerations: 9596Import 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)