Skip to content

Commit f8e10d8

Browse files
phryneasmsutkowski
andauthored
Move internalActions.prefetchThunk to util.prefetchThunk (#133)
* Move `internalActions.prefetchThunk` to `util.prefetchThunk` * Docs: Move prefetchThunk to util Co-authored-by: Matt Sutkowski <[email protected]>
1 parent 1704dc1 commit f8e10d8

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

docs/api/createApi.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -385,31 +385,12 @@ These may change at any given time and are not part of the public API for now
385385
- `removeQueryResult: ActionCreatorWithPayload<{ queryCacheKey: QueryCacheKey }, string>`
386386
- `unsubscribeQueryResult: ActionCreatorWithPayload<{ queryCacheKey: QueryCacheKey, requestId: string }, string>`,
387387
- `unsubscribeMutationResult: ActionCreatorWithPayload<MutationSubstateIdentifier, string>`,
388-
- `prefetchThunk(endpointName, args, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>`
389388

390389
### `util`
391390

392-
Both of these utils are currently used for [optimistic updates](../concepts/optimistic-updates).
393-
394-
- **patchQueryResult**
395-
396-
```ts
397-
<EndpointName extends QueryKeys<Definitions>>(
398-
endpointName: EndpointName,
399-
args: QueryArgFrom<Definitions[EndpointName]>,
400-
patches: Patch[]
401-
) => ThunkAction<void, PartialState, any, AnyAction>
402-
```
403-
404-
- **updateQueryResult**
405-
406-
```ts
407-
<EndpointName extends QueryKeys<Definitions>>(
408-
endpointName: EndpointName,
409-
args: QueryArgFrom<Definitions[EndpointName]>,
410-
updateRecicpe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>
411-
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>
412-
```
391+
- **prefetchThunk** - used for [prefetching](../concepts/prefetching).
392+
- **patchQueryResult** - used for [optimistic updates](../concepts/optimistic-updates).
393+
- **updateQueryResult** - used for [optimistic updates](../concepts/optimistic-updates).
413394

414395
### `injectEndpoints`
415396

docs/concepts/prefetching.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export function usePrefetchImmediately<T extends EndpointNames>(
8989
) {
9090
const dispatch = useDispatch();
9191
useEffect(() => {
92-
dispatch(api.internalActions.prefetchThunk(endpoint, arg, options));
92+
dispatch(api.util.prefetchThunk(endpoint, arg, options));
9393
}, []);
9494
}
9595

@@ -104,7 +104,7 @@ If you're not using the `usePrefetch` hook, you can recreate the same behavior e
104104
When dispatching the `prefetchThunk` as shown below you will see the same exact behavior as [described here](#what-to-expect-when-you-call-the-callback).
105105

106106
```js title="Non-hook prefetching example"
107-
store.dispatch(api.internalActions.prefetchThunk(endpointName, arg, { force: false, ifOlderThan: 10 }));
107+
store.dispatch(api.util.prefetchThunk(endpointName, arg, { force: false, ifOlderThan: 10 }));
108108
```
109109

110110
You can also dispatch the query action, but you would be responsible for implementing any additional logic.

src/core/module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ declare module '../apiTypes' {
4343
reducer: Reducer<CombinedState<Definitions, EntityTypes, ReducerPath>, AnyAction>;
4444
middleware: Middleware<{}, RootState<Definitions, string, ReducerPath>, ThunkDispatch<any, any, AnyAction>>;
4545
util: {
46+
prefetchThunk<EndpointName extends QueryKeys<EndpointDefinitions>>(
47+
endpointName: EndpointName,
48+
arg: QueryArgFrom<Definitions[EndpointName]>,
49+
options: PrefetchOptions
50+
): ThunkAction<void, any, any, AnyAction>;
4651
updateQueryResult: UpdateQueryResultThunk<Definitions, RootState<Definitions, string, ReducerPath>>;
4752
patchQueryResult: PatchQueryResultThunk<Definitions, RootState<Definitions, string, ReducerPath>>;
4853
};
@@ -78,8 +83,6 @@ export interface ApiEndpointMutation<
7883
> {}
7984

8085
export type InternalActions = SliceActions & {
81-
prefetchThunk: (endpointName: any, arg: any, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>;
82-
} & {
8386
/**
8487
* Will cause the RTK Query middleware to trigger any refetchOnReconnect-related behavior
8588
* @link https://rtk-query-docs.netlify.app/api/setupListeners
@@ -157,8 +160,8 @@ export const coreModule: Module<CoreModule> = {
157160
config: { refetchOnFocus, refetchOnReconnect, refetchOnMountOrArgChange, keepUnusedDataFor, reducerPath },
158161
});
159162

160-
safeAssign(api.util, { patchQueryResult, updateQueryResult });
161-
safeAssign(api.internalActions, sliceActions, { prefetchThunk: prefetchThunk as any });
163+
safeAssign(api.util, { patchQueryResult, updateQueryResult, prefetchThunk });
164+
safeAssign(api.internalActions, sliceActions);
162165

163166
const { middleware } = buildMiddleware({
164167
reducerPath,

src/react-hooks/buildHooks.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AnyAction, createSelector, ThunkDispatch } from '@reduxjs/toolkit';
1+
import { AnyAction, createSelector, ThunkAction, ThunkDispatch } from '@reduxjs/toolkit';
22
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
33
import { useDispatch, useSelector, batch, shallowEqual } from 'react-redux';
44
import {
@@ -143,6 +143,12 @@ const defaultQueryStateSelector: DefaultQueryStateSelector<any> = (currentState,
143143
return { ...currentState, data, isFetching, isLoading, isSuccess } as UseQueryStateDefaultResult<any>;
144144
};
145145

146+
type GenericPrefetchThunk = (
147+
endpointName: any,
148+
arg: any,
149+
options: PrefetchOptions
150+
) => ThunkAction<void, any, any, AnyAction>;
151+
146152
export function buildHooks<Definitions extends EndpointDefinitions>({
147153
api,
148154
}: {
@@ -159,7 +165,9 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
159165

160166
return useCallback(
161167
(arg: any, options?: PrefetchOptions) =>
162-
dispatch(api.internalActions.prefetchThunk(endpointName, arg, { ...stableDefaultOptions, ...options })),
168+
dispatch(
169+
(api.util.prefetchThunk as GenericPrefetchThunk)(endpointName, arg, { ...stableDefaultOptions, ...options })
170+
),
163171
[endpointName, dispatch, stableDefaultOptions]
164172
);
165173
}

0 commit comments

Comments
 (0)