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.

middleware.js 866B

123456789101112131415161718192021222324252627282930313233343536
  1. /* @flow */
  2. import _ from 'lodash';
  3. import { persistState } from './functions';
  4. import MiddlewareRegistry from './MiddlewareRegistry';
  5. import { toState } from '../redux';
  6. /**
  7. * The delay that passes between the last state change and the state to be
  8. * persisted in the storage.
  9. */
  10. const PERSIST_DELAY = 2000;
  11. /**
  12. * A throttled function to avoid repetitive state persisting.
  13. */
  14. const throttledFunc = _.throttle(state => {
  15. persistState(state);
  16. }, PERSIST_DELAY);
  17. /**
  18. * A master MiddleWare to selectively persist state. Please use the
  19. * {@link persisterconfig.json} to set which subtrees of the Redux state
  20. * should be persisted.
  21. *
  22. * @param {Store} store - The redux store.
  23. * @returns {Function}
  24. */
  25. MiddlewareRegistry.register(store => next => action => {
  26. const result = next(action);
  27. throttledFunc(toState(store));
  28. return result;
  29. });