Skip to content

Commit e4502f6

Browse files
authored
remove phantom type identifying state (#100)
1 parent 963af60 commit e4502f6

File tree

6 files changed

+19
-37
lines changed

6 files changed

+19
-37
lines changed

src/apiState.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ export type MutationSubState<D extends BaseEndpointDefinition<any, any, any>> =
149149
fulfilledTimeStamp?: undefined;
150150
};
151151

152-
export type CombinedState<D extends EndpointDefinitions, E extends string> = {
152+
export type CombinedState<D extends EndpointDefinitions, E extends string, ReducerPath extends string> = {
153153
queries: QueryState<D>;
154154
mutations: MutationState<D>;
155155
provided: InvalidationState<E>;
156156
subscriptions: SubscriptionState;
157-
config: ConfigState;
157+
config: ConfigState<ReducerPath>;
158158
};
159159

160160
export type InvalidationState<EntityTypes extends string> = {
@@ -172,7 +172,8 @@ export type SubscriptionState = {
172172
[queryCacheKey: string]: Subscribers | undefined;
173173
};
174174

175-
export type ConfigState = {
175+
export type ConfigState<ReducerPath> = RefetchConfigOptions & {
176+
reducerPath: ReducerPath;
176177
online: boolean;
177178
focused: boolean;
178179
} & ModifiableConfigState;
@@ -185,19 +186,10 @@ export type MutationState<D extends EndpointDefinitions> = {
185186
[requestId: string]: MutationSubState<D[string]> | undefined;
186187
};
187188

188-
const __phantomType_ReducerPath = Symbol();
189-
export interface QueryStatePhantomType<Identifier extends string> {
190-
[__phantomType_ReducerPath]: Identifier;
191-
}
192-
193189
export type RootState<
194190
Definitions extends EndpointDefinitions,
195191
EntityTypes extends string,
196192
ReducerPath extends string
197193
> = {
198-
[P in ReducerPath]: CombinedState<Definitions, EntityTypes> & QueryStatePhantomType<P>;
199-
};
200-
201-
export type InternalRootState<ReducerPath extends string> = {
202-
[_ in ReducerPath]: CombinedState<any, string>;
194+
[P in ReducerPath]: CombinedState<Definitions, EntityTypes, P>;
203195
};

src/apiTypes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
QueryDefinition,
1212
MutationDefinition,
1313
} from './endpointDefinitions';
14-
import { CombinedState, QueryKeys, QueryStatePhantomType, RootState } from './apiState';
14+
import { CombinedState, QueryKeys, RootState } from './apiState';
1515
import { UnionToIntersection } from './tsHelpers';
1616
import { TS41Hooks } from './ts41Types';
1717
import './buildSelectors';
@@ -63,7 +63,7 @@ export type Api<
6363
> = {
6464
reducerPath: ReducerPath;
6565
internalActions: InternalActions;
66-
reducer: Reducer<CombinedState<Definitions, EntityTypes> & QueryStatePhantomType<ReducerPath>, AnyAction>;
66+
reducer: Reducer<CombinedState<Definitions, EntityTypes, ReducerPath>, AnyAction>;
6767
middleware: Middleware<{}, RootState<Definitions, string, ReducerPath>, ThunkDispatch<any, any, AnyAction>>;
6868
util: {
6969
updateQueryResult: UpdateQueryResultThunk<Definitions, RootState<Definitions, string, ReducerPath>>;

src/buildSelectors.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
EntityTypesFrom,
1616
ReducerPathFrom,
1717
} from './endpointDefinitions';
18-
import type { InternalState } from './buildSlice';
1918
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
2019

2120
export const skipSelector = Symbol('skip selector');
@@ -77,7 +76,7 @@ export function buildSelectors<InternalQueryArgs, Definitions extends EndpointDe
7776
}
7877

7978
function selectInternalState(rootState: RootState) {
80-
return rootState[reducerPath] as InternalState;
79+
return rootState[reducerPath];
8180
}
8281

8382
function buildQuerySelector(

src/buildSlice.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { applyPatches, Patch } from 'immer';
2020
import { onFocus, onFocusLost, onOffline, onOnline } from './setupListeners';
2121
import { isDocumentVisible, isOnline } from './utils';
2222

23-
export type InternalState = CombinedState<any, string>;
24-
2523
function updateQuerySubstateIfExists(
2624
state: QueryState<any>,
2725
queryCacheKey: QueryCacheKey,
@@ -57,7 +55,7 @@ export function buildSlice({
5755
mutationThunk: AsyncThunk<ThunkResult, MutationThunkArg<any>, {}>;
5856
endpointDefinitions: EndpointDefinitions;
5957
assertEntityType: AssertEntityTypes;
60-
config: Omit<ConfigState, 'online' | 'focused'>;
58+
config: Omit<ConfigState<string>, 'online' | 'focused'>;
6159
}) {
6260
const querySlice = createSlice({
6361
name: `${reducerPath}/queries`,
@@ -247,7 +245,7 @@ export function buildSlice({
247245
online: isOnline(),
248246
focused: isDocumentVisible(),
249247
...config,
250-
} as ConfigState,
248+
} as ConfigState<string>,
251249
reducers: {},
252250
extraReducers: (builder) => {
253251
builder
@@ -266,7 +264,7 @@ export function buildSlice({
266264
},
267265
});
268266

269-
const reducer = combineReducers<InternalState>({
267+
const reducer = combineReducers<CombinedState<any, string, string>>({
270268
queries: querySlice.reducer,
271269
mutations: mutationSlice.reducer,
272270
provided: invalidationSlice.reducer,

src/buildThunks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
22
import { Api, ApiEndpointQuery, BaseQueryFn, BaseQueryArg, BaseQueryError } from './apiTypes';
3-
import { InternalRootState, QueryKeys, QueryStatus, QuerySubstateIdentifier } from './apiState';
3+
import { RootState, QueryKeys, QueryStatus, QuerySubstateIdentifier } from './apiState';
44
import { StartQueryActionCreatorOptions } from './buildActionMaps';
55
import {
66
EndpointDefinition,
@@ -144,7 +144,7 @@ export function buildThunks<
144144
api: Api<BaseQuery, Definitions, ReducerPath, string>;
145145
}) {
146146
type InternalQueryArgs = BaseQueryArg<BaseQuery>;
147-
type State = InternalRootState<ReducerPath>;
147+
type State = RootState<any, string, ReducerPath>;
148148

149149
const patchQueryResult: PatchQueryResultThunk<EndpointDefinitions, State> = (endpointName, args, patches) => (
150150
dispatch
@@ -193,7 +193,7 @@ export function buildThunks<
193193
const queryThunk = createAsyncThunk<
194194
ThunkResult,
195195
QueryThunkArg<InternalQueryArgs>,
196-
{ state: InternalRootState<ReducerPath> }
196+
{ state: RootState<any, string, ReducerPath> }
197197
>(
198198
`${reducerPath}/executeQuery`,
199199
async (arg, { signal, rejectWithValue, dispatch, getState }) => {
@@ -240,7 +240,7 @@ export function buildThunks<
240240
const mutationThunk = createAsyncThunk<
241241
ThunkResult,
242242
MutationThunkArg<InternalQueryArgs>,
243-
{ state: InternalRootState<ReducerPath> }
243+
{ state: RootState<any, string, ReducerPath> }
244244
>(`${reducerPath}/executeMutation`, async (arg, { signal, rejectWithValue, ...api }) => {
245245
const endpoint = endpointDefinitions[arg.endpoint] as MutationDefinition<any, any, any, any>;
246246

src/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AnyAction, Reducer } from '@reduxjs/toolkit';
2-
import type { CombinedState, ModifiableConfigState, QueryStatePhantomType } from './apiState';
2+
import type { CombinedState } from './apiState';
33
import { Api, BaseQueryArg, BaseQueryFn } from './apiTypes';
44
import { buildActionMaps } from './buildActionMaps';
55
import { buildHooks } from './buildHooks';
@@ -53,7 +53,7 @@ export function createApi<
5353
refetchOnFocus?: boolean;
5454
refetchOnReconnect?: boolean;
5555
}): Api<BaseQuery, Definitions, ReducerPath, EntityTypes> {
56-
type State = CombinedState<Definitions, EntityTypes>;
56+
type State = CombinedState<Definitions, EntityTypes, ReducerPath>;
5757

5858
type InternalQueryArgs = BaseQueryArg<BaseQuery>;
5959

@@ -115,22 +115,15 @@ export function createApi<
115115
});
116116
Object.assign(api.util, { patchQueryResult, updateQueryResult });
117117

118-
const config: ModifiableConfigState = {
119-
refetchOnFocus,
120-
refetchOnReconnect,
121-
refetchOnMountOrArgChange,
122-
keepUnusedDataFor,
123-
};
124-
125118
const { reducer, actions: sliceActions } = buildSlice({
126119
endpointDefinitions,
127120
queryThunk,
128121
mutationThunk,
129122
reducerPath,
130123
assertEntityType,
131-
config,
124+
config: { refetchOnFocus, refetchOnReconnect, refetchOnMountOrArgChange, keepUnusedDataFor, reducerPath },
132125
});
133-
assertCast<Reducer<State & QueryStatePhantomType<ReducerPath>, AnyAction>>(reducer);
126+
assertCast<Reducer<State, AnyAction>>(reducer);
134127
Object.assign(api.internalActions, sliceActions);
135128
api.reducer = reducer;
136129

0 commit comments

Comments
 (0)