redux-promise-thunk
Version:
Thunk generator to dispatch Flux-Standard-Action in each phase of promise
68 lines (53 loc) • 2.01 kB
JavaScript
/*eslint-disable no-empty */
;
exports.__esModule = true;
exports.saveState = saveState;
exports.readState = readState;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var KeyPrefix = '@@History/';
var QuotaExceededError = 'QuotaExceededError';
var SecurityError = 'SecurityError';
function createKey(key) {
return KeyPrefix + key;
}
function saveState(key, state) {
try {
window.sessionStorage.setItem(createKey(key), JSON.stringify(state));
} catch (error) {
if (error.name === SecurityError) {
// Blocking cookies in Chrome/Firefox/Safari throws SecurityError on any
// attempt to access window.sessionStorage.
process.env.NODE_ENV !== 'production' ? _warning2['default'](false, '[history] Unable to save state; sessionStorage is not available due to security settings') : undefined;
return;
}
if (error.name === QuotaExceededError && window.sessionStorage.length === 0) {
// Safari "private mode" throws QuotaExceededError.
process.env.NODE_ENV !== 'production' ? _warning2['default'](false, '[history] Unable to save state; sessionStorage is not available in Safari private mode') : undefined;
return;
}
throw error;
}
}
function readState(key) {
var json = undefined;
try {
json = window.sessionStorage.getItem(createKey(key));
} catch (error) {
if (error.name === SecurityError) {
// Blocking cookies in Chrome/Firefox/Safari throws SecurityError on any
// attempt to access window.sessionStorage.
process.env.NODE_ENV !== 'production' ? _warning2['default'](false, '[history] Unable to read state; sessionStorage is not available due to security settings') : undefined;
return null;
}
}
if (json) {
try {
return JSON.parse(json);
} catch (error) {
// Ignore invalid JSON.
}
}
return null;
}