autoMergeLevel2.js 2.9 KB

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