redux-persist
Version:
persist and rehydrate redux stores
57 lines (47 loc) • 3.43 kB
JavaScript
;
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 = autoMergeLevel1;
function autoMergeLevel1(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;
}
// otherwise hard set the new value
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;
}
/*
autoMergeLevel1:
- merges 1 level of substate
- skips substate if already modified
*/