devCache.js
3.14 KB
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
import devOptions from "./devOptions"
/**
* dev工具缓存管理
*/
export default {
/**
* 存储的键开始名称
*/
cacheKey: "devTools_v3_",
options: null,
/**
* 临时缓存对象
*/
tempCache: {
errorReport: [],
logReport: [],
console: [],
request: [],
uniBus: [],
},
/**
* 临时数据存放
*/
tempData: {},
/**
* 向缓存内写入数据
*/
set(key, value) {
try {
if (['errorReport', 'logReport', 'console', 'request', 'uniBus'].indexOf(key) != -1) {
let setting = this.getLongListSetting(key)
if (!setting.status) return;
if (!setting.cache.status) {
// !不使用缓存
this.tempCache[key] = value;
return;
}
}
key = `${this.cacheKey}${key}`
// #ifdef APP-NVUE
let pages = getCurrentPages()
if (pages[pages.length - 1].route == "devTools/page/index") {
// devtools 页面直接走设置缓存
return uni.setStorageSync(key, value);
}
// #endif
this.tempData[key] = value;
} catch (error) {
console.log("devCache.set error", error);
}
},
/**
* 同步读取缓存数据
*/
get(key) {
try {
if (['errorReport', 'logReport', 'console', 'request', 'uniBus'].indexOf(key) != -1) {
let setting = this.getLongListSetting(key)
if (!setting.status) return [];
if (!setting.cache.status) {
// !不使用缓存
return this.tempCache[key];
}
}
key = `${this.cacheKey}${key}`
// #ifdef APP-NVUE
let pages = getCurrentPages()
if (pages[pages.length - 1].route == "devTools/page/index") {
// devtools 页面直接走设置缓存
return uni.getStorageSync(key);
}
// #endif
if (this.tempData.hasOwnProperty(key)) {
return this.tempData[key];
} else {
let value = uni.getStorageSync(key);
this.tempData[key] = value;
return value;
}
} catch (error) {
console.log("devCache.get error", error);
return "";
}
},
getLongListSetting(key) {
let optionsKey = {
errorReport: 'error',
logReport: 'logs',
console: 'console',
request: 'network',
uniBus: 'uniBus',
}
if (this.options) return this.options[optionsKey[key]];
this.options = devOptions.getOptions()
return this.options[optionsKey[key]];
},
/**
* 同步本地缓存
*/
syncLocalCache() {
let that = this;
setTimeout(() => {
try {
let waitSetKeys = Object.keys(that.tempData);
for (let i = 0; i < waitSetKeys.length; i++) {
const key = waitSetKeys[i];
uni.setStorage({
key,
data: that.tempData[key],
success() {
// console.log("set " + key + " success,length=" + that.tempData[key].length);
delete that.tempData[key];
}
});
}
} catch (error) {
console.log("devCache error: ", error);
}
setTimeout(() => {
that.syncLocalCache();
}, 500);
}, Math.round(Math.random() * 3 * 1000) + 2000);
},
}