-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
180 lines (158 loc) · 4.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Config = {
useBrowserStorage: false,
timeout: 0
};
var Helper = {
access: new Date(),
expired: function expired(storage) {
var expireMinute = Math.round((new Date() - Helper.access) / 60000);
if (expireMinute > Config.timeout && Config.timeout !== 0) {
console.warn("Session expired!", storage);
storage.clear();
}
Helper.access = new Date();
}
};
var Storage = function Storage() {
try {
if (!Config.useBrowserStorage) throw new Error();
var key = "jcfOnRWMIvigArtNb1z3hj6yQ2xlZGiD";
sessionStorage.setItem(key, key);
sessionStorage.removeItem(key);
return sessionStorage;
} catch (e) {
var _SessionStorage = {};
Object.defineProperty(_SessionStorage, "length", {
get: function get() {
return Object.keys(this).length;
},
enumerable: false,
__proto__: null
});
Object.setPrototypeOf(_SessionStorage, {
setItem: function setItem(key, value) {
_SessionStorage[key] = value;
},
getItem: function getItem(key) {
return _SessionStorage[key];
},
removeItem: function removeItem(key) {
delete _SessionStorage[key];
},
clear: function clear() {
_SessionStorage = {};
}
});
return _SessionStorage;
}
};
var SessionStorage =
/*#__PURE__*/
function () {
function SessionStorage() {
_classCallCheck(this, SessionStorage);
this.Storage = Storage();
this.Callback = [];
}
/**
* @param {boolean} browserStorage Use browser sessionStorage.
* @param {number} timeout Session timeout period, in minutes.
*/
_createClass(SessionStorage, [{
key: "config",
value: function config() {
var browserStorage = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
Config.useBrowserStorage = browserStorage;
Config.timeout = timeout;
this.Storage = Storage();
}
/**
* @return {Array<Object>} Return session items.
*/
}, {
key: "items",
value: function items() {
var _this = this;
Helper.expired(this.Storage);
var sessionData = {};
Object.keys(this.Storage).forEach(function (item) {
sessionData[item] = _this.Storage.getItem(item);
});
return sessionData;
}
}, {
key: "clear",
value: function clear() {
this.Storage.clear();
}
/**
* @param {string} key Session item key.
* @param {Object|string} value Session item value. If you are using browser storage, it can only take {:string}.
*/
}, {
key: "set",
value: function set(key, value) {
Helper.expired(this.Storage);
this.Storage.setItem(key, value);
var data = this.items();
this.Callback.forEach(function (func) {
func(data);
});
}
/**
* @param {string} key Session item key.
* @return {Object} Session item value. If you are using browser storage, it can return {:string}.
*/
}, {
key: "get",
value: function get(key) {
Helper.expired(this.Storage);
return this.Storage.getItem(key);
}
/**
* @param {string} key Session item key.
*/
}, {
key: "remove",
value: function remove(key) {
return this.Storage.removeItem(key);
}
/**
* @param {func} callback Triggered when session items set.
*/
}, {
key: "onSet",
value: function onSet(callback) {
var filter = this.Callback.filter(function (f) {
return f.name === callback.name;
});
if (filter.length === 0) {
callback(this.items());
this.Callback.push(callback);
}
}
/**
* @param {string} callbackName Callback function key.
*/
}, {
key: "unmount",
value: function unmount(callbackName) {
this.Callback = this.Callback.filter(function (f) {
return f.name !== callbackName;
});
}
}]);
return SessionStorage;
}();
var Session = new SessionStorage();
var _default = Session;
exports["default"] = _default;