mirror of
https://github.com/the-djmaze/snappymail.git
synced 2025-10-01 17:34:37 +08:00
Improve web storage detection.
Possible fix for Safari private mode (#1092)
This commit is contained in:
parent
a2308e251b
commit
739ed215ea
2 changed files with 52 additions and 18 deletions
|
@ -2,24 +2,31 @@
|
||||||
import window from 'window';
|
import window from 'window';
|
||||||
import JSON from 'JSON';
|
import JSON from 'JSON';
|
||||||
import {isUnd} from 'Common/Utils';
|
import {isUnd} from 'Common/Utils';
|
||||||
|
import {isStorageSupported} from 'Storage/RainLoop';
|
||||||
import {CLIENT_SIDE_STORAGE_INDEX_NAME} from 'Common/Consts';
|
import {CLIENT_SIDE_STORAGE_INDEX_NAME} from 'Common/Consts';
|
||||||
|
|
||||||
class LocalStorageDriver
|
class LocalStorageDriver
|
||||||
{
|
{
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
this.s = window.localStorage || null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} key
|
* @param {string} key
|
||||||
* @param {*} data
|
* @param {*} data
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
set(key, data) {
|
set(key, data) {
|
||||||
|
if (!this.s)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
let
|
let storageResult = null;
|
||||||
result = false,
|
|
||||||
storageResult = null;
|
|
||||||
|
|
||||||
try
|
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);
|
storageResult = null === storageValue ? null : JSON.parse(storageValue);
|
||||||
}
|
}
|
||||||
catch (e) {} // eslint-disable-line no-empty
|
catch (e) {} // eslint-disable-line no-empty
|
||||||
|
@ -28,12 +35,12 @@ class LocalStorageDriver
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
window.localStorage[CLIENT_SIDE_STORAGE_INDEX_NAME] = JSON.stringify(storageResult);
|
this.s.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, JSON.stringify(storageResult));
|
||||||
result = true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (e) {} // eslint-disable-line no-empty
|
catch (e) {} // eslint-disable-line no-empty
|
||||||
|
|
||||||
return result;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -41,27 +48,29 @@ class LocalStorageDriver
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
get(key) {
|
get(key) {
|
||||||
|
if (!this.s)
|
||||||
let result = null;
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const
|
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);
|
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
|
catch (e) {} // eslint-disable-line no-empty
|
||||||
|
|
||||||
return result;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
static supported() {
|
static supported() {
|
||||||
return !!window.localStorage;
|
return isStorageSupported('localStorage');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,34 @@ import JSON from 'JSON';
|
||||||
const STORAGE_KEY = '__rlA';
|
const STORAGE_KEY = '__rlA';
|
||||||
const TIME_KEY = '__rlT';
|
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 WIN_STORAGE = window.top || window || null;
|
||||||
|
|
||||||
const __get = (key) => {
|
const __get = (key) => {
|
||||||
|
@ -93,6 +120,4 @@ export function checkTimestamp()
|
||||||
}
|
}
|
||||||
|
|
||||||
// init section
|
// init section
|
||||||
window.setInterval(() => {
|
window.setInterval(setTimestamp, 1000 * 60); // 1m
|
||||||
setTimestamp();
|
|
||||||
}, 1000 * 60); // 1m
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue