diff --git a/dev/Common/ClientStorageDriver/LocalStorage.jsx b/dev/Common/ClientStorageDriver/LocalStorage.jsx index b99431971..c7f6401bc 100644 --- a/dev/Common/ClientStorageDriver/LocalStorage.jsx +++ b/dev/Common/ClientStorageDriver/LocalStorage.jsx @@ -2,24 +2,31 @@ import window from 'window'; import JSON from 'JSON'; import {isUnd} from 'Common/Utils'; +import {isStorageSupported} from 'Storage/RainLoop'; import {CLIENT_SIDE_STORAGE_INDEX_NAME} from 'Common/Consts'; class LocalStorageDriver { + constructor() + { + this.s = window.localStorage || null; + } + /** * @param {string} key * @param {*} data * @returns {boolean} */ set(key, data) { + if (!this.s) + { + return false; + } - let - result = false, - storageResult = null; - + let storageResult = null; try { - const storageValue = window.localStorage[CLIENT_SIDE_STORAGE_INDEX_NAME] || null; + const storageValue = this.s.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null; storageResult = null === storageValue ? null : JSON.parse(storageValue); } catch (e) {} // eslint-disable-line no-empty @@ -28,12 +35,12 @@ class LocalStorageDriver try { - window.localStorage[CLIENT_SIDE_STORAGE_INDEX_NAME] = JSON.stringify(storageResult); - result = true; + this.s.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, JSON.stringify(storageResult)); + return true; } catch (e) {} // eslint-disable-line no-empty - return result; + return false; } /** @@ -41,27 +48,29 @@ class LocalStorageDriver * @returns {*} */ get(key) { - - let result = null; + if (!this.s) + { + return null; + } try { const - storageValue = window.localStorage[CLIENT_SIDE_STORAGE_INDEX_NAME] || null, + storageValue = this.s.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null, storageResult = null === storageValue ? null : JSON.parse(storageValue); - result = (storageResult && !isUnd(storageResult[key])) ? storageResult[key] : null; + return (storageResult && !isUnd(storageResult[key])) ? storageResult[key] : null; } catch (e) {} // eslint-disable-line no-empty - return result; + return null; } /** * @returns {boolean} */ static supported() { - return !!window.localStorage; + return isStorageSupported('localStorage'); } } diff --git a/dev/Storage/RainLoop.jsx b/dev/Storage/RainLoop.jsx index de1c2840d..0ea510986 100644 --- a/dev/Storage/RainLoop.jsx +++ b/dev/Storage/RainLoop.jsx @@ -5,7 +5,34 @@ import JSON from 'JSON'; const STORAGE_KEY = '__rlA'; const TIME_KEY = '__rlT'; -const SESS_STORAGE = window.sessionStorage || null; +/** + * @param {string} storageName + * @returns {boolean} + */ +export function isStorageSupported(storageName) +{ + if (storageName in window && window[storageName] && window[storageName].setItem) + { + const + s = window[storageName], + key = 'testLocalStorage_' + window.Math.random(); + + try + { + s.setItem(key, key); + if (key === s.getItem(key)) + { + s.removeItem(key); + return true; + } + } + catch (e) {} // eslint-disable-line no-empty + } + + return false; +} + +const SESS_STORAGE = isStorageSupported('sessionStorage') ? (window.sessionStorage || null) : null; const WIN_STORAGE = window.top || window || null; const __get = (key) => { @@ -93,6 +120,4 @@ export function checkTimestamp() } // init section -window.setInterval(() => { - setTimestamp(); -}, 1000 * 60); // 1m +window.setInterval(setTimestamp, 1000 * 60); // 1m