redux-persist
Version:
persist and rehydrate redux stores
44 lines (35 loc) • 1.34 kB
JavaScript
;
exports.__esModule = true;
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 = getStorage;
function noop() {}
var noopStorage = {
getItem: noop,
setItem: noop,
removeItem: noop
};
function hasStorage(storageType) {
if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object' || !(storageType in window)) {
return false;
}
try {
var storage = window[storageType];
var testKey = 'redux-persist ' + storageType + ' test';
storage.setItem(testKey, 'test');
storage.getItem(testKey);
storage.removeItem(testKey);
} catch (e) {
if (process.env.NODE_ENV !== 'production') console.warn('redux-persist ' + storageType + ' test failed, persistence will be disabled.');
return false;
}
return true;
}
function getStorage(type) {
var storageType = type + 'Storage';
if (hasStorage(storageType)) return window[storageType];else {
if (process.env.NODE_ENV !== 'production') {
console.error('redux-persist failed to create sync storage. falling back to memory storage.');
}
return noopStorage;
}
}