persistStore.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  2. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  3. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  4. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
  5. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  6. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  7. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  8. import { createStore } from 'redux';
  9. import { FLUSH, PAUSE, PERSIST, PURGE, REGISTER, REHYDRATE } from './constants';
  10. var initialState = {
  11. registry: [],
  12. bootstrapped: false
  13. };
  14. var persistorReducer = function persistorReducer() {
  15. var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  16. var action = arguments.length > 1 ? arguments[1] : undefined;
  17. switch (action.type) {
  18. case REGISTER:
  19. return _objectSpread({}, state, {
  20. registry: [].concat(_toConsumableArray(state.registry), [action.key])
  21. });
  22. case REHYDRATE:
  23. var firstIndex = state.registry.indexOf(action.key);
  24. var registry = _toConsumableArray(state.registry);
  25. registry.splice(firstIndex, 1);
  26. return _objectSpread({}, state, {
  27. registry: registry,
  28. bootstrapped: registry.length === 0
  29. });
  30. default:
  31. return state;
  32. }
  33. };
  34. export default function persistStore(store, options, cb) {
  35. // help catch incorrect usage of passing PersistConfig in as PersistorOptions
  36. if (process.env.NODE_ENV !== 'production') {
  37. var optionsToTest = options || {};
  38. var bannedKeys = ['blacklist', 'whitelist', 'transforms', 'storage', 'keyPrefix', 'migrate'];
  39. bannedKeys.forEach(function (k) {
  40. if (!!optionsToTest[k]) console.error("redux-persist: invalid option passed to persistStore: \"".concat(k, "\". You may be incorrectly passing persistConfig into persistStore, whereas it should be passed into persistReducer."));
  41. });
  42. }
  43. var boostrappedCb = cb || false;
  44. var _pStore = createStore(persistorReducer, initialState, options && options.enhancer ? options.enhancer : undefined);
  45. var register = function register(key) {
  46. _pStore.dispatch({
  47. type: REGISTER,
  48. key: key
  49. });
  50. };
  51. var rehydrate = function rehydrate(key, payload, err) {
  52. var rehydrateAction = {
  53. type: REHYDRATE,
  54. payload: payload,
  55. err: err,
  56. key: key // dispatch to `store` to rehydrate and `persistor` to track result
  57. };
  58. store.dispatch(rehydrateAction);
  59. _pStore.dispatch(rehydrateAction);
  60. if (boostrappedCb && persistor.getState().bootstrapped) {
  61. boostrappedCb();
  62. boostrappedCb = false;
  63. }
  64. };
  65. var persistor = _objectSpread({}, _pStore, {
  66. purge: function purge() {
  67. var results = [];
  68. store.dispatch({
  69. type: PURGE,
  70. result: function result(purgeResult) {
  71. results.push(purgeResult);
  72. }
  73. });
  74. return Promise.all(results);
  75. },
  76. flush: function flush() {
  77. var results = [];
  78. store.dispatch({
  79. type: FLUSH,
  80. result: function result(flushResult) {
  81. results.push(flushResult);
  82. }
  83. });
  84. return Promise.all(results);
  85. },
  86. pause: function pause() {
  87. store.dispatch({
  88. type: PAUSE
  89. });
  90. },
  91. persist: function persist() {
  92. store.dispatch({
  93. type: PERSIST,
  94. register: register,
  95. rehydrate: rehydrate
  96. });
  97. }
  98. });
  99. if (!(options && options.manualPersist)) {
  100. persistor.persist();
  101. }
  102. return persistor;
  103. }