Skip to content

Commit b1fac9d

Browse files
authored
chore: reduce size by using TS const enum (#812)
1 parent 0dc1d2e commit b1fac9d

File tree

10 files changed

+75
-93
lines changed

10 files changed

+75
-93
lines changed

src/core/current.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
set,
99
ImmerState,
1010
isDraftable,
11-
ArchtypeMap,
12-
ArchtypeSet,
11+
Archtype,
1312
getArchtype,
1413
getPlugin
1514
} from "../internal"
@@ -45,15 +44,15 @@ function currentImpl(value: any): any {
4544
set(copy, key, currentImpl(childValue))
4645
})
4746
// In the future, we might consider freezing here, based on the current settings
48-
return archType === ArchtypeSet ? new Set(copy) : copy
47+
return archType === Archtype.Set ? new Set(copy) : copy
4948
}
5049

5150
function copyHelper(value: any, archType: number): any {
5251
// creates a shallow copy, even if it is a map or set
5352
switch (archType) {
54-
case ArchtypeMap:
53+
case Archtype.Map:
5554
return new Map(value)
56-
case ArchtypeSet:
55+
case Archtype.Set:
5756
// Set will be cloned as array temporarily, so that we can replace individual items
5857
return Array.from(value)
5958
}

src/core/finalize.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import {
1111
isDraft,
1212
SetState,
1313
set,
14-
ProxyTypeES5Object,
15-
ProxyTypeES5Array,
16-
ProxyTypeSet,
14+
ProxyType,
1715
getPlugin,
1816
die,
1917
revokeScope,
@@ -84,15 +82,15 @@ function finalize(rootScope: ImmerScope, value: any, path?: PatchPath) {
8482
state.scope_.unfinalizedDrafts_--
8583
const result =
8684
// For ES5, create a good copy from the draft first, with added keys and without deleted keys.
87-
state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array
85+
state.type_ === ProxyType.ES5Object || state.type_ === ProxyType.ES5Array
8886
? (state.copy_ = shallowCopy(state.draft_))
8987
: state.copy_
9088
// Finalize all children of the copy
9189
// For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628
9290
// Although the original test case doesn't seem valid anyway, so if this in the way we can turn the next line
9391
// back to each(result, ....)
9492
each(
95-
state.type_ === ProxyTypeSet ? new Set(result) : result,
93+
state.type_ === ProxyType.Set ? new Set(result) : result,
9694
(key, childValue) =>
9795
finalizeProperty(rootScope, state, result, key, childValue, path)
9896
)
@@ -124,7 +122,7 @@ function finalizeProperty(
124122
const path =
125123
rootPath &&
126124
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.
128126
!has((parentState as Exclude<ImmerState, SetState>).assigned_!, prop) // Skip deep patches for assigned keys.
129127
? rootPath!.concat(prop)
130128
: undefined

src/core/proxy.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import {
1515
DRAFT_STATE,
1616
die,
1717
createProxy,
18-
ProxyTypeProxyObject,
19-
ProxyTypeProxyArray
18+
ProxyType
2019
} from "../internal"
2120

2221
interface ProxyBaseState extends ImmerBaseState {
@@ -28,14 +27,14 @@ interface ProxyBaseState extends ImmerBaseState {
2827
}
2928

3029
export interface ProxyObjectState extends ProxyBaseState {
31-
type_: typeof ProxyTypeProxyObject
30+
type_: ProxyType.ProxyObject
3231
base_: any
3332
copy_: any
3433
draft_: Drafted<AnyObject, ProxyObjectState>
3534
}
3635

3736
export interface ProxyArrayState extends ProxyBaseState {
38-
type_: typeof ProxyTypeProxyArray
37+
type_: ProxyType.ProxyArray
3938
base_: AnyArray
4039
copy_: AnyArray | null
4140
draft_: Drafted<AnyArray, ProxyArrayState>
@@ -54,7 +53,7 @@ export function createProxyProxy<T extends Objectish>(
5453
): Drafted<T, ProxyState> {
5554
const isArray = Array.isArray(base)
5655
const state: ProxyState = {
57-
type_: isArray ? ProxyTypeProxyArray : (ProxyTypeProxyObject as any),
56+
type_: isArray ? ProxyType.ProxyArray : (ProxyType.ProxyObject as any),
5857
// Track which produce call this is associated with.
5958
scope_: parent ? parent.scope_ : getCurrentScope()!,
6059
// True for both shallow and deep changes.
@@ -187,7 +186,7 @@ export const objectTraps: ProxyHandler<ProxyState> = {
187186
if (!desc) return desc
188187
return {
189188
writable: true,
190-
configurable: state.type_ !== ProxyTypeProxyArray || prop !== "length",
189+
configurable: state.type_ !== ProxyType.ProxyArray || prop !== "length",
191190
enumerable: desc.enumerable,
192191
value: owner[prop]
193192
}

src/core/scope.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
Immer,
66
DRAFT_STATE,
77
ImmerState,
8-
ProxyTypeProxyObject,
9-
ProxyTypeProxyArray,
8+
ProxyType,
109
getPlugin
1110
} from "../internal"
1211
import {die} from "../utils/errors"
@@ -78,8 +77,8 @@ export function enterScope(immer: Immer) {
7877
function revokeDraft(draft: Drafted) {
7978
const state: ImmerState = draft[DRAFT_STATE]
8079
if (
81-
state.type_ === ProxyTypeProxyObject ||
82-
state.type_ === ProxyTypeProxyArray
80+
state.type_ === ProxyType.ProxyObject ||
81+
state.type_ === ProxyType.ProxyArray
8382
)
8483
state.revoke_()
8584
else state.revoked_ = true

src/plugins/es5.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import {
1111
is,
1212
loadPlugin,
1313
ImmerScope,
14-
ProxyTypeES5Array,
15-
ProxyTypeES5Object,
14+
ProxyType,
1615
getCurrentScope,
1716
die,
1817
markChanged,
@@ -74,7 +73,7 @@ export function enableES5() {
7473
const draft = createES5Draft(isArray, base)
7574

7675
const state: ES5ObjectState | ES5ArrayState = {
77-
type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),
76+
type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),
7877
scope_: parent ? parent.scope_ : getCurrentScope(),
7978
modified_: false,
8079
finalized_: false,
@@ -139,10 +138,10 @@ export function enableES5() {
139138
const state: ES5State = drafts[i][DRAFT_STATE]
140139
if (!state.modified_) {
141140
switch (state.type_) {
142-
case ProxyTypeES5Array:
141+
case ProxyType.ES5Array:
143142
if (hasArrayChanges(state)) markChanged(state)
144143
break
145-
case ProxyTypeES5Object:
144+
case ProxyType.ES5Object:
146145
if (hasObjectChanges(state)) markChanged(state)
147146
break
148147
}
@@ -155,7 +154,7 @@ export function enableES5() {
155154
const state: ES5State | undefined = object[DRAFT_STATE]
156155
if (!state) return
157156
const {base_, draft_, assigned_, type_} = state
158-
if (type_ === ProxyTypeES5Object) {
157+
if (type_ === ProxyType.ES5Object) {
159158
// Look for added keys.
160159
// probably there is a faster way to detect changes, as sweep + recurse seems to do some
161160
// unnecessary work.
@@ -179,7 +178,7 @@ export function enableES5() {
179178
markChanged(state)
180179
}
181180
})
182-
} else if (type_ === ProxyTypeES5Array) {
181+
} else if (type_ === ProxyType.ES5Array) {
183182
if (hasArrayChanges(state as ES5ArrayState)) {
184183
markChanged(state)
185184
assigned_.length = true
@@ -253,7 +252,7 @@ export function enableES5() {
253252
}
254253

255254
function hasChanges_(state: ES5State) {
256-
return state.type_ === ProxyTypeES5Object
255+
return state.type_ === ProxyType.ES5Object
257256
? hasObjectChanges(state)
258257
: hasArrayChanges(state)
259258
}

src/plugins/mapset.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import {
1313
createProxy,
1414
loadPlugin,
1515
markChanged,
16-
ProxyTypeMap,
17-
ProxyTypeSet,
16+
ProxyType,
1817
die,
1918
each
2019
} from "../internal"
@@ -50,7 +49,7 @@ export function enableMapSet() {
5049
// Create class manually, cause #502
5150
function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {
5251
this[DRAFT_STATE] = {
53-
type_: ProxyTypeMap,
52+
type_: ProxyType.Map,
5453
parent_: parent,
5554
scope_: parent ? parent.scope_ : getCurrentScope()!,
5655
modified_: false,
@@ -208,7 +207,7 @@ export function enableMapSet() {
208207
// Create class manually, cause #502
209208
function DraftSet(this: any, target: AnySet, parent?: ImmerState) {
210209
this[DRAFT_STATE] = {
211-
type_: ProxyTypeSet,
210+
type_: ProxyType.Set,
212211
parent_: parent,
213212
scope_: parent ? parent.scope_ : getCurrentScope()!,
214213
modified_: false,

src/plugins/patches.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@ import {
1616
isSet,
1717
isMap,
1818
loadPlugin,
19-
ProxyTypeProxyObject,
20-
ProxyTypeES5Object,
21-
ProxyTypeMap,
22-
ProxyTypeES5Array,
23-
ProxyTypeProxyArray,
24-
ProxyTypeSet,
25-
ArchtypeMap,
26-
ArchtypeSet,
27-
ArchtypeArray,
19+
ProxyType,
20+
Archtype,
2821
die,
2922
isDraft,
30-
isDraftable,
31-
ArchtypeObject
23+
isDraftable
3224
} from "../internal"
3325

3426
export function enablePatches() {
@@ -43,19 +35,19 @@ export function enablePatches() {
4335
inversePatches: Patch[]
4436
): void {
4537
switch (state.type_) {
46-
case ProxyTypeProxyObject:
47-
case ProxyTypeES5Object:
48-
case ProxyTypeMap:
38+
case ProxyType.ProxyObject:
39+
case ProxyType.ES5Object:
40+
case ProxyType.Map:
4941
return generatePatchesFromAssigned(
5042
state,
5143
basePath,
5244
patches,
5345
inversePatches
5446
)
55-
case ProxyTypeES5Array:
56-
case ProxyTypeProxyArray:
47+
case ProxyType.ES5Array:
48+
case ProxyType.ProxyArray:
5749
return generateArrayPatches(state, basePath, patches, inversePatches)
58-
case ProxyTypeSet:
50+
case ProxyType.Set:
5951
return generateSetPatches(
6052
(state as any) as SetState,
6153
basePath,
@@ -217,7 +209,7 @@ export function enablePatches() {
217209
const p = path[i]
218210
// See #738, avoid prototype pollution
219211
if (
220-
(parentType === ArchtypeObject || parentType === ArchtypeArray) &&
212+
(parentType === Archtype.Object || parentType === Archtype.Array) &&
221213
(p === "__proto__" || p === "constructor")
222214
)
223215
die(24)
@@ -232,10 +224,10 @@ export function enablePatches() {
232224
switch (op) {
233225
case REPLACE:
234226
switch (type) {
235-
case ArchtypeMap:
227+
case Archtype.Map:
236228
return base.set(key, value)
237229
/* istanbul ignore next */
238-
case ArchtypeSet:
230+
case Archtype.Set:
239231
die(16)
240232
default:
241233
// if value is an object, then it's assigned by reference
@@ -246,22 +238,22 @@ export function enablePatches() {
246238
}
247239
case ADD:
248240
switch (type) {
249-
case ArchtypeArray:
241+
case Archtype.Array:
250242
return base.splice(key as any, 0, value)
251-
case ArchtypeMap:
243+
case Archtype.Map:
252244
return base.set(key, value)
253-
case ArchtypeSet:
245+
case Archtype.Set:
254246
return base.add(value)
255247
default:
256248
return (base[key] = value)
257249
}
258250
case REMOVE:
259251
switch (type) {
260-
case ArchtypeArray:
252+
case Archtype.Array:
261253
return base.splice(key as any, 1)
262-
case ArchtypeMap:
254+
case Archtype.Map:
263255
return base.delete(key)
264-
case ArchtypeSet:
256+
case Archtype.Set:
265257
return base.delete(patch.value)
266258
default:
267259
return delete base[key]

src/types/types-internal.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ export type AnyArray = Array<any>
1717
export type AnySet = Set<any>
1818
export type AnyMap = Map<any, any>
1919

20-
export const ArchtypeObject = 0
21-
export const ArchtypeArray = 1
22-
export const ArchtypeMap = 2
23-
export const ArchtypeSet = 3
20+
export const enum Archtype {
21+
Object,
22+
Array,
23+
Map,
24+
Set
25+
}
2426

25-
export const ProxyTypeProxyObject = 0
26-
export const ProxyTypeProxyArray = 1
27-
export const ProxyTypeES5Object = 4
28-
export const ProxyTypeES5Array = 5
29-
export const ProxyTypeMap = 2
30-
export const ProxyTypeSet = 3
27+
export const enum ProxyType {
28+
ProxyObject,
29+
ProxyArray,
30+
Map,
31+
Set,
32+
ES5Object,
33+
ES5Array
34+
}
3135

3236
export interface ImmerBaseState {
3337
parent_?: ImmerState

0 commit comments

Comments
 (0)