Improve web storage detection.

Possible fix for Safari private mode (#1092)
This commit is contained in:
RainLoop Team 2016-07-07 21:53:02 +03:00
parent a2308e251b
commit 739ed215ea
2 changed files with 52 additions and 18 deletions

View file

@ -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');
}
}

View file

@ -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