redux-promise-thunk
Version:
Thunk generator to dispatch Flux-Standard-Action in each phase of promise
62 lines (50 loc) • 1.77 kB
JavaScript
/*eslint-disable no-empty */
;
export { saveState };
export { readState };
import warning from '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' ? warning(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' ? warning(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' ? warning(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;
}