UNPKG

@lyra/vision

Version:

React-based data management tool for Lyra projects

59 lines (48 loc) 1.29 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getState = getState; exports.storeState = storeState; var _isPlainObject = require('./isPlainObject'); var _isPlainObject2 = _interopRequireDefault(_isPlainObject); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const storageKey = 'lyraVision'; const defaultState = {}; const hasLocalStorage = supportsLocalStorage(); let state = null; function getState(key, defaultVal) { ensureState(); return typeof state[key] === 'undefined' ? defaultVal : state[key]; } function storeState(key, value) { ensureState(); state[key] = value; localStorage.setItem(storageKey, JSON.stringify(state)); } function ensureState() { if (state === null) { state = loadState(); } } function loadState() { if (!hasLocalStorage) { return defaultState; } try { const stored = JSON.parse(localStorage.getItem(storageKey)); return (0, _isPlainObject2.default)(stored) ? stored : defaultState; } catch (err) { return defaultState; } } function supportsLocalStorage() { const mod = 'lsCheck'; try { localStorage.setItem(mod, mod); localStorage.removeItem(mod); return true; } catch (err) { return false; } }