-
Notifications
You must be signed in to change notification settings - Fork 10
/
Persist.js
executable file
·248 lines (218 loc) · 5.76 KB
/
Persist.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* eslint-disable no-use-before-define */
import {
reset,
setStateByKey,
getStateByKey,
getStorage,
} from "./storageManager";
import {isNeeded, getUrl, getStorageKey, getNavigationType} from "./utils";
import {console, window} from "./browser";
import {TYPE_BACK_FORWARD, TYPE_NAVIGATE, CONST_PERSIST_STATE, CONST_DEPTHS, CONST_LAST_URL} from "./consts";
let currentUrl = "";
function setRec(obj, path, value) {
let _obj = obj;
if (!_obj) {
_obj = isNaN(path[0]) ? {} : [];
}
const head = path.shift();
if (path.length === 0) {
if (_obj instanceof Array && isNaN(head)) {
console.warn("Don't use key string on array");
}
_obj[head] = value;
return _obj;
}
_obj[head] = setRec(_obj[head], path, value);
return _obj;
}
function setPersistState(key, value) {
try {
setStateByKey(CONST_PERSIST_STATE, key, value);
} catch (e) {
if (clearFirst()) {
if (key === CONST_LAST_URL) {
setPersistState(key, value);
} else if (key === CONST_DEPTHS) {
setPersistState(key, value && value.slice(1));
}
} else {
// There is no more size to fit in.
throw e;
}
}
}
function getPersistState(key) {
return getStateByKey(CONST_PERSIST_STATE, key);
}
function updateDepth(type) {
const url = getUrl();
if (currentUrl === url) {
return;
}
// url is not the same for the first time, pushState, or replaceState.
currentUrl = url;
const depths = getPersistState(CONST_DEPTHS) || [];
if (type === TYPE_BACK_FORWARD) {
// Change current url only
const currentIndex = depths.indexOf(currentUrl);
~currentIndex && setPersistState(CONST_LAST_URL, currentUrl);
} else {
const prevLastUrl = getPersistState(CONST_LAST_URL);
reset(getStorageKey(currentUrl));
if (type === TYPE_NAVIGATE && url !== prevLastUrl) {
// Remove all url lists with higher index than current index
const prevLastIndex = depths.indexOf(prevLastUrl);
const removedList = depths.splice(prevLastIndex + 1, depths.length);
removedList.forEach(removedUrl => {
reset(getStorageKey(removedUrl));
});
// If the type is NAVIGATE and there is information about current url, delete it.
const currentIndex = depths.indexOf(currentUrl);
~currentIndex && depths.splice(currentIndex, 1);
}
// Add depth for new address.
if (depths.indexOf(url) < 0) {
depths.push(url);
}
setPersistState(CONST_DEPTHS, depths);
setPersistState(CONST_LAST_URL, url);
}
}
function clearFirst() {
const depths = getPersistState(CONST_DEPTHS) || [];
const removed = depths.splice(0, 1);
if (!removed.length) {
// There is an error because there is no depth to add data.
return false;
}
const removedUrl = removed[0];
reset(getStorageKey(removedUrl));
if (currentUrl === removedUrl) {
currentUrl = "";
setPersistState(CONST_LAST_URL, "");
if (!depths.length) {
// I tried to add myself, but it didn't add up, so I got an error.
return false;
}
}
setPersistState(CONST_DEPTHS, depths);
// Clear the previous record and try to add data again.
return true;
}
function clear() {
const depths = getPersistState(CONST_DEPTHS) || [];
depths.forEach(url => {
reset(getStorageKey(url));
});
reset(CONST_PERSIST_STATE);
currentUrl = "";
}
if ("onpopstate" in window) {
window.addEventListener("popstate", () => {
// popstate event occurs when backward or forward
updateDepth(TYPE_BACK_FORWARD);
});
}
/**
* Get or store the current state of the web page using JSON.
* @ko 웹 페이지의 현재 상태를 JSON 형식으로 저장하거나 읽는다.
* @alias eg.Persist
*
* @support {"ie": "9+", "ch" : "latest", "ff" : "latest", "sf" : "latest" , "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
*/
class Persist {
static VERSION = "#__VERSION__#";
static StorageManager = {
reset,
setStateByKey,
getStateByKey,
getStorage,
};
/**
* @static
* Clear all information in Persist
*/
static clear() {
clear();
}
/**
* @static
* Return whether you need "Persist" module by checking the bfCache support of the current browser
* @return {Boolean}
*/
static isNeeded() {
return isNeeded;
}
/**
* Constructor
* @param {String} key The key of the state information to be stored <ko>저장할 상태 정보의 키</ko>
**/
constructor(key) {
this.key = key || "";
}
/**
* Read value
* @param {String?} path target path
* @return {String|Number|Boolean|Object|Array}
*/
get(path) {
const urlKey = getStorageKey(getUrl());
// update url for pushState, replaceState
updateDepth(TYPE_NAVIGATE);
// find path
const globalState = getStateByKey(urlKey, this.key);
if (!path || path.length === 0) {
return globalState;
}
const pathToken = path.split(".");
let currentItem = globalState;
let isTargetExist = true;
for (let i = 0; i < pathToken.length; i++) {
if (!currentItem) {
isTargetExist = false;
break;
}
currentItem = currentItem[pathToken[i]];
}
if (!isTargetExist || !currentItem) {
return null;
}
return currentItem;
}
/**
* Save value
* @param {String} path target path
* @param {String|Number|Boolean|Object|Array} value value to save
* @return {Persist}
*/
set(path, value) {
const urlKey = getStorageKey(getUrl());
// update url for pushState, replaceState
updateDepth(TYPE_NAVIGATE);
// find path
const key = this.key;
const globalState = getStateByKey(urlKey, key);
try {
if (path.length === 0) {
setStateByKey(urlKey, key, value);
} else {
setStateByKey(
urlKey,
key,
setRec(globalState, path.split("."), value)
);
}
} catch (e) {
if (clearFirst(e)) {
this.set(path, value);
} else {
// There is no more size to fit in.
throw e;
}
}
return this;
}
}
// If navigation's type is not TYPE_BACK_FORWARD, delete information about current url.
updateDepth(getNavigationType());
export default Persist;