cbvirtua 1.0.55 → 1.0.57

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/mitt-main.zip ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cbvirtua",
3
- "version": "1.0.55",
3
+ "version": "1.0.57",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -97,4 +97,244 @@ input.ui-input, .ui-input > input, [is="ui-input"] {
97
97
  font-size: var(--ui-font, 14px);
98
98
  outline: none;
99
99
  color: var(--ui-dark, #4c5161);
100
- transition: border-color var(--ui-animate-time, .2s), background-color var(--ui-animate-time, .2s);
100
+ transition: border-color var(--ui-animate-time, .2s), background-color var(--ui-animate-time, .2s);
101
+
102
+
103
+ import mitt from 'mitt'
104
+
105
+ // 当前文件名 需要同步修改
106
+ const CURRENT_FILE_NAME = 'useMitt'
107
+
108
+ // 基础配置
109
+ const defineOptions = {
110
+ // 是否禁用
111
+ logDisabled: false,
112
+ // 是否展开
113
+ logExpanded: false,
114
+ // console.group样式
115
+ logStyle: 'color: #fff; background: #51a2e4; font-size: 12px; padding: 4px; border-radius: 4px'
116
+ }
117
+
118
+ // uuid
119
+ let idCounter = 0;
120
+ const uniqueId = (prefix) => {
121
+ return prefix.toString() + ++idCounter;
122
+ }
123
+
124
+ // 操作时间 HH:mm:ss:ms
125
+ const formatTime = (date = new Date()) => {
126
+ const hours = date.getHours().toString().padStart(2, '0');
127
+ const minutes = date.getMinutes().toString().padStart(2, '0');
128
+ const seconds = date.getSeconds().toString().padStart(2, '0');
129
+ const milliseconds = date.getMilliseconds().toString();
130
+
131
+ return `${hours}:${minutes}:${seconds}:${milliseconds}`;
132
+ }
133
+
134
+ // 获取堆栈
135
+ const getStackTrace = () => {
136
+ let stackStr = ''
137
+ let stackArr = []
138
+ // 通过Error获取
139
+ try {
140
+ throw new Error('');
141
+ }
142
+ // catch掉才不会被博睿监控到
143
+ catch (error) {
144
+ stackStr = error.stack || '';
145
+ }
146
+ // 字符串信息分割为数组 并去掉空格
147
+ stackArr = stackStr.split('\n').map((line) => line.trim());
148
+ return stackArr.splice(stackArr[0] === 'Error' ? 2 : 1);
149
+ }
150
+
151
+ // 在堆栈信息数组中 匹配触发事件的函数
152
+ const matchFnCaller = (stackTrace = []) => {
153
+ // 文件在src下的 且排除本文件 没有匹配的返回第0项
154
+ return stackTrace.find(item => item.includes('src') && !item.includes(CURRENT_FILE_NAME)) || stackTrace[0]
155
+ }
156
+
157
+
158
+ // 事件信息map结构 key: eventName value: { count: 触发次数, describe: 事件描述 }
159
+ const eventMap = new Map()
160
+
161
+ // 监听事件 配置logger
162
+ const handleOnEvent = (eventName, value, options) => {
163
+ const stackTrace = getStackTrace()
164
+
165
+ // 匹配事件源 去掉 at 字符
166
+ const source = matchFnCaller(stackTrace).substring(3)
167
+ const describe = eventMap.get(eventName).describe
168
+ const count = eventMap.get(eventName).count
169
+
170
+ const g = options.logExpanded ? console.group : console.groupCollapsed
171
+ g(`%cmittId => ${options.id}`, options.logStyle)
172
+ console.log('eventName:', eventName)
173
+ console.log('eventValue:', value)
174
+ console.log('eventDescribe:', describe)
175
+ console.log('eventSource:', source)
176
+ console.log('count:', count)
177
+ console.log('time:', formatTime())
178
+ console.groupEnd()
179
+ }
180
+
181
+ // 记录事件信息
182
+ const markEventMap = (type, describe) => {
183
+ if (eventMap.has(type)) {
184
+ let event = eventMap.get(type)
185
+ eventMap.set(type, {
186
+ count: ++event.count,
187
+ describe
188
+ })
189
+ } else {
190
+ eventMap.set(type, {
191
+ count: 1,
192
+ describe
193
+ })
194
+ }
195
+ console.log('eventMap', eventMap)
196
+ }
197
+ // emit事件 记录事件信息
198
+ const handleEmitEvent = (emitter) => {
199
+ const originEmit = emitter.emit
200
+ emitter.emit = (type, e, describe = '') => {
201
+ markEventMap(type, describe)
202
+ originEmit(type, e)
203
+ }
204
+ }
205
+
206
+ // 增加emitAsync方法
207
+ // https://github.com/developit/mitt/discussions/157
208
+ const emitAsync = async function(type, e, describe) {
209
+ // 记录事件信息
210
+ markEventMap(type, describe)
211
+
212
+ let handlers = this.all.get(type)
213
+ if (handlers) {
214
+ for (const f of handlers) {
215
+ await f(e)
216
+ }
217
+ }
218
+ handlers = this.all.get('*')
219
+ if (handlers) {
220
+ for (const f of handlers) {
221
+ await f(type, e)
222
+ }
223
+ }
224
+ }
225
+ const mittAsync = (all) => {
226
+ const instance = mitt(all)
227
+ instance.emitAsync = emitAsync
228
+ return instance
229
+ }
230
+
231
+ export default function useMitt(customOption) {
232
+ // 配置项
233
+ const options = Object.assign({
234
+ id: uniqueId('mitt-')
235
+ }, defineOptions, customOption)
236
+
237
+ // 实例化mitt
238
+ const emitter = mittAsync()
239
+
240
+ if (!options.disabled) {
241
+ // 监听所有事件,配置logger
242
+ emitter.on('*', (eventName, value) => handleOnEvent(eventName, value, options))
243
+ // 劫持emit,记录信息
244
+ handleEmitEvent(emitter)
245
+ }
246
+
247
+ return emitter
248
+ }
249
+
250
+ import useMitt from '@/hooks/useMitt'
251
+
252
+ const emitter = useMitt({id: 'listEmitter', logExpanded: true})
253
+ const handleClick = () => {
254
+ emitter.emitAsync('showModal', 'data', '此函数是激活confirm modal')
255
+ .then(() => {
256
+ console.log('confirm') // resolve后打印
257
+ })
258
+ .catch(() => {
259
+ console.log('cancel') // reject后打印
260
+ })
261
+ }
262
+ emitter.on('showModal', () => {
263
+ return new Promise((resolve, reject) => {
264
+ Modal.confirm({
265
+ title: '确认是否XXX',
266
+ onOk() {
267
+ resolve()
268
+ },
269
+ onCancel() {
270
+ reject()
271
+ }
272
+ })
273
+ })
274
+ })
275
+ import useMitt from '@/hooks/useMitt'
276
+ const emitter = useMitt({ id: 'contract-edit', logExpanded: true })
277
+ const handleClick = () => {
278
+ emitter.emit('click', 'data', '这是一个点击事件')
279
+ }
280
+ export default function mitt(all){
281
+ //使用Map存储注册的事件
282
+ all = all || new Map();
283
+
284
+
285
+ return {
286
+ all,
287
+ on(type, handler, priority = 0, listener) {
288
+ const handlers = all!.get(type) || [];
289
+ handlers.push({handler, priority});
290
+ handlers.sort((a, b) => b.priority - a.priority);
291
+ all!.set(type, handlers);
292
+ if (listener) {
293
+ listener({type, handler, priority});
294
+ }
295
+ },
296
+
297
+ off(type, handler) {
298
+ const handlers = all!.get(type);
299
+ if (handlers) {
300
+ if (handler) {
301
+ const index = handlers.findIndex(item => item.handler === handler);
302
+ handlers.splice(index > -1 ? index : 0, 1);
303
+ }
304
+ else {
305
+ all!.set(type, []);
306
+ }
307
+ }
308
+ },
309
+
310
+ emit(type, evt, listener) {
311
+ if (listener) {
312
+ listener({type, evt});
313
+ }
314
+ let handlers = all!.get(type);
315
+ if (handlers) {
316
+ handlers.slice().map(async (handler) => {
317
+ await handler(evt);
318
+ });
319
+ }
320
+ }
321
+ };
322
+ }
323
+
324
+
325
+ function eventListener({type, handler, priority}) {
326
+ console.log(`事件 ${type} 已注册,处理函数为 ${handler},优先级为 ${priority}`);
327
+ }
328
+
329
+
330
+ function eventTriggerListener({type, evt}) {
331
+ console.log(`事件 ${type} 已触发,事件对象为 ${evt}`);
332
+ }
333
+
334
+
335
+ const bus = mitt();
336
+
337
+
338
+ bus.on('click', () => console.log('点击事件已触发'), 1, eventListener);
339
+ bus.emit('click', {target: '按钮'}, eventTriggerListener);
340
+