2017-04-12 00:48:27 +05:30
|
|
|
import logger from 'services/logger';
|
|
|
|
|
|
|
|
let _hasStorage = false;
|
|
|
|
|
|
|
|
try {
|
2019-11-27 14:33:32 +05:30
|
|
|
const test = 'test';
|
|
|
|
window.localStorage.setItem(test, test);
|
|
|
|
window.localStorage.removeItem(test);
|
2017-04-12 00:48:27 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
_hasStorage = true;
|
2017-04-12 00:48:27 +05:30
|
|
|
} catch (err) {
|
2019-11-27 14:33:32 +05:30
|
|
|
// bad luck, no storage available
|
|
|
|
logger.info('No storage available'); // log for statistic purposes
|
2017-04-12 00:48:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function hasStorage() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return _hasStorage;
|
2017-04-12 00:48:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function DummyStorage() {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
getItem(key) {
|
|
|
|
return this[key] || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
setItem(key, value) {
|
|
|
|
this[key] = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem(key) {
|
|
|
|
Reflect.deleteProperty(this, key);
|
|
|
|
},
|
|
|
|
};
|
2017-04-12 00:48:27 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export const localStorage = _hasStorage
|
|
|
|
? window.localStorage
|
|
|
|
: new DummyStorage();
|
2017-04-12 00:48:27 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export const sessionStorage = _hasStorage
|
|
|
|
? window.sessionStorage
|
|
|
|
: new DummyStorage();
|
2017-04-12 00:48:27 +05:30
|
|
|
|
|
|
|
export default localStorage;
|