Skip to content

Commit cfae1ac

Browse files
committed
Byte-shave field names and arrow functions
1 parent 2759d45 commit cfae1ac

9 files changed

Lines changed: 120 additions & 116 deletions

File tree

src/core/finalize.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ function markStateFinalized(state: ImmerState) {
108108
state.scope_.unfinalizedDrafts_--
109109
}
110110

111-
function isSameScope(state: ImmerState, rootScope: ImmerScope) {
112-
return state.scope_ === rootScope
113-
}
111+
let isSameScope = (state: ImmerState, rootScope: ImmerScope) =>
112+
state.scope_ === rootScope
114113

115114
// A reusable empty array to avoid allocations
116115
const EMPTY_LOCATIONS_RESULT: (string | symbol | number)[] = []
@@ -180,7 +179,7 @@ export function registerChildFinalizationCallback(
180179
}
181180

182181
// Handle potential set value finalization first
183-
rootScope.mapSetPlugin_?.fixPotentialSetContents(state)
182+
rootScope.mapSetPlugin_?.fixSetContents(state)
184183

185184
const finalizedValue = getFinalValue(state)
186185

src/core/immerClass.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import {
3232
AnySet,
3333
isObjectish,
3434
isFunction,
35-
isBoolean
35+
isBoolean,
36+
PluginMapSet,
37+
PluginPatches
3638
} from "../internal"
3739

3840
interface ProducersFns {
@@ -122,7 +124,7 @@ export class Immer implements ProducersFns {
122124
if (patchListener) {
123125
const p: Patch[] = []
124126
const ip: Patch[] = []
125-
getPlugin("Patches").generateReplacementPatches_(base, result, {
127+
getPlugin(PluginPatches).generateReplacementPatches_(base, result, {
126128
patches_: p,
127129
inversePatches_: ip
128130
} as ImmerScope) // dummy scope
@@ -217,7 +219,7 @@ export class Immer implements ProducersFns {
217219
patches = patches.slice(i + 1)
218220
}
219221

220-
const applyPatchesImpl = getPlugin("Patches").applyPatches_
222+
const applyPatchesImpl = getPlugin(PluginPatches).applyPatches_
221223
if (isDraft(base)) {
222224
// N.B: never hits if some patch a replacement, patches are never drafts
223225
return applyPatchesImpl(base, patches)
@@ -239,9 +241,9 @@ export function createProxy<T extends Objectish>(
239241
// returning a tuple here lets us skip a proxy access
240242
// to DRAFT_STATE later
241243
const [draft, state] = isMap(value)
242-
? getPlugin("MapSet").proxyMap_(value, parent)
244+
? getPlugin(PluginMapSet).proxyMap_(value, parent)
243245
: isSet(value)
244-
? getPlugin("MapSet").proxySet_(value, parent)
246+
? getPlugin(PluginMapSet).proxySet_(value, parent)
245247
: createProxyProxy(value, parent)
246248

247249
const scope = parent?.scope_ ?? getCurrentScope()
@@ -257,7 +259,7 @@ export function createProxy<T extends Objectish>(
257259
} else {
258260
// It's a root draft, register it with the scope
259261
state.callbacks_.push(function rootDraftCleanup(rootScope) {
260-
rootScope.mapSetPlugin_?.fixPotentialSetContents(state)
262+
rootScope.mapSetPlugin_?.fixSetContents(state)
261263

262264
const {patchPlugin_} = rootScope
263265

src/core/proxy.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
createProxy,
1919
ArchType,
2020
ImmerScope,
21-
handleCrossReference
21+
handleCrossReference,
22+
WRITABLE,
23+
CONFIGURABLE,
24+
ENUMERABLE,
25+
VALUE,
26+
isArray
2227
} from "../internal"
2328

2429
interface ProxyBaseState extends ImmerBaseState {
@@ -51,9 +56,9 @@ export function createProxyProxy<T extends Objectish>(
5156
base: T,
5257
parent?: ImmerState
5358
): [Drafted<T, ProxyState>, ProxyState] {
54-
const isArray = Array.isArray(base)
59+
const baseIsArray = isArray(base)
5560
const state: ProxyState = {
56-
type_: isArray ? ArchType.Array : (ArchType.Object as any),
61+
type_: baseIsArray ? ArchType.Array : (ArchType.Object as any),
5762
// Track which produce call this is associated with.
5863
scope_: parent ? parent.scope_ : getCurrentScope()!,
5964
// True for both shallow and deep changes.
@@ -86,7 +91,7 @@ export function createProxyProxy<T extends Objectish>(
8691
// Note that in the case of an array, we put the state in an array to have better Reflect defaults ootb
8792
let target: T = state as any
8893
let traps: ProxyHandler<object | Array<any>> = objectTraps
89-
if (isArray) {
94+
if (baseIsArray) {
9095
target = [state] as any
9196
traps = arrayTraps
9297
}
@@ -201,10 +206,10 @@ export const objectTraps: ProxyHandler<ProxyState> = {
201206
const desc = Reflect.getOwnPropertyDescriptor(owner, prop)
202207
if (!desc) return desc
203208
return {
204-
writable: true,
205-
configurable: state.type_ !== ArchType.Array || prop !== "length",
206-
enumerable: desc.enumerable,
207-
value: owner[prop]
209+
[WRITABLE]: true,
210+
[CONFIGURABLE]: state.type_ !== ArchType.Array || prop !== "length",
211+
[ENUMERABLE]: desc[ENUMERABLE],
212+
[VALUE]: owner[prop]
208213
}
209214
},
210215
defineProperty() {
@@ -226,8 +231,9 @@ const arrayTraps: ProxyHandler<[ProxyArrayState]> = {}
226231
each(objectTraps, (key, fn) => {
227232
// @ts-ignore
228233
arrayTraps[key] = function() {
229-
arguments[0] = arguments[0][0]
230-
return fn.apply(this, arguments)
234+
const args = arguments
235+
args[0] = args[0][0]
236+
return fn.apply(this, args)
231237
}
232238
})
233239
arrayTraps.deleteProperty = function(state, prop) {
@@ -256,8 +262,8 @@ function peek(draft: Drafted, prop: PropertyKey) {
256262
function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {
257263
const desc = getDescriptorFromProto(source, prop)
258264
return desc
259-
? `value` in desc
260-
? desc.value
265+
? VALUE in desc
266+
? desc[VALUE]
261267
: // This is a very special case, if the prop is a getter defined by the
262268
// prototype, we should invoke it with the draft as context!
263269
desc.get?.call(state.draft_)

src/core/scope.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
getPlugin,
1010
PatchesPlugin,
1111
MapSetPlugin,
12-
isPluginLoaded
12+
isPluginLoaded,
13+
PluginMapSet,
14+
PluginPatches
1315
} from "../internal"
1416

1517
/** Each scope represents a `produce` call. */
@@ -31,34 +33,32 @@ export interface ImmerScope {
3133

3234
let currentScope: ImmerScope | undefined
3335

34-
export function getCurrentScope() {
35-
return currentScope!
36-
}
36+
export let getCurrentScope = () => currentScope!
3737

38-
function createScope(
38+
let createScope = (
3939
parent_: ImmerScope | undefined,
4040
immer_: Immer
41-
): ImmerScope {
42-
return {
43-
drafts_: [],
44-
parent_,
45-
immer_,
46-
// Whenever the modified draft contains a draft from another scope, we
47-
// need to prevent auto-freezing so the unowned draft can be finalized.
48-
canAutoFreeze_: true,
49-
unfinalizedDrafts_: 0,
50-
handledSet_: new Set(),
51-
processedForPatches_: new Set(),
52-
mapSetPlugin_: isPluginLoaded("MapSet") ? getPlugin("MapSet") : undefined
53-
}
54-
}
41+
): ImmerScope => ({
42+
drafts_: [],
43+
parent_,
44+
immer_,
45+
// Whenever the modified draft contains a draft from another scope, we
46+
// need to prevent auto-freezing so the unowned draft can be finalized.
47+
canAutoFreeze_: true,
48+
unfinalizedDrafts_: 0,
49+
handledSet_: new Set(),
50+
processedForPatches_: new Set(),
51+
mapSetPlugin_: isPluginLoaded(PluginMapSet)
52+
? getPlugin(PluginMapSet)
53+
: undefined
54+
})
5555

5656
export function usePatchesInScope(
5757
scope: ImmerScope,
5858
patchListener?: PatchListener
5959
) {
6060
if (patchListener) {
61-
scope.patchPlugin_ = getPlugin("Patches") // assert we have the plugin
61+
scope.patchPlugin_ = getPlugin(PluginPatches) // assert we have the plugin
6262
scope.patches_ = []
6363
scope.inversePatches_ = []
6464
scope.patchListener_ = patchListener
@@ -78,9 +78,8 @@ export function leaveScope(scope: ImmerScope) {
7878
}
7979
}
8080

81-
export function enterScope(immer: Immer) {
82-
return (currentScope = createScope(currentScope, immer))
83-
}
81+
export let enterScope = (immer: Immer) =>
82+
(currentScope = createScope(currentScope, immer))
8483

8584
function revokeDraft(draft: Drafted) {
8685
const state: ImmerState = draft[DRAFT_STATE]

src/immer.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,14 @@ export const finishDraft = /* @__PURE__ */ immer.finishDraft.bind(immer)
110110
*
111111
* @param value
112112
*/
113-
export function castDraft<T>(value: T): Draft<T> {
114-
return value as any
115-
}
113+
export let castDraft = <T>(value: T): Draft<T> => value as any
116114

117115
/**
118116
* This function is actually a no-op, but can be used to cast a mutable type
119117
* to an immutable type and make TypeScript happy
120118
* @param value
121119
*/
122-
export function castImmutable<T>(value: T): Immutable<T> {
123-
return value as any
124-
}
120+
export let castImmutable = <T>(value: T): Immutable<T> => value as any
125121

126122
export {Immer}
127123

src/plugins/mapset.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
die,
1616
ArchType,
1717
each,
18-
getValue
18+
getValue,
19+
PluginMapSet
1920
} from "../internal"
2021

2122
export function enableMapSet() {
@@ -312,7 +313,7 @@ export function enableMapSet() {
312313
if (state.revoked_) die(3, JSON.stringify(latest(state)))
313314
}
314315

315-
function fixPotentialSetContents(target: ImmerState) {
316+
function fixSetContents(target: ImmerState) {
316317
// For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628
317318
// To preserve insertion order in all cases we then clear the set
318319
if (target.type_ === ArchType.Set && target.copy_) {
@@ -324,5 +325,5 @@ export function enableMapSet() {
324325
}
325326
}
326327

327-
loadPlugin("MapSet", {proxyMap_, proxySet_, fixPotentialSetContents})
328+
loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})
328329
}

src/plugins/patches.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
getProxyDraft,
2626
ImmerScope,
2727
isObjectish,
28-
isFunction
28+
isFunction,
29+
CONSTRUCTOR,
30+
PluginPatches,
31+
isArray,
32+
PROTOTYPE
2933
} from "../internal"
3034

3135
export function enablePatches() {
@@ -332,10 +336,10 @@ export function enablePatches() {
332336
// See #738, avoid prototype pollution
333337
if (
334338
(parentType === ArchType.Object || parentType === ArchType.Array) &&
335-
(p === "__proto__" || p === "constructor")
339+
(p === "__proto__" || p === CONSTRUCTOR)
336340
)
337341
die(errorOffset + 3)
338-
if (isFunction(base) && p === "prototype") die(errorOffset + 3)
342+
if (isFunction(base) && p === PROTOTYPE) die(errorOffset + 3)
339343
base = get(base, p)
340344
if (!isObjectish(base)) die(errorOffset + 2, path.join("/"))
341345
}
@@ -396,7 +400,7 @@ export function enablePatches() {
396400
function deepClonePatchValue<T>(obj: T): T
397401
function deepClonePatchValue(obj: any) {
398402
if (!isDraftable(obj)) return obj
399-
if (Array.isArray(obj)) return obj.map(deepClonePatchValue)
403+
if (isArray(obj)) return obj.map(deepClonePatchValue)
400404
if (isMap(obj))
401405
return new Map(
402406
Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
@@ -414,7 +418,7 @@ export function enablePatches() {
414418
} else return obj
415419
}
416420

417-
loadPlugin("Patches", {
421+
loadPlugin(PluginPatches, {
418422
applyPatches_,
419423
generatePatches_,
420424
generateReplacementPatches_,

0 commit comments

Comments
 (0)