snappymail/dev/Storage/RainLoop.jsx

99 lines
1.8 KiB
React
Raw Normal View History

2016-05-06 23:14:40 +08:00
import window from 'window';
2016-06-17 07:23:49 +08:00
import JSON from 'JSON';
2016-05-06 23:14:40 +08:00
2016-06-17 07:23:49 +08:00
const STORAGE_KEY = '__rlA';
const TIME_KEY = '__rlT';
2016-05-06 23:14:40 +08:00
2016-06-17 07:23:49 +08:00
const SESS_STORAGE = window.sessionStorage || null;
const WIN_STORAGE = window.top || window || null;
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const __get = (key) => {
2016-05-06 23:14:40 +08:00
2016-06-17 07:23:49 +08:00
let result = null;
if (SESS_STORAGE)
{
result = SESS_STORAGE.getItem(key) || null;
2016-05-06 23:14:40 +08:00
}
2016-06-17 07:23:49 +08:00
else if (WIN_STORAGE && JSON)
{
const data = WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1) ? JSON.parse(WIN_STORAGE.name.toString()) : null;
result = data ? (data[key] || null) : null;
2016-05-22 08:09:20 +08:00
}
2016-06-17 07:23:49 +08:00
return result;
};
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const __set = (key, value) => {
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
if (SESS_STORAGE)
{
SESS_STORAGE.setItem(key, value);
2016-05-22 20:27:50 +08:00
}
2016-06-17 07:23:49 +08:00
else if (WIN_STORAGE && JSON)
{
let data = WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1) ? JSON.parse(WIN_STORAGE.name.toString()) : null;
data = data || {};
data[key] = value;
2016-05-22 20:27:50 +08:00
2016-06-17 07:23:49 +08:00
WIN_STORAGE.name = JSON.stringify(data);
2016-05-22 08:09:20 +08:00
}
2016-06-17 07:23:49 +08:00
};
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const timestamp = () => window.Math.round((new window.Date()).getTime() / 1000);
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const setTimestamp = () => __set(TIME_KEY, timestamp());
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
const getTimestamp = () => {
2016-06-30 08:02:45 +08:00
const time = __get(TIME_KEY, 0);
2016-06-17 07:23:49 +08:00
return time ? (window.parseInt(time, 10) || 0) : 0;
};
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
/**
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
export function getHash()
{
return __get(STORAGE_KEY);
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-06-17 07:23:49 +08:00
export function setHash()
{
const
key = 'AuthAccountHash',
2016-06-30 08:02:45 +08:00
appData = window.__rlah_data();
2016-05-22 08:09:20 +08:00
2016-06-17 07:23:49 +08:00
__set(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
setTimestamp();
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-06-17 07:23:49 +08:00
export function clearHash()
{
__set(STORAGE_KEY, '');
setTimestamp();
}
2016-05-22 08:09:20 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {boolean}
*/
2016-06-17 07:23:49 +08:00
export function checkTimestamp()
{
if (timestamp() > getTimestamp() + 1000 * 60 * 60) // 60m
{
clearHash();
return true;
2016-05-06 23:14:40 +08:00
}
2016-06-17 07:23:49 +08:00
return false;
2016-05-06 23:14:40 +08:00
}
2016-06-17 07:23:49 +08:00
// init section
window.setInterval(() => {
setTimestamp();
}, 1000 * 60); // 1m