Skip to content

Commit 5bb06ed

Browse files
committed
Deduplicate TS enums with string constants
1 parent 20bc81b commit 5bb06ed

File tree

8 files changed

+51
-31
lines changed

8 files changed

+51
-31
lines changed

packages/toolkit/src/query/core/apiState.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export type InfiniteData<DataType, PageParam> = {
6565
pageParams: Array<PageParam>
6666
}
6767

68+
// NOTE: DO NOT import and use this for runtime comparisons internally,
69+
// except in the RTKQ React package. Use the string versions just below this.
70+
// ESBuild auto-inlines TS enums, which bloats our bundle with many repeated
71+
// constants like "initialized":
72+
// https://github.com/evanw/esbuild/releases/tag/v0.14.7
73+
// We still have to use this in the React package since we don't publicly export
74+
// the string constants below.
6875
/**
6976
* Strings describing the query state at any given time.
7077
*/
@@ -75,6 +82,12 @@ export enum QueryStatus {
7582
rejected = 'rejected',
7683
}
7784

85+
// Use these string constants for runtime comparisons internally
86+
export const STATUS_UNINITIALIZED = QueryStatus.uninitialized
87+
export const STATUS_PENDING = QueryStatus.pending
88+
export const STATUS_FULFILLED = QueryStatus.fulfilled
89+
export const STATUS_REJECTED = QueryStatus.rejected
90+
7891
export type RequestStatusFlags =
7992
| {
8093
status: QueryStatus.uninitialized
@@ -108,10 +121,10 @@ export type RequestStatusFlags =
108121
export function getRequestStatusFlags(status: QueryStatus): RequestStatusFlags {
109122
return {
110123
status,
111-
isUninitialized: status === QueryStatus.uninitialized,
112-
isLoading: status === QueryStatus.pending,
113-
isSuccess: status === QueryStatus.fulfilled,
114-
isError: status === QueryStatus.rejected,
124+
isUninitialized: status === STATUS_UNINITIALIZED,
125+
isLoading: status === STATUS_PENDING,
126+
isSuccess: status === STATUS_FULFILLED,
127+
isError: status === STATUS_REJECTED,
115128
} as any
116129
}
117130

packages/toolkit/src/query/core/buildMiddleware/invalidationByTags.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
} from '../../endpointDefinitions'
1212
import { calculateProvidedBy } from '../../endpointDefinitions'
1313
import type { CombinedState, QueryCacheKey } from '../apiState'
14-
import { QueryStatus } from '../apiState'
14+
import { QueryStatus, STATUS_UNINITIALIZED } from '../apiState'
1515
import { calculateProvidedByThunk } from '../buildThunks'
1616
import type {
1717
SubMiddlewareApi,
@@ -127,7 +127,7 @@ export const buildInvalidationByTagsHandler: InternalHandlerBuilder = ({
127127
queryCacheKey: queryCacheKey as QueryCacheKey,
128128
}),
129129
)
130-
} else if (querySubState.status !== QueryStatus.uninitialized) {
130+
} else if (querySubState.status !== STATUS_UNINITIALIZED) {
131131
mwApi.dispatch(refetchQuery(querySubState))
132132
}
133133
}

packages/toolkit/src/query/core/buildMiddleware/polling.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {
44
Subscribers,
55
SubscribersInternal,
66
} from '../apiState'
7-
import { QueryStatus } from '../apiState'
7+
import { QueryStatus, STATUS_UNINITIALIZED } from '../apiState'
88
import type {
99
QueryStateMeta,
1010
SubMiddlewareApi,
@@ -83,8 +83,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
8383
const querySubState = state.queries[queryCacheKey]
8484
const subscriptions = currentSubscriptions.get(queryCacheKey)
8585

86-
if (!querySubState || querySubState.status === QueryStatus.uninitialized)
87-
return
86+
if (!querySubState || querySubState.status === STATUS_UNINITIALIZED) return
8887

8988
const { lowestPollingInterval, skipPollingIfUnfocused } =
9089
findLowestPollingInterval(subscriptions)
@@ -119,7 +118,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
119118
const querySubState = state.queries[queryCacheKey]
120119
const subscriptions = currentSubscriptions.get(queryCacheKey)
121120

122-
if (!querySubState || querySubState.status === QueryStatus.uninitialized) {
121+
if (!querySubState || querySubState.status === STATUS_UNINITIALIZED) {
123122
return
124123
}
125124

packages/toolkit/src/query/core/buildMiddleware/windowEventHandling.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QueryStatus } from '../apiState'
1+
import { QueryStatus, STATUS_UNINITIALIZED } from '../apiState'
22
import type { QueryCacheKey } from '../apiState'
33
import { onFocus, onOnline } from '../setupListeners'
44
import type {
@@ -53,7 +53,7 @@ export const buildWindowEventHandler: InternalHandlerBuilder = ({
5353
queryCacheKey: queryCacheKey as QueryCacheKey,
5454
}),
5555
)
56-
} else if (querySubState.status !== QueryStatus.uninitialized) {
56+
} else if (querySubState.status !== STATUS_UNINITIALIZED) {
5757
api.dispatch(refetchQuery(querySubState))
5858
}
5959
}

packages/toolkit/src/query/core/buildSelectors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import type {
2020
InfiniteQuerySubState,
2121
MutationSubState,
2222
QueryCacheKey,
23-
QueryKeys,
2423
QueryState,
2524
QuerySubState,
2625
RequestStatusFlags,
2726
RootState as _RootState,
27+
QueryStatus,
2828
} from './apiState'
29-
import { QueryStatus, getRequestStatusFlags } from './apiState'
29+
import { STATUS_UNINITIALIZED, getRequestStatusFlags } from './apiState'
3030
import { getMutationCacheKey } from './buildSlice'
3131
import type { createSelector as _createSelector } from './rtkImports'
3232
import { createNextState } from './rtkImports'
@@ -151,7 +151,7 @@ export type MutationResultSelectorResult<
151151
> = MutationSubState<Definition> & RequestStatusFlags
152152

153153
const initialSubState: QuerySubState<any> = {
154-
status: QueryStatus.uninitialized as const,
154+
status: STATUS_UNINITIALIZED,
155155
}
156156

157157
// abuse immer to freeze default states
@@ -388,7 +388,7 @@ export function buildSelectors<
388388
{ status: QueryStatus.uninitialized }
389389
> =>
390390
entry?.endpointName === queryName &&
391-
entry.status !== QueryStatus.uninitialized,
391+
entry.status !== STATUS_UNINITIALIZED,
392392
(entry) => entry.originalArgs,
393393
)
394394
}

packages/toolkit/src/query/core/buildSlice.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ import type {
2626
InfiniteQuerySubState,
2727
InfiniteQueryDirection,
2828
} from './apiState'
29-
import { QueryStatus } from './apiState'
29+
import {
30+
STATUS_FULFILLED,
31+
STATUS_PENDING,
32+
QueryStatus,
33+
STATUS_REJECTED,
34+
STATUS_UNINITIALIZED,
35+
} from './apiState'
3036
import type {
3137
AllQueryKeys,
3238
QueryArgFromAnyQueryDefinition,
@@ -46,7 +52,7 @@ import {
4652
} from '../endpointDefinitions'
4753
import type { Patch } from 'immer'
4854
import { isDraft } from 'immer'
49-
import { applyPatches, original } from 'immer'
55+
import { applyPatches, original } from '../utils/immerImports'
5056
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners'
5157
import {
5258
isDocumentVisible,
@@ -189,12 +195,12 @@ export function buildSlice({
189195
} & { startedTimeStamp: number },
190196
) {
191197
draft[arg.queryCacheKey] ??= {
192-
status: QueryStatus.uninitialized,
198+
status: STATUS_UNINITIALIZED,
193199
endpointName: arg.endpointName,
194200
}
195201

196202
updateQuerySubstateIfExists(draft, arg.queryCacheKey, (substate) => {
197-
substate.status = QueryStatus.pending
203+
substate.status = STATUS_PENDING
198204

199205
substate.requestId =
200206
upserting && substate.requestId
@@ -233,7 +239,7 @@ export function buildSlice({
233239
any,
234240
any
235241
>
236-
substate.status = QueryStatus.fulfilled
242+
substate.status = STATUS_FULFILLED
237243

238244
if (merge) {
239245
if (substate.data !== undefined) {
@@ -390,7 +396,7 @@ export function buildSlice({
390396
} else {
391397
// request failed
392398
if (substate.requestId !== requestId) return
393-
substate.status = QueryStatus.rejected
399+
substate.status = STATUS_REJECTED
394400
substate.error = (payload ?? error) as any
395401
}
396402
},
@@ -402,8 +408,8 @@ export function buildSlice({
402408
for (const [key, entry] of Object.entries(queries)) {
403409
if (
404410
// do not rehydrate entries that were currently in flight.
405-
entry?.status === QueryStatus.fulfilled ||
406-
entry?.status === QueryStatus.rejected
411+
entry?.status === STATUS_FULFILLED ||
412+
entry?.status === STATUS_REJECTED
407413
) {
408414
draft[key] = entry
409415
}
@@ -434,7 +440,7 @@ export function buildSlice({
434440

435441
draft[getMutationCacheKey(meta)] = {
436442
requestId,
437-
status: QueryStatus.pending,
443+
status: STATUS_PENDING,
438444
endpointName: arg.endpointName,
439445
startedTimeStamp,
440446
}
@@ -445,7 +451,7 @@ export function buildSlice({
445451

446452
updateMutationSubstateIfExists(draft, meta, (substate) => {
447453
if (substate.requestId !== meta.requestId) return
448-
substate.status = QueryStatus.fulfilled
454+
substate.status = STATUS_FULFILLED
449455
substate.data = payload
450456
substate.fulfilledTimeStamp = meta.fulfilledTimeStamp
451457
})
@@ -456,7 +462,7 @@ export function buildSlice({
456462
updateMutationSubstateIfExists(draft, meta, (substate) => {
457463
if (substate.requestId !== meta.requestId) return
458464

459-
substate.status = QueryStatus.rejected
465+
substate.status = STATUS_REJECTED
460466
substate.error = (payload ?? error) as any
461467
})
462468
})
@@ -465,8 +471,8 @@ export function buildSlice({
465471
for (const [key, entry] of Object.entries(mutations)) {
466472
if (
467473
// do not rehydrate entries that were currently in flight.
468-
(entry?.status === QueryStatus.fulfilled ||
469-
entry?.status === QueryStatus.rejected) &&
474+
(entry?.status === STATUS_FULFILLED ||
475+
entry?.status === STATUS_REJECTED) &&
470476
// only rehydrate endpoints that were persisted using a `fixedCacheKey`
471477
key !== entry?.requestId
472478
) {

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import type {
5050
InfiniteQueryDirection,
5151
InfiniteQueryKeys,
5252
} from './apiState'
53-
import { QueryStatus } from './apiState'
53+
import { QueryStatus, STATUS_UNINITIALIZED } from './apiState'
5454
import type {
5555
InfiniteQueryActionCreatorResult,
5656
QueryActionCreatorResult,
@@ -425,7 +425,7 @@ export function buildThunks<
425425
),
426426
),
427427
}
428-
if (currentState.status === QueryStatus.uninitialized) {
428+
if (currentState.status === STATUS_UNINITIALIZED) {
429429
return ret
430430
}
431431
let newValue

packages/toolkit/src/query/react/buildHooks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,8 @@ const noPendingQueryStateSelector: QueryStateSelector<any, any> = (
14141414
isUninitialized: false,
14151415
isFetching: true,
14161416
isLoading: selected.data !== undefined ? false : true,
1417+
// This is the one place where we still have to use `QueryStatus` as an enum,
1418+
// since it's the only reference in the React package and not in the core.
14171419
status: QueryStatus.pending,
14181420
} as any
14191421
}

0 commit comments

Comments
 (0)