autoMergeLevel2.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  2. 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; }
  3. 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; }
  4. 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; }
  5. /*
  6. autoMergeLevel2:
  7. - merges 2 level of substate
  8. - skips substate if already modified
  9. - this is essentially redux-perist v4 behavior
  10. */
  11. export default function autoMergeLevel2(inboundState, originalState, reducedState, _ref) {
  12. var debug = _ref.debug;
  13. var newState = _objectSpread({}, reducedState); // only rehydrate if inboundState exists and is an object
  14. if (inboundState && _typeof(inboundState) === 'object') {
  15. Object.keys(inboundState).forEach(function (key) {
  16. // ignore _persist data
  17. if (key === '_persist') return; // if reducer modifies substate, skip auto rehydration
  18. if (originalState[key] !== reducedState[key]) {
  19. if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist/stateReconciler: sub state for key `%s` modified, skipping.', key);
  20. return;
  21. }
  22. if (isPlainEnoughObject(reducedState[key])) {
  23. // if object is plain enough shallow merge the new values (hence "Level2")
  24. newState[key] = _objectSpread({}, newState[key], {}, inboundState[key]);
  25. return;
  26. } // otherwise hard set
  27. newState[key] = inboundState[key];
  28. });
  29. }
  30. if (process.env.NODE_ENV !== 'production' && debug && inboundState && _typeof(inboundState) === 'object') console.log("redux-persist/stateReconciler: rehydrated keys '".concat(Object.keys(inboundState).join(', '), "'"));
  31. return newState;
  32. }
  33. function isPlainEnoughObject(o) {
  34. return o !== null && !Array.isArray(o) && _typeof(o) === 'object';
  35. }