Skip to content
Myr

chore: reduce size by using TS const enum (#812)

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

authored by houfeng · committed by GitHub · parent 0dc1d2e · Browse files · 10 files+75 added−93 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+75 added−93 removed

  • other:+75 added−93 removed

10 files+75 added−93 removed

modified:src/core/current.ts+4 added-5 removed
modified:src/core/finalize.ts+4 added-6 removed
9 unmodified lines
10 ImmerState,11 isDraft,12 SetState,13 set,14 ProxyTypeES5Object,15 ProxyTypeES5Array,16 ProxyTypeSet,14 ProxyType,15 getPlugin,16 die,17 revokeScope,18 isFrozen,
62 unmodified lines
81 state.finalized_ = true82 state.scope_.unfinalizedDrafts_--83 const result =84 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.87 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array85 state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array86 ? (state.copy_ = shallowCopy(state.draft_))87 : state.copy_88 // Finalize all children of the copy89 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #62890 // Although the original test case doesn't seem valid anyway, so if this in the way we can turn the next line91 // back to each(result, ....)92 each(95 state.type_ === ProxyTypeSet ? new Set(result) : result,93 state.type_ === ProxyType.Set ? new Set(result) : result,94 (key, childValue) =>95 finalizeProperty(rootScope, state, result, key, childValue, path)96 )97 // everything inside is frozen, we can freeze here
23 unmodified lines
121 if (isDraft(childValue)) {122 const path =123 rootPath &&124 parentState &&127 parentState!.type_ !== ProxyTypeSet && // Set objects are atomic since they have no keys.125 parentState!.type_ !== ProxyType.Set && // Set objects are atomic since they have no keys.126 !has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.127 ? rootPath!.concat(prop)128 : undefined129 // Drafts owned by `scope` are finalized here.
30 unmodified lines
modified:src/core/proxy.ts+5 added-6 removed
13 unmodified lines
14 getCurrentScope,15 DRAFT_STATE,16 die,17 createProxy,18 ProxyTypeProxyObject,19 ProxyTypeProxyArray18 ProxyType19} from "../internal"2021interface ProxyBaseState extends ImmerBaseState {22 assigned_: {
3 unmodified lines
26 revoke_(): void27}2829export interface ProxyObjectState extends ProxyBaseState {31 type_: typeof ProxyTypeProxyObject30 type_: ProxyType.ProxyObject31 base_: any32 copy_: any33 draft_: Drafted<AnyObject, ProxyObjectState>34}3536export interface ProxyArrayState extends ProxyBaseState {38 type_: typeof ProxyTypeProxyArray37 type_: ProxyType.ProxyArray38 base_: AnyArray39 copy_: AnyArray | null40 draft_: Drafted<AnyArray, ProxyArrayState>41}
10 unmodified lines
52 parent?: ImmerState53): Drafted<T, ProxyState> {54 const isArray = Array.isArray(base)55 const state: ProxyState = {57 type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),56 type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),57 // Track which produce call this is associated with.58 scope_: parent ? parent.scope_ : getCurrentScope()!,59 // True for both shallow and deep changes.60 modified_: false,
124 unmodified lines
185 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)186 if (!desc) return desc187 return {188 writable: true,190 configurable: state.type_ !== ProxyTypeProxyArray || prop !== "length",189 configurable: state.type_ !== ProxyType.ProxyArray || prop !== "length",190 enumerable: desc.enumerable,191 value: owner[prop]192 }193 },
79 unmodified lines
modified:src/core/scope.ts+3 added-4 removed
modified:src/plugins/es5.ts+7 added-8 removed
9 unmodified lines
10 DRAFT_STATE,11 is,12 loadPlugin,13 ImmerScope,14 ProxyTypeES5Array,15 ProxyTypeES5Object,14 ProxyType,15 getCurrentScope,16 die,17 markChanged,18 objectTraps,
53 unmodified lines
72 const isArray = Array.isArray(base)73 const draft = createES5Draft(isArray, base)7475 const state: ES5ObjectState | ES5ArrayState = {77 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),76 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),77 scope_: parent ? parent.scope_ : getCurrentScope(),78 modified_: false,79 finalized_: false,80 assigned_: {},
56 unmodified lines
137 for (let i = drafts.length - 1; i >= 0; i--) {138 const state: ES5State = drafts[i][DRAFT_STATE]139 if (!state.modified_) {140 switch (state.type_) {142 case ProxyTypeES5Array:141 case ProxyType.ES5Array:142 if (hasArrayChanges(state)) markChanged(state)143 break145 case ProxyTypeES5Object:144 case ProxyType.ES5Object:145 if (hasObjectChanges(state)) markChanged(state)146 break147 }148 }
4 unmodified lines
153 if (!object || typeof object !== "object") return154 const state: ES5State | undefined = object[DRAFT_STATE]155 if (!state) return156 const {base_, draft_, assigned_, type_} = state158 if (type_ === ProxyTypeES5Object) {157 if (type_ === ProxyType.ES5Object) {158 // Look for added keys.159 // probably there is a faster way to detect changes, as sweep + recurse seems to do some160 // unnecessary work.161 // also: probably we can store the information we detect here, to speed up tree finalization!
15 unmodified lines
177 assigned_[key] = false178 markChanged(state)179 }180 })182 } else if (type_ === ProxyTypeES5Array) {181 } else if (type_ === ProxyType.ES5Array) {182 if (hasArrayChanges(state as ES5ArrayState)) {183 markChanged(state)184 assigned_.length = true185 }
65 unmodified lines
251 return false252 }253254 function hasChanges_(state: ES5State) {256 return state.type_ === ProxyTypeES5Object255 return state.type_ === ProxyType.ES5Object256 ? hasObjectChanges(state)257 : hasArrayChanges(state)258 }259
10 unmodified lines
modified:src/plugins/mapset.ts+3 added-4 removed
11 unmodified lines
12 isDraftable,13 createProxy,14 loadPlugin,15 markChanged,16 ProxyTypeMap,17 ProxyTypeSet,16 ProxyType,17 die,18 each19} from "../internal"20
27 unmodified lines
48 __extends(DraftMap, _super)49 // Create class manually, cause #50250 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {51 this[DRAFT_STATE] = {53 type_: ProxyTypeMap,52 type_: ProxyType.Map,53 parent_: parent,54 scope_: parent ? parent.scope_ : getCurrentScope()!,55 modified_: false,56 finalized_: false,
149 unmodified lines
206 __extends(DraftSet, _super)207 // Create class manually, cause #502208 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {209 this[DRAFT_STATE] = {211 type_: ProxyTypeSet,210 type_: ProxyType.Set,211 parent_: parent,212 scope_: parent ? parent.scope_ : getCurrentScope()!,213 modified_: false,214 finalized_: false,
130 unmodified lines
modified:src/plugins/patches.ts+18 added-26 removed
14 unmodified lines
15 getArchtype,16 isSet,17 isMap,18 loadPlugin,19 ProxyTypeProxyObject,20 ProxyTypeES5Object,21 ProxyTypeMap,22 ProxyTypeES5Array,23 ProxyTypeProxyArray,24 ProxyTypeSet,25 ArchtypeMap,26 ArchtypeSet,27 ArchtypeArray,19 ProxyType,20 Archtype,21 die,22 isDraft,30 isDraftable,31 ArchtypeObject23 isDraftable24} from "../internal"2526export function enablePatches() {27 const REPLACE = "replace"
6 unmodified lines
34 patches: Patch[],35 inversePatches: Patch[]36 ): void {37 switch (state.type_) {46 case ProxyTypeProxyObject:47 case ProxyTypeES5Object:48 case ProxyTypeMap:38 case ProxyType.ProxyObject:39 case ProxyType.ES5Object:40 case ProxyType.Map:41 return generatePatchesFromAssigned(42 state,43 basePath,44 patches,45 inversePatches46 )55 case ProxyTypeES5Array:56 case ProxyTypeProxyArray:47 case ProxyType.ES5Array:48 case ProxyType.ProxyArray:49 return generateArrayPatches(state, basePath, patches, inversePatches)58 case ProxyTypeSet:50 case ProxyType.Set:51 return generateSetPatches(52 (state as any) as SetState,53 basePath,54 patches,
153 unmodified lines
208 const parentType = getArchtype(base)209 const p = path[i]210 // See #738, avoid prototype pollution211 if (220 (parentType === ArchtypeObject || parentType === ArchtypeArray) &&212 (parentType === Archtype.Object || parentType === Archtype.Array) &&213 (p === "__proto__" || p === "constructor")214 )215 die(24)216 if (typeof base === "function" && p === "prototype") die(24)
6 unmodified lines
223 const key = path[path.length - 1]224 switch (op) {225 case REPLACE:226 switch (type) {235 case ArchtypeMap:227 case Archtype.Map:228 return base.set(key, value)229 /* istanbul ignore next */238 case ArchtypeSet:230 case Archtype.Set:231 die(16)232 default:233 // if value is an object, then it's assigned by reference234 // in the following add or remove ops, the value field inside the patch will also be modifyed
2 unmodified lines
237 return (base[key] = value)238 }239 case ADD:240 switch (type) {249 case ArchtypeArray:241 case Archtype.Array:242 return base.splice(key as any, 0, value)251 case ArchtypeMap:243 case Archtype.Map:244 return base.set(key, value)253 case ArchtypeSet:245 case Archtype.Set:246 return base.add(value)247 default:248 return (base[key] = value)249 }250 case REMOVE:251 switch (type) {260 case ArchtypeArray:252 case Archtype.Array:253 return base.splice(key as any, 1)262 case ArchtypeMap:254 case Archtype.Map:255 return base.delete(key)264 case ArchtypeSet:256 case Archtype.Set:257 return base.delete(patch.value)258 default:259 return delete base[key]260 }
38 unmodified lines
modified:src/types/types-internal.ts+14 added-10 removed
15 unmodified lines
16export type AnyArray = Array<any>17export type AnySet = Set<any>18export type AnyMap = Map<any, any>1920export const ArchtypeObject = 021export const ArchtypeArray = 122export const ArchtypeMap = 223export const ArchtypeSet = 320export const enum Archtype {21 Object,22 Array,23 Map,24 Set25}2625export const ProxyTypeProxyObject = 026export const ProxyTypeProxyArray = 127export const ProxyTypeES5Object = 428export const ProxyTypeES5Array = 529export const ProxyTypeMap = 230export const ProxyTypeSet = 327export const enum ProxyType {28 ProxyObject,29 ProxyArray,30 Map,31 Set,32 ES5Object,33 ES5Array34}3536export interface ImmerBaseState {37 parent_?: ImmerState38 scope_: ImmerScope
17 unmodified lines
modified:src/utils/common.ts+12 added-16 removed
7 unmodified lines
8 AnyMap,9 AnySet,10 ImmerState,11 hasMap,12 ArchtypeObject,13 ArchtypeArray,14 ArchtypeMap,15 ArchtypeSet,12 Archtype,13 die14} from "../internal"1516/** Returns true if the given value is an Immer draft */
26 unmodified lines
43 }44 const Ctor =45 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor4650 if (Ctor === Object)51 return true47 if (Ctor === Object) return true4849 return (50 typeof Ctor == "function" &&51 Function.toString.call(Ctor) === objectCtorString
35 unmodified lines
87 iter: (key: string | number, value: any, source: T) => void,88 enumerableOnly?: boolean89): void90export function each(obj: any, iter: any, enumerableOnly = false) {95 if (getArchtype(obj) === ArchtypeObject) {91 if (getArchtype(obj) === Archtype.Object) {92 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key => {93 if (!enumerableOnly || typeof key !== "symbol") iter(key, obj[key], obj)94 })95 } else {
1 unmodified line
97 }98}99100/*#__PURE__*/105export function getArchtype(thing: any): 0 | 1 | 2 | 3 {101export function getArchtype(thing: any): Archtype {102 /* istanbul ignore next */103 const state: undefined | ImmerState = thing[DRAFT_STATE]104 return state105 ? state.type_ > 3106 ? state.type_ - 4 // cause Object and Array map back from 4 and 5107 : (state.type_ as any) // others are the same108 : Array.isArray(thing)113 ? ArchtypeArray109 ? Archtype.Array110 : isMap(thing)115 ? ArchtypeMap111 ? Archtype.Map112 : isSet(thing)117 ? ArchtypeSet118 : ArchtypeObject113 ? Archtype.Set114 : Archtype.Object115}116117/*#__PURE__*/118export function has(thing: any, prop: PropertyKey): boolean {123 return getArchtype(thing) === ArchtypeMap119 return getArchtype(thing) === Archtype.Map120 ? thing.has(prop)121 : Object.prototype.hasOwnProperty.call(thing, prop)122}123124/*#__PURE__*/125export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {126 // @ts-ignore131 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]127 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]128}129130/*#__PURE__*/131export function set(thing: any, propOrOldValue: PropertyKey, value: any) {132 const t = getArchtype(thing)137 if (t === ArchtypeMap) thing.set(propOrOldValue, value)138 else if (t === ArchtypeSet) {133 if (t === Archtype.Map) thing.set(propOrOldValue, value)134 else if (t === Archtype.Set) {135 thing.delete(propOrOldValue)136 thing.add(value)137 } else thing[propOrOldValue] = value138}
79 unmodified lines
modified:src/utils/plugins.ts+5 added-8 removed
5 unmodified lines
6 AnyObject,7 ImmerBaseState,8 AnyMap,9 AnySet,10 ProxyTypeES5Array,11 ProxyTypeES5Object,12 ProxyTypeMap,13 ProxyTypeSet,10 ProxyType,11 die12} from "../internal"1314/** Plugin utilities */
55 unmodified lines
70 revoked_: boolean71}7273export interface ES5ObjectState extends ES5BaseState {77 type_: typeof ProxyTypeES5Object74 type_: ProxyType.ES5Object75 draft_: Drafted<AnyObject, ES5ObjectState>76 base_: AnyObject77 copy_: AnyObject | null78}7980export interface ES5ArrayState extends ES5BaseState {84 type_: typeof ProxyTypeES5Array81 type_: ProxyType.ES5Array82 draft_: Drafted<AnyObject, ES5ArrayState>83 base_: any84 copy_: any85}8687/** Map / Set plugin */8889export interface MapState extends ImmerBaseState {93 type_: typeof ProxyTypeMap90 type_: ProxyType.Map91 copy_: AnyMap | undefined92 assigned_: Map<any, boolean> | undefined93 base_: AnyMap94 revoked_: boolean95 draft_: Drafted<AnyMap, MapState>96}9798export interface SetState extends ImmerBaseState {102 type_: typeof ProxyTypeSet99 type_: ProxyType.Set100 copy_: AnySet | undefined101 base_: AnySet102 drafts_: Map<any, Drafted> // maps the original value to the draft value in the new set103 revoked_: boolean
6 unmodified lines