snappymail/dev/Storage/RainLoop.jsx

106 lines
1.7 KiB
React
Raw Normal View History

2016-05-06 23:14:40 +08:00
import window from 'window';
2016-05-22 08:09:20 +08:00
const
STORAGE_KEY = '__rlA',
TIME_KEY = '__rlT'
;
2016-05-06 23:14:40 +08:00
class RainLoopStorage
{
s = null;
t = null;
constructor()
{
this.s = window.sessionStorage || null;
this.t = window.top || window;
2016-05-22 08:09:20 +08:00
this.init();
2016-05-06 23:14:40 +08:00
}
2016-05-22 08:09:20 +08:00
__get(key) {
2016-05-06 23:14:40 +08:00
let result = null;
if (this.s)
{
2016-05-22 08:09:20 +08:00
result = this.s.getItem(key) || null;
2016-05-06 23:14:40 +08:00
}
else if (this.t && JSON)
{
const data = this.t.name && '{' === this.t.name.toString().substr(0, 1) ? JSON.parse(this.t.name.toString()) : null;
2016-05-22 08:09:20 +08:00
result = data ? (data[key] || null) : null;
2016-05-06 23:14:40 +08:00
}
return result;
}
2016-05-22 08:09:20 +08:00
__set(key, value) {
2016-05-06 23:14:40 +08:00
if (this.s)
{
2016-05-22 08:09:20 +08:00
this.s.setItem(key, value);
2016-05-06 23:14:40 +08:00
}
else if (this.t && JSON)
{
2016-05-22 08:09:20 +08:00
let data = this.t.name && '{' === this.t.name.toString().substr(0, 1) ? JSON.parse(this.t.name.toString()) : null;
data = data || {};
data[key] = value;
2016-05-06 23:14:40 +08:00
this.t.name = JSON.stringify(data);
}
}
2016-05-22 08:09:20 +08:00
timestamp() {
return window.Math.round((new Date()).getTime() / 1000);
}
2016-05-22 20:27:50 +08:00
checkTimestamp() {
2016-05-22 08:09:20 +08:00
2016-05-22 20:27:50 +08:00
if (this.timestamp() > this.getTimestamp() + 1000 * 60 * 60) // 60m
2016-05-06 23:14:40 +08:00
{
2016-05-22 08:09:20 +08:00
this.clearHash();
2016-05-22 20:27:50 +08:00
return true;
2016-05-06 23:14:40 +08:00
}
2016-05-22 08:09:20 +08:00
2016-05-22 20:27:50 +08:00
return false;
}
init() {
2016-05-22 08:09:20 +08:00
window.setInterval(() => {
this.setTimestamp();
2016-05-22 20:27:50 +08:00
}, 1000 * 60); // 1m
2016-05-22 08:09:20 +08:00
}
getHash() {
return this.__get(STORAGE_KEY);
}
setHash() {
const
key = 'AuthAccountHash',
appData = window.__rlah_data()
;
this.__set(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
this.setTimestamp();
}
setTimestamp() {
this.__set(TIME_KEY, this.timestamp());
}
getTimestamp() {
let time = this.__get(TIME_KEY, 0);
return time ? (window.parseInt(time, 10) | 0) : 0;
}
clearHash() {
this.__set(STORAGE_KEY, '');
this.setTimestamp();
2016-05-06 23:14:40 +08:00
}
}
module.exports = new RainLoopStorage();