You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.3 KiB
32 lines
1.3 KiB
2 years ago
|
/** A function that accepts a potential "extra argument" value to be injected later,
|
||
|
* and returns an instance of the thunk middleware that uses that value
|
||
|
*/
|
||
|
function createThunkMiddleware(extraArgument) {
|
||
|
// Standard Redux middleware definition pattern:
|
||
|
// See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
|
||
|
var middleware = function middleware(_ref) {
|
||
|
var dispatch = _ref.dispatch,
|
||
|
getState = _ref.getState;
|
||
|
return function (next) {
|
||
|
return function (action) {
|
||
|
// The thunk middleware looks for any functions that were passed to `store.dispatch`.
|
||
|
// If this "action" is really a function, call it and return the result.
|
||
|
if (typeof action === 'function') {
|
||
|
// Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
|
||
|
return action(dispatch, getState, extraArgument);
|
||
|
} // Otherwise, pass the action down the middleware chain as usual
|
||
|
|
||
|
|
||
|
return next(action);
|
||
|
};
|
||
|
};
|
||
|
};
|
||
|
|
||
|
return middleware;
|
||
|
}
|
||
|
|
||
|
var thunk = createThunkMiddleware(); // Attach the factory function so users can create a customized version
|
||
|
// with whatever "extra arg" they want to inject into their thunks
|
||
|
|
||
|
thunk.withExtraArgument = createThunkMiddleware;
|
||
|
export default thunk;
|