Skip to content
Myr

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

8 files+74 added−57 removed

modified:src/common.ts+15 added-19 removed
3 unmodified lines
4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,8 ImmerState8 ImmerState,9 ProxyType,10 Archtype11} from "./types"1213/** Use a class type for `nothing` so its type is unique */14export class Nothing {
92 unmodified lines
107 const desc = Object.getOwnPropertyDescriptor(base, prop)108 return desc && desc.enumerable ? true : false109}110109export enum Archtype {110 Object,111 Array,112 Map,113 Set114}115116const 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}124111export 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 lines
modified:src/es5.ts+9 added-8 removed
17 unmodified lines
18 Drafted,19 AnyObject,20 Objectish,21 ImmerBaseState,22 AnyArray22 AnyArray,23 ProxyType24} from "./types"2526interface ES5BaseState extends ImmerBaseState {27 finalizing: boolean
3 unmodified lines
31 revoked: boolean32}3334export interface ES5ObjectState extends ES5BaseState {34 type: "es5_object"35 type: ProxyType.ES5Object36 draft: Drafted<AnyObject, ES5ObjectState>37 base: AnyObject38 copy: AnyObject | null39}4041export interface ES5ArrayState extends ES5BaseState {41 type: "es5_array"42 type: ProxyType.ES5Array43 draft: Drafted<AnyObject, ES5ArrayState>44 base: AnyArray45 copy: AnyArray | null46}
32 unmodified lines
79 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))80 })8182 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 lines
206 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 lines
222 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) return
13 unmodified lines
244 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] = false
55 unmodified lines
modified:src/finalize.ts+5 added-2 removed
1import {Immer} from "./immer"2import {ImmerState, Drafted} from "./types"2import {ImmerState, Drafted, ProxyType} from "./types"3import {ImmerScope} from "./scope"4import {5 isSet,6 has,
109 unmodified lines
116 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.copy
91 unmodified lines
modified:src/map.ts+3 added-3 removed
modified:src/patches.ts+14 added-9 removed
1import {get, each, isMap, has, die, getArchtype, Archtype} from "./common"2import {Patch, ImmerState} from "./types"1import {get, each, isMap, has, die, getArchtype} from "./common"2import {Patch, ImmerState, ProxyType, Archtype} from "./types"3import {SetState} from "./set"4import {ES5ArrayState, ES5ObjectState} from "./es5"5import {ProxyArrayState, ProxyObjectState} from "./proxy"6import {MapState} from "./map"
6 unmodified lines
13 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}3839function generateArrayPatches(
209 unmodified lines
modified:src/proxy.ts+11 added-6 removed
13 unmodified lines
14 Drafted,15 ImmerState,16 AnyArray,17 Objectish,18 ImmerBaseState18 ImmerBaseState,19 ProxyType20} from "./types"2122interface ProxyBaseState extends ImmerBaseState {23 assigned: {
6 unmodified lines
30 revoke(): void31}3233export interface ProxyObjectState extends ProxyBaseState {33 type: "proxy_object"34 type: ProxyType.ProxyObject35 base: AnyObject36 copy: AnyObject | null37 draft: Drafted<AnyObject, ProxyObjectState>38}3940export interface ProxyArrayState extends ProxyBaseState {40 type: "proxy_array"41 type: ProxyType.ProxyArray42 base: AnyArray43 copy: AnyArray | null44 draft: Drafted<AnyArray, ProxyArrayState>45}
10 unmodified lines
56 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 lines
173 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 lines
231232export 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] = value
15 unmodified lines
modified:src/set.ts+3 added-3 removed
1import {DRAFT_STATE, latest, isDraftable, iteratorSymbol} from "./common"23import {ImmerScope} from "./scope"4import {AnySet, Drafted, ImmerState, ImmerBaseState} from "./types"4import {AnySet, Drafted, ImmerState, ImmerBaseState, ProxyType} from "./types"56export 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 set
9 unmodified lines
21 [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 lines
modified:src/types.ts+14 added-7 removed
10 unmodified lines
11export type AnyObject = {[key: string]: any}12export type AnyArray = Array<any>13export type AnySet = Set<any>14export type AnyMap = Map<any, any>15export enum Archtype {16 Object,17 Array,18 Map,19 Set20}2116export type DraftType =17 | "proxy_object"18 | "proxy_array"19 | "es5_object"20 | "es5_array"21 | "map"22 | "set"22export enum ProxyType {23 ProxyObject,24 ProxyArray,25 ES5Object,26 ES5Array,27 Map,28 Set29}3031export interface ImmerBaseState {32 parent?: ImmerState33 scope: ImmerScope
192 unmodified lines