uniBus.js
4.07 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import devCache from "../libs/devCache";
import devOptions from "../libs/devOptions";
import jsonCompress from "../libs/jsonCompress";
export default {
logList: [],
busCount: [],
options: null,
/**
* 挂载打印拦截器
*/
install() {
try {
let that = this;
this.options = devOptions.getOptions()
if (!this.options.uniBus.status) return;
this.logList = devCache.get("uniBus");
if (!this.logList) this.logList = [];
this.busCount = devCache.get("busCount");
if (!this.busCount) this.busCount = [];
this.syncReqData(); //同步缓存
let now = () => new Date().getTime();
const _on = uni.$on;
uni.$on = function () {
try {
let n = arguments[0];
if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
that.logList.unshift({
t: now(),
e: jsonCompress.compressObject(`on>${n}`, that.options.uniBus.cache.rowMax)
})
addCount(n, "on")
}
} catch (error) {
console.error("uni.$on出错", error);
}
_on(...arguments)
}
const _once = uni.$once;
uni.$once = function () {
try {
let n = arguments[0];
if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
that.logList.unshift({
t: now(),
e: jsonCompress.compressObject(`once>${n}`, that.options.uniBus.cache.rowMax)
})
addCount(n, "once")
}
} catch (error) {
console.error("uni.$once出错", error);
}
_once(...arguments)
}
const _emit = uni.$emit;
uni.$emit = function () {
try {
let n = arguments[0];
let p = arguments[1];
if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
that.logList.unshift({
t: now(),
e: jsonCompress.compressObject(`emit>${n}` + (p ? ('>' + JSON.stringify(p)) : ''), that.options.uniBus.cache.rowMax)
})
addCount(n, "emit")
}
} catch (error) {
console.error("uni.$emit出错", error);
}
_emit(...arguments)
}
const _off = uni.$off;
uni.$off = function () {
try {
let n = arguments[0];
if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
that.logList.unshift({
t: now(),
e: jsonCompress.compressObject(`off>${n}` + arguments[0], that.options.uniBus.cache.rowMax)
})
addCount(n, "off")
}
} catch (error) {
console.error("uni.$off出错", error);
}
_off(...arguments)
}
/**
* 统计总次数
*/
function addCount(name, type = "on") {
let i = that.busCount.findIndex(x => x.e == name)
if (i == -1) {
let item = {
e: name,
on: 0,
off: 0,
emit: 0,
once: 0,
};
item[type] = item[type] + 1;
that.busCount.push(item)
} else {
that.busCount[i][type] = that.busCount[i][type] + 1;
}
}
// ! 清空全部记录
uni.$on("devTools_delUniBusAll", () => {
that.logList = []
that.busCount = []
})
} catch (error) {
console.log("devTools uniBus.install error", error);
}
},
/**
* 同步请求信息到缓存数据中
*/
syncReqData() {
let that = this;
setTimeout(() => {
try {
that.logList = jsonCompress.compressArray(that.logList, "end", that.options.uniBus.cache.rowMax)
devCache.set("uniBus", that.logList)
that.busCount = jsonCompress.compressArray(that.busCount, "end", that.options.uniBus.cache.countMaxSize)
devCache.set("busCount", that.busCount)
} catch (error) {
console.log("devTools uniBus.syncReqData error", error);
}
that.syncReqData()
}, 5000);
},
}