accounts-frontend/packages/app/services/localStorage.js

43 lines
786 B
JavaScript
Raw Normal View History

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