mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-29 03:59:21 +03: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 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');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue