index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /** A function that accepts a potential "extra argument" value to be injected later,
  7. * and returns an instance of the thunk middleware that uses that value
  8. */
  9. function createThunkMiddleware(extraArgument) {
  10. // Standard Redux middleware definition pattern:
  11. // See: https://redux.js.org/tutorials/fundamentals/part-4-store#writing-custom-middleware
  12. var middleware = function middleware(_ref) {
  13. var dispatch = _ref.dispatch,
  14. getState = _ref.getState;
  15. return function (next) {
  16. return function (action) {
  17. // The thunk middleware looks for any functions that were passed to `store.dispatch`.
  18. // If this "action" is really a function, call it and return the result.
  19. if (typeof action === 'function') {
  20. // Inject the store's `dispatch` and `getState` methods, as well as any "extra arg"
  21. return action(dispatch, getState, extraArgument);
  22. } // Otherwise, pass the action down the middleware chain as usual
  23. return next(action);
  24. };
  25. };
  26. };
  27. return middleware;
  28. }
  29. var thunk = createThunkMiddleware(); // Attach the factory function so users can create a customized version
  30. // with whatever "extra arg" they want to inject into their thunks
  31. thunk.withExtraArgument = createThunkMiddleware;
  32. var _default = thunk;
  33. exports.default = _default;