source: node_modules/redux-thunk/src/index.ts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[a762898]1import type { Action, AnyAction } from 'redux'
2
3import type { ThunkMiddleware } from './types'
4
5export type {
6 ThunkAction,
7 ThunkDispatch,
8 ThunkActionDispatch,
9 ThunkMiddleware
10} from './types'
11
12/** A function that accepts a potential "extra argument" value to be injected later,
13 * and returns an instance of the thunk middleware that uses that value
14 */
15function createThunkMiddleware<
16 State = any,
17 BasicAction extends Action = AnyAction,
18 ExtraThunkArg = undefined
19>(extraArgument?: ExtraThunkArg) {
20 // Standard Redux middleware definition pattern:
21 // See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
22 const middleware: ThunkMiddleware<State, BasicAction, ExtraThunkArg> =
23 ({ dispatch, getState }) =>
24 next =>
25 action => {
26 // The thunk middleware looks for any functions that were passed to `store.dispatch`.
27 // If this "action" is really a function, call it and return the result.
28 if (typeof action === 'function') {
29 // Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
30 return action(dispatch, getState, extraArgument)
31 }
32
33 // Otherwise, pass the action down the middleware chain as usual
34 return next(action)
35 }
36 return middleware
37}
38
39export const thunk = createThunkMiddleware()
40
41// Export the factory function so users can create a customized version
42// with whatever "extra arg" they want to inject into their thunks
43export const withExtraArgument = createThunkMiddleware
Note: See TracBrowser for help on using the repository browser.