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 1014B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @flow
  2. import _ from 'lodash';
  3. import { MiddlewareRegistry, toState } from '../redux';
  4. import PersistenceRegistry from './PersistenceRegistry';
  5. /**
  6. * The delay in milliseconds that passes between the last state change and the
  7. * persisting of that state in the storage.
  8. */
  9. const PERSIST_STATE_DELAY = 2000;
  10. /**
  11. * A throttled function to avoid repetitive state persisting.
  12. */
  13. const throttledPersistState
  14. = _.throttle(
  15. state => PersistenceRegistry.persistState(state),
  16. PERSIST_STATE_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 should
  20. * be persisted.
  21. *
  22. * @param {Store} store - The redux store.
  23. * @returns {Function}
  24. */
  25. MiddlewareRegistry.register(store => next => action => {
  26. const oldState = toState(store);
  27. const result = next(action);
  28. const newState = toState(store);
  29. oldState === newState || throttledPersistState(newState);
  30. return result;
  31. });