UNPKG

redux-persist

Version:

persist and rehydrate redux stores

67 lines (56 loc) 3.85 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.default = autoMergeLevel2; function autoMergeLevel2(inboundState, originalState, reducedState, _ref) { var debug = _ref.debug; // various dev only sanity checks if (process.env.NODE_ENV !== 'production') { if (inboundState) { Object.keys(inboundState).forEach(function (key) { // check if initialState is missing a key if (!originalState.hasOwnProperty(key)) console.log('\n redux-persist/stateReconciler: state missing key\n "' + key + '". state-manager will still store the rehydrated value. If you\n removed ' + key + ' from your reducer tree, you should write a migration to\n remove ' + key + ' from stored state. If you code-split ' + key + ' reducer, then\n this is the expected behavior.\n '); // check recently added reducer properties that may require a migration if (originalState[key] && _typeof(originalState[key]) === 'object' && inboundState[key] && _typeof(inboundState[key]) === 'object') { var stateKeys = originalState[key] ? Object.keys(originalState[key]) : []; var inboundStateKeys = Object.keys(inboundState[key]); stateKeys.forEach(function (checkKey) { if (inboundState[checkKey] === 'undefined') console.log('\n redux-persist/stateReconciler: initialState for "' + key + '"\n has property "' + checkKey + '" which is missing in rehydratedState. After\n rehydration, "' + checkKey + '" will be null. If you recently added\n ' + checkKey + ' to your ' + key + ' reducer, consider adding ' + checkKey + ' to a\n state migration.\n '); }); } }); } } var newState = _extends({}, reducedState); // only rehydrate if inboundState exists and is an object if (inboundState && (typeof inboundState === 'undefined' ? 'undefined' : _typeof(inboundState)) === 'object') { Object.keys(inboundState).forEach(function (key) { // ignore _persist data if (key === '_persist') return; // if reducer modifies substate, skip auto rehydration if (originalState[key] !== reducedState[key]) { if (process.env.NODE_ENV !== 'production' && debug) console.log('redux-persist/stateReconciler: sub state for key `%s` modified, skipping.', key); return; } if (isPlainEnoughObject(reducedState[key])) { // if object is plain enough shallow merge the new values (hence "Level2") newState[key] = _extends({}, newState[key], inboundState[key]); return; } // otherwise hard set newState[key] = inboundState[key]; }); } if (process.env.NODE_ENV !== 'production' && debug && inboundState && (typeof inboundState === 'undefined' ? 'undefined' : _typeof(inboundState)) === 'object') console.log('redux-persist/stateReconciler: rehydrated keys \'' + Object.keys(inboundState).join(', ') + '\''); return newState; } /* autoMergeLevel2: - merges 2 level of substate - skips substate if already modified - this is essentially redux-perist v4 behavior */ function isPlainEnoughObject(o) { return o !== null && !Array.isArray(o) && (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === 'object'; }