| 1 |
import type { |
| 2 |
DynamicMiddlewareInstance, |
| 3 |
GetDispatch, |
| 4 |
GetState, |
| 5 |
MiddlewareApiConfig, |
| 6 |
TSHelpersExtractDispatchExtensions, |
| 7 |
} from '@reduxjs/toolkit' |
| 8 |
import { createDynamicMiddleware as cDM } from '@reduxjs/toolkit' |
| 9 |
import type { Context } from 'react' |
| 10 |
import type { ReactReduxContextValue } from 'react-redux' |
| 11 |
import { |
| 12 |
createDispatchHook, |
| 13 |
ReactReduxContext, |
| 14 |
useDispatch as useDefaultDispatch, |
| 15 |
} from 'react-redux' |
| 16 |
import type { Action, Dispatch, Middleware, UnknownAction } from 'redux' |
| 17 |
|
| 18 |
export type UseDispatchWithMiddlewareHook< |
| 19 |
Middlewares extends Middleware<any, State, DispatchType>[] = [], |
| 20 |
State = any, |
| 21 |
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>, |
| 22 |
> = () => TSHelpersExtractDispatchExtensions<Middlewares> & DispatchType |
| 23 |
|
| 24 |
export type CreateDispatchWithMiddlewareHook< |
| 25 |
State = any, |
| 26 |
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>, |
| 27 |
> = { |
| 28 |
< |
| 29 |
Middlewares extends [ |
| 30 |
Middleware<any, State, DispatchType>, |
| 31 |
...Middleware<any, State, DispatchType>[], |
| 32 |
], |
| 33 |
>( |
| 34 |
...middlewares: Middlewares |
| 35 |
): UseDispatchWithMiddlewareHook<Middlewares, State, DispatchType> |
| 36 |
withTypes< |
| 37 |
MiddlewareConfig extends MiddlewareApiConfig, |
| 38 |
>(): CreateDispatchWithMiddlewareHook< |
| 39 |
GetState<MiddlewareConfig>, |
| 40 |
GetDispatch<MiddlewareConfig> |
| 41 |
> |
| 42 |
} |
| 43 |
|
| 44 |
type ActionFromDispatch<DispatchType extends Dispatch<Action>> = |
| 45 |
DispatchType extends Dispatch<infer Action> ? Action : never |
| 46 |
|
| 47 |
type ReactDynamicMiddlewareInstance< |
| 48 |
State = any, |
| 49 |
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>, |
| 50 |
> = DynamicMiddlewareInstance<State, DispatchType> & { |
| 51 |
createDispatchWithMiddlewareHookFactory: ( |
| 52 |
context?: Context<ReactReduxContextValue< |
| 53 |
State, |
| 54 |
ActionFromDispatch<DispatchType> |
| 55 |
> | null>, |
| 56 |
) => CreateDispatchWithMiddlewareHook<State, DispatchType> |
| 57 |
createDispatchWithMiddlewareHook: CreateDispatchWithMiddlewareHook< |
| 58 |
State, |
| 59 |
DispatchType |
| 60 |
> |
| 61 |
} |
| 62 |
|
| 63 |
export const createDynamicMiddleware = < |
| 64 |
State = any, |
| 65 |
DispatchType extends Dispatch<UnknownAction> = Dispatch<UnknownAction>, |
| 66 |
>(): ReactDynamicMiddlewareInstance<State, DispatchType> => { |
| 67 |
const instance = cDM<State, DispatchType>() |
| 68 |
const createDispatchWithMiddlewareHookFactory = ( |
| 69 |
// @ts-ignore |
| 70 |
context: Context<ReactReduxContextValue< |
| 71 |
State, |
| 72 |
ActionFromDispatch<DispatchType> |
| 73 |
> | null> = ReactReduxContext, |
| 74 |
) => { |
| 75 |
const useDispatch = |
| 76 |
context === ReactReduxContext |
| 77 |
? useDefaultDispatch |
| 78 |
: createDispatchHook(context) |
| 79 |
function createDispatchWithMiddlewareHook< |
| 80 |
Middlewares extends Middleware<any, State, DispatchType>[], |
| 81 |
>(...middlewares: Middlewares) { |
| 82 |
instance.addMiddleware(...middlewares) |
| 83 |
return useDispatch |
| 84 |
} |
| 85 |
createDispatchWithMiddlewareHook.withTypes = () => |
| 86 |
createDispatchWithMiddlewareHook |
| 87 |
return createDispatchWithMiddlewareHook as CreateDispatchWithMiddlewareHook< |
| 88 |
State, |
| 89 |
DispatchType |
| 90 |
> |
| 91 |
} |
| 92 |
|
| 93 |
const createDispatchWithMiddlewareHook = |
| 94 |
createDispatchWithMiddlewareHookFactory() |
| 95 |
|
| 96 |
return { |
| 97 |
...instance, |
| 98 |
createDispatchWithMiddlewareHookFactory, |
| 99 |
createDispatchWithMiddlewareHook, |
| 100 |
} |
| 101 |
} |