Enums FTW
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 c7f8f5b · Browse files · 8 files·+74 added−57 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+74 added−57 removed
- other:+74 added−57 removed
Provenance
- Commit
fd76f1c- Tree
2189e3d- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
c7f8f5b
8 files changed+74 added−57 removed
modified:src/common.ts+15 added-19 removed
3 unmodified lines4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,8- ImmerState8+ ImmerState,9+ ProxyType,10+ Archtype11 } from "./types"12 13 /** Use a class type for `nothing` so its type is unique */14 export class Nothing {92 unmodified lines107 const desc = Object.getOwnPropertyDescriptor(base, prop)108 return desc && desc.enumerable ? true : false109 }110 109-export enum Archtype {110- Object,111- Array,112- Map,113- Set114-}115-116-const toArchType = {117- proxy_object: Archtype.Object,118- es5_object: Archtype.Object,119- proxy_array: Archtype.Array,120- es5_array: Archtype.Array,121- map: Archtype.Map,122- set: Archtype.Set123-}124-111 export function getArchtype(thing: any): Archtype {112 if (!thing) die()113 if (thing[DRAFT_STATE]) {128- // @ts-ignore129- return toArchType[(thing as Drafted)[DRAFT_STATE].type]114+ switch ((thing as Drafted)[DRAFT_STATE].type) {115+ case ProxyType.ES5Object:116+ case ProxyType.ProxyObject:117+ return Archtype.Object118+ case ProxyType.ES5Array:119+ case ProxyType.ProxyArray:120+ return Archtype.Array121+ case ProxyType.Map:122+ return Archtype.Map123+ case ProxyType.Set:124+ return Archtype.Set125+ }126 }127 return Array.isArray(thing)128 ? Archtype.Array129 : isMap(thing)115 unmodified linesmodified:src/es5.ts+9 added-8 removed
17 unmodified lines18 Drafted,19 AnyObject,20 Objectish,21 ImmerBaseState,22- AnyArray22+ AnyArray,23+ ProxyType24 } from "./types"25 26 interface ES5BaseState extends ImmerBaseState {27 finalizing: boolean3 unmodified lines31 revoked: boolean32 }33 34 export interface ES5ObjectState extends ES5BaseState {34- type: "es5_object"35+ type: ProxyType.ES5Object36 draft: Drafted<AnyObject, ES5ObjectState>37 base: AnyObject38 copy: AnyObject | null39 }40 41 export interface ES5ArrayState extends ES5BaseState {41- type: "es5_array"42+ type: ProxyType.ES5Array43 draft: Drafted<AnyObject, ES5ArrayState>44 base: AnyArray45 copy: AnyArray | null46 }32 unmodified lines79 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))80 })81 82 const state: ES5ObjectState | ES5ArrayState = {82- type: isArray ? "es5_array" : ("es5_object" as any),83+ type: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),84 scope: parent ? parent.scope : ImmerScope.current!,85 modified: false,86 finalizing: false,87 finalized: false,118 unmodified lines206 for (let i = drafts.length - 1; i >= 0; i--) {207 const state = drafts[i][DRAFT_STATE]208 if (!state.modified) {209 switch (state.type) {209- case "es5_array":210+ case ProxyType.ES5Array:211 if (hasArrayChanges(state)) markChangedES5(state)212 break212- case "es5_object":213+ case ProxyType.ES5Object:214 if (hasObjectChanges(state)) markChangedES5(state)215 break216 }217 }4 unmodified lines222 if (!object || typeof object !== "object") return223 const state = object[DRAFT_STATE]224 if (!state) return225 const {base, draft, assigned, type} = state225- if (type === "es5_object") {226+ if (type === ProxyType.ES5Object) {227 // Look for added keys.228 // TODO: looks quite duplicate to hasObjectChanges229 each(draft, key => {230 if ((key as any) === DRAFT_STATE) return13 unmodified lines244 assigned[key] = false245 markChangedES5(state)246 }247 })247- } else if (type === "es5_array" && hasArrayChanges(state)) {248+ } else if (type === ProxyType.ES5Array && hasArrayChanges(state)) {249 markChangedES5(state)250 assigned.length = true251 if (draft.length < base.length) {252 for (let i = draft.length; i < base.length; i++) assigned[i] = false55 unmodified linesmodified:src/finalize.ts+5 added-2 removed
1 import {Immer} from "./immer"2-import {ImmerState, Drafted} from "./types"2+import {ImmerState, Drafted, ProxyType} from "./types"3 import {ImmerScope} from "./scope"4 import {5 isSet,6 has,109 unmodified lines116 rootPath?: PatchPath117 ) {118 const state = root[DRAFT_STATE]119 if (state) {120- if (state.type === "es5_object" || state.type === "es5_array") {120+ if (121+ state.type === ProxyType.ES5Object ||122+ state.type === ProxyType.ES5Array123+ ) {124 // Create the final copy, with added keys and without deleted keys.125 state.copy = shallowCopy(state.draft, true)126 }127 root = state.copy91 unmodified linesmodified:src/map.ts+3 added-3 removed
1234456778910119 unmodified lines21222324252526272829130 unmodified linesExpand allimport {isDraftable, DRAFT_STATE, latest, iteratorSymbol} from "./common"
import {ImmerScope} from "./scope"import {AnyMap, Drafted, ImmerState, ImmerBaseState} from "./types"import {AnyMap, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"
export interface MapState extends ImmerBaseState { type: "map" type: ProxyType.Map finalizing: boolean copy: AnyMap | undefined assigned: Map<any, boolean> | undefined base: AnyMap9 unmodified lines [DRAFT_STATE]: MapState constructor(target: AnyMap, parent?: ImmerState) { super() this[DRAFT_STATE] = { type: "map", type: ProxyType.Map, parent, scope: parent ? parent.scope : ImmerScope.current!, modified: false, finalized: false,130 unmodified linesExpand allmodified:src/patches.ts+14 added-9 removed
1-import {get, each, isMap, has, die, getArchtype, Archtype} from "./common"2-import {Patch, ImmerState} from "./types"1+import {get, each, isMap, has, die, getArchtype} from "./common"2+import {Patch, ImmerState, ProxyType, Archtype} from "./types"3 import {SetState} from "./set"4 import {ES5ArrayState, ES5ObjectState} from "./es5"5 import {ProxyArrayState, ProxyObjectState} from "./proxy"6 import {MapState} from "./map"6 unmodified lines13 patches: Patch[],14 inversePatches: Patch[]15 ): void {16 switch (state.type) {17- case "proxy_object":18- case "es5_object":19- case "map":17+ case ProxyType.ProxyObject:18+ case ProxyType.ES5Object:19+ case ProxyType.Map:20 return generatePatchesFromAssigned(21 state,22 basePath,23 patches,24 inversePatches25 )26- case "es5_array":27- case "proxy_array":26+ case ProxyType.ES5Array:27+ case ProxyType.ProxyArray:28 return generateArrayPatches(state, basePath, patches, inversePatches)29- case "set":30- return generateSetPatches(state, basePath, patches, inversePatches)29+ case ProxyType.Set:30+ return generateSetPatches(31+ (state as any) as SetState,32+ basePath,33+ patches,34+ inversePatches35+ )36 }37 }38 39 function generateArrayPatches(209 unmodified linesmodified:src/proxy.ts+11 added-6 removed
13 unmodified lines14 Drafted,15 ImmerState,16 AnyArray,17 Objectish,18- ImmerBaseState18+ ImmerBaseState,19+ ProxyType20 } from "./types"21 22 interface ProxyBaseState extends ImmerBaseState {23 assigned: {6 unmodified lines30 revoke(): void31 }32 33 export interface ProxyObjectState extends ProxyBaseState {33- type: "proxy_object"34+ type: ProxyType.ProxyObject35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38 }39 40 export interface ProxyArrayState extends ProxyBaseState {40- type: "proxy_array"41+ type: ProxyType.ProxyArray42 base: AnyArray43 copy: AnyArray | null44 draft: Drafted<AnyArray, ProxyArrayState>45 }10 unmodified lines56 parent?: ImmerState57 ): Drafted<T, ProxyState> {58 const isArray = Array.isArray(base)59 const state: ProxyState = {59- type: isArray ? "proxy_array" : ("proxy_object" as any),60+ type: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),61 // Track which produce call this is associated with.62 scope: parent ? parent.scope : ImmerScope.current!,63 // True for both shallow and deep changes.64 modified: false,108 unmodified lines173 const owner = latest(state)174 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)175 if (desc) {176 desc.writable = true176- desc.configurable = state.type !== "proxy_array" || prop !== "length"177+ desc.configurable =178+ state.type !== ProxyType.ProxyArray || prop !== "length"179 }180 return desc181 },182 defineProperty() {48 unmodified lines231 232 export function markChanged(state: ImmerState) {233 if (!state.modified) {234 state.modified = true233- if (state.type === "proxy_object" || state.type === "proxy_array") {235+ if (236+ state.type === ProxyType.ProxyObject ||237+ state.type === ProxyType.ProxyArray238+ ) {239 const copy = (state.copy = shallowCopy(state.base))240 each(state.drafts!, (key, value) => {241 // @ts-ignore242 copy[key] = value15 unmodified linesmodified:src/set.ts+3 added-3 removed
1 import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"2 3 import {ImmerScope} from "./scope"4-import {AnySet, Drafted, ImmerState, ImmerBaseState} from "./types"4+import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"5 6 export interface SetState extends ImmerBaseState {7- type: "set"7+ type: ProxyType.Set8 finalizing: boolean9 copy: AnySet | undefined10 base: AnySet11 drafts: Map<any, Drafted> // maps the original value to the draft value in the new set9 unmodified lines21 [DRAFT_STATE]: SetState22 constructor(target: AnySet, parent?: ImmerState) {23 super()24 this[DRAFT_STATE] = {25- type: "set",25+ type: ProxyType.Set,26 parent,27 scope: parent ? parent.scope : ImmerScope.current!,28 modified: false,29 finalized: false,112 unmodified linesmodified:src/types.ts+14 added-7 removed
10 unmodified lines11 export type AnyObject = {[key: string]: any}12 export type AnyArray = Array<any>13 export type AnySet = Set<any>14 export type AnyMap = Map<any, any>15+export enum Archtype {16+ Object,17+ Array,18+ Map,19+ Set20+}21 16-export type DraftType =17- | "proxy_object"18- | "proxy_array"19- | "es5_object"20- | "es5_array"21- | "map"22- | "set"22+export enum ProxyType {23+ ProxyObject,24+ ProxyArray,25+ ES5Object,26+ ES5Array,27+ Map,28+ Set29+}30 31 export interface ImmerBaseState {32 parent?: ImmerState33 scope: ImmerScope192 unmodified lines