Skip to content

Commit

Permalink
Move internalActions.prefetchThunk to util.prefetchThunk (#133)
Browse files Browse the repository at this point in the history
* Move `internalActions.prefetchThunk` to `util.prefetchThunk`
* Docs: Move prefetchThunk to util

Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>
  • Loading branch information
phryneas and msutkowski authored Dec 26, 2020
1 parent 1704dc1 commit f8e10d8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
25 changes: 3 additions & 22 deletions docs/api/createApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,31 +385,12 @@ These may change at any given time and are not part of the public API for now
- `removeQueryResult: ActionCreatorWithPayload<{ queryCacheKey: QueryCacheKey }, string>`
- `unsubscribeQueryResult: ActionCreatorWithPayload<{ queryCacheKey: QueryCacheKey, requestId: string }, string>`,
- `unsubscribeMutationResult: ActionCreatorWithPayload<MutationSubstateIdentifier, string>`,
- `prefetchThunk(endpointName, args, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>`

### `util`

Both of these utils are currently used for [optimistic updates](../concepts/optimistic-updates).

- **patchQueryResult**

```ts
<EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>,
patches: Patch[]
) => ThunkAction<void, PartialState, any, AnyAction>
```

- **updateQueryResult**

```ts
<EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>,
updateRecicpe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>
```
- **prefetchThunk** - used for [prefetching](../concepts/prefetching).
- **patchQueryResult** - used for [optimistic updates](../concepts/optimistic-updates).
- **updateQueryResult** - used for [optimistic updates](../concepts/optimistic-updates).

### `injectEndpoints`

Expand Down
4 changes: 2 additions & 2 deletions docs/concepts/prefetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function usePrefetchImmediately<T extends EndpointNames>(
) {
const dispatch = useDispatch();
useEffect(() => {
dispatch(api.internalActions.prefetchThunk(endpoint, arg, options));
dispatch(api.util.prefetchThunk(endpoint, arg, options));
}, []);
}

Expand All @@ -104,7 +104,7 @@ If you're not using the `usePrefetch` hook, you can recreate the same behavior e
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).

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

You can also dispatch the query action, but you would be responsible for implementing any additional logic.
Expand Down
11 changes: 7 additions & 4 deletions src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ declare module '../apiTypes' {
reducer: Reducer<CombinedState<Definitions, EntityTypes, ReducerPath>, AnyAction>;
middleware: Middleware<{}, RootState<Definitions, string, ReducerPath>, ThunkDispatch<any, any, AnyAction>>;
util: {
prefetchThunk<EndpointName extends QueryKeys<EndpointDefinitions>>(
endpointName: EndpointName,
arg: QueryArgFrom<Definitions[EndpointName]>,
options: PrefetchOptions
): ThunkAction<void, any, any, AnyAction>;
updateQueryResult: UpdateQueryResultThunk<Definitions, RootState<Definitions, string, ReducerPath>>;
patchQueryResult: PatchQueryResultThunk<Definitions, RootState<Definitions, string, ReducerPath>>;
};
Expand Down Expand Up @@ -78,8 +83,6 @@ export interface ApiEndpointMutation<
> {}

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

safeAssign(api.util, { patchQueryResult, updateQueryResult });
safeAssign(api.internalActions, sliceActions, { prefetchThunk: prefetchThunk as any });
safeAssign(api.util, { patchQueryResult, updateQueryResult, prefetchThunk });
safeAssign(api.internalActions, sliceActions);

const { middleware } = buildMiddleware({
reducerPath,
Expand Down
12 changes: 10 additions & 2 deletions src/react-hooks/buildHooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyAction, createSelector, ThunkDispatch } from '@reduxjs/toolkit';
import { AnyAction, createSelector, ThunkAction, ThunkDispatch } from '@reduxjs/toolkit';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useDispatch, useSelector, batch, shallowEqual } from 'react-redux';
import {
Expand Down Expand Up @@ -143,6 +143,12 @@ const defaultQueryStateSelector: DefaultQueryStateSelector<any> = (currentState,
return { ...currentState, data, isFetching, isLoading, isSuccess } as UseQueryStateDefaultResult<any>;
};

type GenericPrefetchThunk = (
endpointName: any,
arg: any,
options: PrefetchOptions
) => ThunkAction<void, any, any, AnyAction>;

export function buildHooks<Definitions extends EndpointDefinitions>({
api,
}: {
Expand All @@ -159,7 +165,9 @@ export function buildHooks<Definitions extends EndpointDefinitions>({

return useCallback(
(arg: any, options?: PrefetchOptions) =>
dispatch(api.internalActions.prefetchThunk(endpointName, arg, { ...stableDefaultOptions, ...options })),
dispatch(
(api.util.prefetchThunk as GenericPrefetchThunk)(endpointName, arg, { ...stableDefaultOptions, ...options })
),
[endpointName, dispatch, stableDefaultOptions]
);
}
Expand Down

0 comments on commit f8e10d8

Please sign in to comment.