persistStore.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = persistStore;
  4. var _redux = require("redux");
  5. var _constants = require("./constants");
  6. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
  7. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
  8. function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
  9. 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; } }
  10. 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; }
  11. 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; }
  12. 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; }
  13. var initialState = {
  14. registry: [],
  15. bootstrapped: false
  16. };
  17. var persistorReducer = function persistorReducer() {
  18. var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  19. var action = arguments.length > 1 ? arguments[1] : undefined;
  20. switch (action.type) {
  21. case _constants.REGISTER:
  22. return _objectSpread({}, state, {
  23. registry: [].concat(_toConsumableArray(state.registry), [action.key])
  24. });
  25. case _constants.REHYDRATE:
  26. var firstIndex = state.registry.indexOf(action.key);
  27. var registry = _toConsumableArray(state.registry);
  28. registry.splice(firstIndex, 1);
  29. return _objectSpread({}, state, {
  30. registry: registry,
  31. bootstrapped: registry.length === 0
  32. });
  33. default:
  34. return state;
  35. }
  36. };
  37. function persistStore(store, options, cb) {
  38. // help catch incorrect usage of passing PersistConfig in as PersistorOptions
  39. if (process.env.NODE_ENV !== 'production') {
  40. var optionsToTest = options || {};
  41. var bannedKeys = ['blacklist', 'whitelist', 'transforms', 'storage', 'keyPrefix', 'migrate'];
  42. bannedKeys.forEach(function (k) {
  43. 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."));
  44. });
  45. }
  46. var boostrappedCb = cb || false;
  47. var _pStore = (0, _redux.createStore)(persistorReducer, initialState, options && options.enhancer ? options.enhancer : undefined);
  48. var register = function register(key) {
  49. _pStore.dispatch({
  50. type: _constants.REGISTER,
  51. key: key
  52. });
  53. };
  54. var rehydrate = function rehydrate(key, payload, err) {
  55. var rehydrateAction = {
  56. type: _constants.REHYDRATE,
  57. payload: payload,
  58. err: err,
  59. key: key // dispatch to `store` to rehydrate and `persistor` to track result
  60. };
  61. store.dispatch(rehydrateAction);
  62. _pStore.dispatch(rehydrateAction);
  63. if (boostrappedCb && persistor.getState().bootstrapped) {
  64. boostrappedCb();
  65. boostrappedCb = false;
  66. }
  67. };
  68. var persistor = _objectSpread({}, _pStore, {
  69. purge: function purge() {
  70. var results = [];
  71. store.dispatch({
  72. type: _constants.PURGE,
  73. result: function result(purgeResult) {
  74. results.push(purgeResult);
  75. }
  76. });
  77. return Promise.all(results);
  78. },
  79. flush: function flush() {
  80. var results = [];
  81. store.dispatch({
  82. type: _constants.FLUSH,
  83. result: function result(flushResult) {
  84. results.push(flushResult);
  85. }
  86. });
  87. return Promise.all(results);
  88. },
  89. pause: function pause() {
  90. store.dispatch({
  91. type: _constants.PAUSE
  92. });
  93. },
  94. persist: function persist() {
  95. store.dispatch({
  96. type: _constants.PERSIST,
  97. register: register,
  98. rehydrate: rehydrate
  99. });
  100. }
  101. });
  102. if (!(options && options.manualPersist)) {
  103. persistor.persist();
  104. }
  105. return persistor;
  106. }