#316: add localStorage wrapper with feature detection

This commit is contained in:
SleepWalker 2017-04-11 22:18:27 +03:00
parent 5d0951b218
commit dcf95410d7
4 changed files with 46 additions and 0 deletions

View File

@ -113,6 +113,10 @@
"no-undef-init": "error",
"no-undef": "error",
"no-use-before-define": ["warn", "nofunc"],
"no-restricted-globals": ["error",
"localStorage", "sessionStorage", // we have our own localStorage module
"event"
],
// CommonJS
"no-mixed-requires": "warn",

View File

@ -1,6 +1,7 @@
import { routeActions } from 'react-router-redux';
import logger from 'services/logger';
import localStorage from 'services/localStorage';
import loader from 'services/loader';
import history from 'services/history';
import { updateUser, acceptRules as userAcceptRules } from 'components/user/actions';

View File

@ -1,6 +1,7 @@
import { routeActions } from 'react-router-redux';
import logger from 'services/logger';
import localStorage from 'services/localStorage';
import RegisterState from './RegisterState';
import LoginState from './LoginState';

View File

@ -0,0 +1,40 @@
import logger from '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;
}
function DummyStorage() {
return {
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;