闭社主体 forked from https://github.com/tootsuite/mastodon
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.

25 lines
855 B

  1. import { showLoading, hideLoading } from 'react-redux-loading-bar';
  2. const defaultTypeSuffixes = ['PENDING', 'FULFILLED', 'REJECTED'];
  3. export default function loadingBarMiddleware(config = {}) {
  4. const promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypeSuffixes;
  5. return ({ dispatch }) => next => (action) => {
  6. if (action.type && !action.skipLoading) {
  7. const [PENDING, FULFILLED, REJECTED] = promiseTypeSuffixes;
  8. const isPending = new RegExp(`${PENDING}$`, 'g');
  9. const isFulfilled = new RegExp(`${FULFILLED}$`, 'g');
  10. const isRejected = new RegExp(`${REJECTED}$`, 'g');
  11. if (action.type.match(isPending)) {
  12. dispatch(showLoading());
  13. } else if (action.type.match(isFulfilled) || action.type.match(isRejected)) {
  14. dispatch(hideLoading());
  15. }
  16. }
  17. return next(action);
  18. };
  19. };