mirror of
https://github.com/the-djmaze/snappymail.git
synced 2024-11-15 20:24:51 +08:00
ea48f5060b
isUnd(*) to native undefined === * isFunc to native typeof * === 'function' isObject to native typeof * === 'object' microtime() to native Date().getTime(); noop to native ()=>{} noopFalse to native ()=>false noopTrue to native ()=>true boolToAjax to native *?'1':'0' Underscore.js to native
55 lines
1.1 KiB
JavaScript
55 lines
1.1 KiB
JavaScript
import window from 'window';
|
|
import Cookies from 'js-cookie';
|
|
import { CLIENT_SIDE_STORAGE_INDEX_NAME } from 'Common/Consts';
|
|
|
|
class CookieDriver {
|
|
/**
|
|
* @param {string} key
|
|
* @param {*} data
|
|
* @returns {boolean}
|
|
*/
|
|
set(key, data) {
|
|
let result = false,
|
|
storageResult = null;
|
|
|
|
try {
|
|
storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
|
|
} catch (e) {} // eslint-disable-line no-empty
|
|
|
|
(storageResult || (storageResult = {}))[key] = data;
|
|
|
|
try {
|
|
Cookies.set(CLIENT_SIDE_STORAGE_INDEX_NAME, storageResult, {
|
|
expires: 30
|
|
});
|
|
|
|
result = true;
|
|
} catch (e) {} // eslint-disable-line no-empty
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @param {string} key
|
|
* @returns {*}
|
|
*/
|
|
get(key) {
|
|
let result = null;
|
|
|
|
try {
|
|
const storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
|
|
result = storageResult && undefined !== storageResult[key] ? storageResult[key] : null;
|
|
} catch (e) {} // eslint-disable-line no-empty
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* @returns {boolean}
|
|
*/
|
|
static supported() {
|
|
return !!(window.navigator && window.navigator.cookieEnabled);
|
|
}
|
|
}
|
|
|
|
export { CookieDriver, CookieDriver as default };
|