redux-thunk.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ReduxThunk = factory());
  5. })(this, (function () { 'use strict';
  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. return thunk;
  33. }));