vite-uni-dev-tool 0.0.6 → 0.0.7

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.
package/README.md CHANGED
@@ -151,7 +151,15 @@ uni.__dev__console.log('hello vite-uni-dev-tool');
151
151
 
152
152
  ## 更新日志
153
153
 
154
+ ### 0.0.07
155
+
156
+ - 监听uni.$on
157
+ - 监听uni.%once
158
+ - 监听uni.$emit
159
+ - 监听uni.$of
160
+
154
161
  ### 0.0.6
162
+
155
163
  - 修复 app 查看 source 重启
156
164
 
157
165
  ### 0.0.5
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <view class="connection-content">
3
3
  <view class="connection-item">
4
- <view class="connection-item-title">网络连接</view>
4
+ <DevToolTitle>网络连接</DevToolTitle>
5
5
  <view class="connection-item-content">
6
6
  <view class="connection-row">
7
7
  <view>IP地址:</view>
@@ -10,8 +10,8 @@
10
10
  netWorkStatus?.isConnected === undefined || netWorkStatus?.ip
11
11
  ? 'info'
12
12
  : netWorkStatus?.isConnected && netWorkStatus?.ip
13
- ? 'success'
14
- : 'error'
13
+ ? 'success'
14
+ : 'error'
15
15
  "
16
16
  >
17
17
  {{
@@ -28,16 +28,16 @@
28
28
  netWorkStatus?.isConnected === undefined
29
29
  ? 'info'
30
30
  : netWorkStatus?.isConnected
31
- ? 'success'
32
- : 'error'
31
+ ? 'success'
32
+ : 'error'
33
33
  "
34
34
  >
35
35
  {{
36
36
  netWorkStatus?.isConnected === undefined
37
37
  ? '未获取'
38
38
  : netWorkStatus?.isConnected
39
- ? '已连接'
40
- : '已断开'
39
+ ? '已连接'
40
+ : '已断开'
41
41
  }}
42
42
  </Tag>
43
43
  </view>
@@ -59,6 +59,7 @@
59
59
  </template>
60
60
  <script lang="ts" setup>
61
61
  import Tag from '../Tag/index.vue';
62
+ import DevToolTitle from '../DevToolTitle/index.vue';
62
63
  defineProps<{
63
64
  netWorkStatus: {
64
65
  isConnected?: boolean;
@@ -73,23 +74,12 @@ defineProps<{
73
74
  overflow: auto;
74
75
  font-size: var(--dev-tool-base-font-size);
75
76
  }
76
- .connection-content .connection-item {
77
+ .connection-item {
77
78
  padding: 16px 16px 0 16px;
78
79
  box-sizing: border-box;
79
80
  }
80
- .connection-content .connection-item .connection-item-title {
81
- display: flex;
82
- align-items: center;
83
- }
84
- .connection-content .connection-item .connection-item-title::before {
85
- content: '';
86
- margin-right: 8px;
87
- width: 2px;
88
- height: 18px;
89
- border-radius: 2px;
90
- background-color: var(--dev-tool-main-color);
91
- }
92
- .connection-content .connection-item .connection-item-content .connection-row {
81
+
82
+ .connection-row {
93
83
  display: flex;
94
84
  align-items: center;
95
85
  justify-content: space-between;
@@ -28,6 +28,7 @@ import { onShow, onHide, onUnload, onLoad } from '@dcloudio/uni-app';
28
28
  import { reactive, ref, nextTick } from 'vue';
29
29
  import DevToolButton from '../DevToolButton/index.vue';
30
30
  import DevToolWindow from '../DevToolWindow/index.vue';
31
+ import { eventBus } from '../../core';
31
32
 
32
33
  import {
33
34
  EVENT_DEV_BUTTON,
@@ -82,7 +83,7 @@ async function onShowDevToolWindow(show: boolean) {
82
83
  }
83
84
 
84
85
  function onSendMessage(param: { type: string; data: Record<string, any> }) {
85
- uni.$emit(DEV_WINDOW_MESSAGE, param);
86
+ eventBus.emit(DEV_WINDOW_MESSAGE, param);
86
87
  }
87
88
 
88
89
  function onDevOptionSend(options: DevTool.DevToolOptions) {
@@ -110,22 +111,22 @@ onLoad(() => {
110
111
  isActive = true;
111
112
  devToolButtonVisible.value = uni.getStorageSync(EVENT_DEV_BUTTON) || false;
112
113
  devToolWindowVisible.value = show;
113
- uni.$on(EVENT_DEV_BUTTON, onShowDevToolButton);
114
- uni.$on(EVENT_DEV_WINDOW, onShowDevToolWindow);
115
- uni.$on(DEV_OPTION_SEND, onDevOptionSend);
114
+ eventBus.on(EVENT_DEV_BUTTON, onShowDevToolButton);
115
+ eventBus.on(EVENT_DEV_WINDOW, onShowDevToolWindow);
116
+ eventBus.on(DEV_OPTION_SEND, onDevOptionSend);
116
117
 
117
118
  nextTick(() => {
118
- uni.$emit(DEV_WINDOW_MESSAGE, { type: DEV_OPTION_GET, data: {} });
119
+ eventBus.emit(DEV_WINDOW_MESSAGE, { type: DEV_OPTION_GET, data: {} });
119
120
  });
120
121
  });
121
122
 
122
123
  onUnload(() => {
123
- uni.$emit(DEV_WINDOW_MESSAGE, {
124
+ eventBus.emit(DEV_WINDOW_MESSAGE, {
124
125
  type: DEV_WINDOW_CLOSE,
125
126
  data: {},
126
127
  });
127
- uni.$off(EVENT_DEV_BUTTON, onShowDevToolButton);
128
- uni.$off(EVENT_DEV_WINDOW, onShowDevToolWindow);
128
+ eventBus.off(EVENT_DEV_BUTTON, onShowDevToolButton);
129
+ eventBus.off(EVENT_DEV_WINDOW, onShowDevToolWindow);
129
130
  });
130
131
 
131
132
  onShow(() => {
@@ -0,0 +1,21 @@
1
+ <template>
2
+ <view class="dev-tool-title" :style="style"> <slot /> </view>
3
+ </template>
4
+ <script lang="ts" setup>
5
+ defineProps<{ style?: any }>();
6
+ </script>
7
+ <style scoped>
8
+ .dev-tool-title {
9
+ display: flex;
10
+ align-items: center;
11
+ height: 32px;
12
+ }
13
+ .dev-tool-title::before {
14
+ content: '';
15
+ margin-right: 8px;
16
+ width: 2px;
17
+ height: 18px;
18
+ border-radius: 2px;
19
+ background-color: var(--dev-tool-main-color);
20
+ }
21
+ </style>
@@ -64,6 +64,12 @@
64
64
  @search="onSearchRoute"
65
65
  @routeRefresh="onRouteRefresh"
66
66
  />
67
+ <UniEvent
68
+ v-if="activeTab === 'UniEvent'"
69
+ :eventList="eventList"
70
+ :eventCount="eventCount"
71
+ @clear="onUniEventClear"
72
+ />
67
73
  <SettingList
68
74
  v-if="activeTab === 'Setting'"
69
75
  :devToolVisible="devToolVisible"
@@ -119,6 +125,7 @@ import DeviceInfo from '../DeviceInfo/index.vue';
119
125
  import WindowInfo from '../WindowInfo/index.vue';
120
126
  import WebSocket from '../WebSocket/index.vue';
121
127
  import UploadList from '../UploadList/index.vue';
128
+ import UniEvent from '../UniEvent/index.vue';
122
129
  import {
123
130
  DEV_BUTTON_SHOW_OR_HIDE,
124
131
  DEV_CONSOLE_CLEAR,
@@ -141,9 +148,11 @@ import {
141
148
  DEV_WEBSOCKET_CLEAR,
142
149
  DEV_WINDOW_INFO,
143
150
  DEV_APP_MESSAGE,
151
+ DEV_UNI_EVENT_CLEAR,
144
152
  } from '../../const';
145
153
  import { debounce, hightLight } from '../../utils/index';
146
154
  import type { DevTool } from '../../type';
155
+ import { eventBus } from '../../core';
147
156
 
148
157
  const items = [
149
158
  {
@@ -171,12 +180,16 @@ const items = [
171
180
  label: 'WebSocket',
172
181
  value: 'WebSocket',
173
182
  },
174
-
175
183
  {
176
184
  key: 'Route',
177
185
  label: 'Route',
178
186
  value: 'Route',
179
187
  },
188
+ {
189
+ key: 'UniEvent',
190
+ label: 'UniEvent',
191
+ value: 'UniEvent',
192
+ },
180
193
  {
181
194
  key: 'Storage',
182
195
  label: 'Storage',
@@ -222,6 +235,7 @@ const backup: {
222
235
  wsList: DevTool.WS[];
223
236
  uploadList: DevTool.UploadItem[];
224
237
  routeList: DevTool.Page[];
238
+ eventList: DevTool.EventItem[];
225
239
  } = {
226
240
  consoleList: [],
227
241
  networkList: [],
@@ -229,6 +243,7 @@ const backup: {
229
243
  wsList: [],
230
244
  uploadList: [],
231
245
  routeList: [],
246
+ eventList: [],
232
247
  };
233
248
  const consoleList = ref<DevTool.ConsoleItem[]>([]);
234
249
  const networkList = ref<DevTool.NetworkItem[]>([]);
@@ -241,6 +256,8 @@ const piniaList = ref<Record<string, any>>({});
241
256
  const systemInfo = ref<Record<string, any>>({});
242
257
  const deviceInfo = ref<Record<string, any>>({});
243
258
  const windowInfo = ref<Record<string, any>>({});
259
+ const eventList = ref<DevTool.EventItem[]>([]);
260
+ const eventCount = ref<DevTool.EventCount>();
244
261
  const netWorkStatus = ref<{
245
262
  isConnected?: boolean;
246
263
  networkType?: string;
@@ -394,6 +411,13 @@ const listenPostMessage = (data: DevTool.WindowData) => {
394
411
  });
395
412
  backup.routeList = [...data.routeList];
396
413
  }
414
+ if (data.eventList) {
415
+ eventList.value = [...data.eventList];
416
+ }
417
+
418
+ if (data.eventCount) {
419
+ eventCount.value = { ...data.eventCount };
420
+ }
397
421
 
398
422
  if (data.vuexList) {
399
423
  vuexList.value = data.vuexList;
@@ -439,11 +463,11 @@ onMounted(() => {
439
463
 
440
464
  onLoad(() => {
441
465
  isActive = true;
442
- uni.$on(DEV_APP_MESSAGE, listenPostMessage);
466
+ eventBus.on(DEV_APP_MESSAGE, listenPostMessage);
443
467
  });
444
468
  onUnload(() => {
445
469
  isActive = false;
446
- uni.$off(DEV_APP_MESSAGE, listenPostMessage);
470
+ eventBus.off(DEV_APP_MESSAGE, listenPostMessage);
447
471
  });
448
472
 
449
473
  onShow(() => {
@@ -704,6 +728,20 @@ function onSearchStorage(value: string) {
704
728
  });
705
729
  }
706
730
 
731
+ function onUniEventClear() {
732
+ eventList.value = [];
733
+ eventCount.value = {
734
+ on: 0,
735
+ once: 0,
736
+ emit: 0,
737
+ off: 0,
738
+ };
739
+ basicSendMessage({
740
+ type: DEV_UNI_EVENT_CLEAR,
741
+ data: {},
742
+ });
743
+ }
744
+
707
745
  function onGoTo(page: DevTool.Page) {
708
746
  basicSendMessage({
709
747
  type: DEV_PAGE_JUMP,
@@ -1,7 +1,7 @@
1
1
  <template>
2
2
  <view class="setting-content">
3
3
  <view class="setting-item">
4
- <view class="setting-item-title">DevTool</view>
4
+ <DevToolTitle>DevTool</DevToolTitle>
5
5
  <view class="setting-item-content">
6
6
  <view class="setting-row">
7
7
  <view>显示调试按钮:</view>
@@ -25,8 +25,8 @@
25
25
  </view>
26
26
 
27
27
  <view class="setting-tips">
28
- 点击后将会销毁调试器,销毁后不再接收调试信息,需要通过
29
- createDevTool() 重新创建
28
+ 点击后将会销毁调试器,销毁后不再接收调试信息,需要通过 createDevTool()
29
+ 重新创建
30
30
  </view>
31
31
  <DButton class="setting-button" @click="onDestructionDevTool">
32
32
  销毁调试器
@@ -52,7 +52,7 @@
52
52
  </view>
53
53
  </view>
54
54
  <view class="setting-item">
55
- <view class="setting-item-title">Log</view>
55
+ <DevToolTitle>Log</DevToolTitle>
56
56
  <view class="setting-item-content">
57
57
  <view class="setting-row">
58
58
  <view>导出当前日志信息:</view>
@@ -121,6 +121,7 @@
121
121
  import { reactive } from 'vue';
122
122
  import Checkbox from '../Checkbox/index.vue';
123
123
  import DButton from '../Button/index.vue';
124
+ import DevToolTitle from '../DevToolTitle/index.vue';
124
125
  const props = defineProps<{
125
126
  devToolVisible?: boolean;
126
127
  sizeFormat?: string;
@@ -204,30 +205,19 @@ function onClearCache() {
204
205
  overflow: auto;
205
206
  font-size: var(--dev-tool-base-font-size);
206
207
  }
207
- .setting-content .setting-item {
208
+ .setting-item {
208
209
  padding: 16px 16px 0 16px;
209
210
  box-sizing: border-box;
210
211
  }
211
- .setting-content .setting-item .setting-item-title {
212
- display: flex;
213
- align-items: center;
214
- }
215
- .setting-content .setting-item .setting-item-title::before {
216
- content: '';
217
- margin-right: 8px;
218
- width: 2px;
219
- height: 18px;
220
- border-radius: 2px;
221
- background-color: var(--dev-tool-main-color);
222
- }
223
- .setting-content .setting-item .setting-item-content .setting-row {
212
+
213
+ .setting-row {
224
214
  display: flex;
225
215
  align-items: center;
226
216
  justify-content: space-between;
227
217
  margin: 8px 0;
228
218
  min-height: 18px;
229
219
  }
230
- .setting-content .setting-tips {
220
+ .setting-tips {
231
221
  color: #616161;
232
222
  font-size: var(--dev-tool-tips-font-size);
233
223
  margin-bottom: 8px;
@@ -0,0 +1,55 @@
1
+ <template>
2
+ <view class="uni-event-item">
3
+ <view class="uni-event-item-row"> {{ eventItem.eventName }} </view>
4
+ <view class="uni-event-item-row">
5
+ <Tag mode="info" v-if="eventItem.type == 'on'">
6
+ {{ eventItem.type }}
7
+ </Tag>
8
+ <Tag mode="warn" v-if="eventItem.type == 'once'">
9
+ {{ eventItem.type }}
10
+ </Tag>
11
+ <Tag mode="main" v-if="eventItem.type == 'emit'">
12
+ {{ eventItem.type }}
13
+ </Tag>
14
+ <Tag mode="error" v-if="eventItem.type == 'off'">
15
+ {{ eventItem.type }}
16
+ </Tag>
17
+ <view class="uni-event-item-right"> {{ eventItem.timer }}</view>
18
+ </view>
19
+
20
+ <view class="uni-event-item-right link">
21
+ {{ eventItem.stock }}
22
+ </view>
23
+ </view>
24
+ </template>
25
+ <script lang="ts" setup>
26
+ import Tag from '../Tag/index.vue';
27
+ import type { DevTool } from '../../type';
28
+
29
+ defineProps<{ eventItem: DevTool.EventItem }>();
30
+ </script>
31
+
32
+ <style scoped>
33
+ .uni-event-item {
34
+ padding: 16px;
35
+ border-bottom: 1px solid var(--dev-tool-border-color);
36
+ box-sizing: border-box;
37
+ }
38
+
39
+ .uni-event-item-row {
40
+ display: flex;
41
+ align-items: center;
42
+ justify-content: space-between;
43
+ height: 28px;
44
+ word-break: break-all;
45
+ }
46
+
47
+ .uni-event-item-right {
48
+ text-align: right;
49
+ word-break: break-all;
50
+ }
51
+ .link {
52
+ cursor: pointer;
53
+ text-decoration: underline;
54
+ }
55
+ </style>
@@ -0,0 +1,85 @@
1
+ <template>
2
+ <view class="uni-event-content">
3
+ <DevToolTitle style="padding: 0 16px">事件触发统计</DevToolTitle>
4
+ <view class="uni-event-statistics">
5
+ <view class="uni-event-statistics-item">
6
+ <view>on: </view>
7
+ <Tag mode="log" style="flex: 1; justify-content: center">
8
+ {{ eventCount?.on ?? 0 }}
9
+ </Tag>
10
+ </view>
11
+ <view class="uni-event-statistics-item">
12
+ <view>once: </view>
13
+ <Tag mode="warn" style="flex: 1; justify-content: center">
14
+ {{ eventCount?.once ?? 0 }}
15
+ </Tag>
16
+ </view>
17
+ <view class="uni-event-statistics-item">
18
+ <view>emit: </view>
19
+ <Tag mode="main" style="flex: 1; justify-content: center">
20
+ {{ eventCount?.emit ?? 0 }}
21
+ </Tag>
22
+ </view>
23
+ <view class="uni-event-statistics-item">
24
+ <view>off: </view>
25
+ <Tag mode="error" style="flex: 1; justify-content: center">
26
+ {{ eventCount?.off ?? 0 }}
27
+ </Tag>
28
+ </view>
29
+ </view>
30
+ <DevToolTitle style="padding: 0 16px"
31
+ >事件触发列表
32
+
33
+ <Tag mode="clear" style="margin-left: auto" @click="emit('clear')">
34
+ 清空
35
+ </Tag>
36
+ </DevToolTitle>
37
+ <view class="uni-event-list">
38
+ <UniEventItem v-for="item in eventList" :eventItem="item" />
39
+ </view>
40
+ </view>
41
+ </template>
42
+ <script lang="ts" setup>
43
+ import DevToolTitle from '../DevToolTitle/index.vue';
44
+ import UniEventItem from './UniEventItem.vue';
45
+ import Tag from '../Tag/index.vue';
46
+ import type { DevTool } from '@/dev/type';
47
+
48
+ defineProps<{
49
+ eventList?: DevTool.EventItem[];
50
+ eventCount?: DevTool.EventCount;
51
+ }>();
52
+
53
+ const emit = defineEmits<{
54
+ (e: 'clear'): void;
55
+ }>();
56
+ </script>
57
+ <style scoped>
58
+ .uni-event-content {
59
+ height: 100%;
60
+ overflow: auto;
61
+ font-size: var(--dev-tool-base-font-size);
62
+ }
63
+
64
+ .uni-event-statistics {
65
+ display: flex;
66
+ align-items: center;
67
+ justify-content: space-around;
68
+ padding: 0 16px;
69
+ height: 32px;
70
+ box-sizing: border-box;
71
+ gap: 12px;
72
+ }
73
+ .uni-event-statistics-item {
74
+ display: flex;
75
+ align-items: center;
76
+ width: 25%;
77
+ }
78
+ .uni-event-statistics-item view:first-child {
79
+ margin-right: 8px;
80
+ }
81
+ .uni-event-list {
82
+ height: calc(100% - 32px * 3);
83
+ overflow: auto;
84
+ }
85
+ </style>
package/dev/const.ts CHANGED
@@ -90,6 +90,8 @@ export const DEV_ROUTE_REFRESH = 'dev-route-refresh';
90
90
  /** 获取 dev option */
91
91
  export const DEV_OPTION = 'dev-option';
92
92
 
93
- export const DEV_OPTION_GET = 'dev-option-get';
93
+ export const DEV_OPTION_GET = 'dev-option-get';
94
94
 
95
- export const DEV_OPTION_SEND = 'dev-option-send';
95
+ export const DEV_OPTION_SEND = 'dev-option-send';
96
+
97
+ export const DEV_UNI_EVENT_CLEAR = 'dev-uni-event-clear';
package/dev/core.ts CHANGED
@@ -6,6 +6,7 @@ import { DevConsole } from './devConsole';
6
6
 
7
7
  import type { DevTool } from './type';
8
8
 
9
+ import { EventBus } from './devEventBus';
9
10
  /**
10
11
  * 备份原生方法
11
12
  *
@@ -26,12 +27,21 @@ const backup = {
26
27
  reject: Promise.reject,
27
28
  connectSocket: uni?.connectSocket,
28
29
  uploadFile: uni?.uploadFile,
30
+ $on: uni.$on,
31
+ $once: uni.$once,
32
+ $emit: uni.$emit,
33
+ $off: uni.$off,
29
34
  __log__: (uni as any).__log__,
30
35
  };
31
36
 
37
+ const eventBus = new EventBus();
38
+
32
39
  const store = new DevStore();
33
40
 
34
- const event = new DevEvent(store);
41
+ const event = new DevEvent({
42
+ store,
43
+ eventBus,
44
+ });
35
45
 
36
46
  const console = new DevConsole(event);
37
47
 
@@ -106,4 +116,5 @@ export {
106
116
  getDevToolOptions,
107
117
  interceptVuexStorage,
108
118
  interceptPiniaStore,
119
+ eventBus,
109
120
  };
@@ -28,6 +28,7 @@ import {
28
28
  DEV_WINDOW_CLOSE,
29
29
  DEV_OPTION_GET,
30
30
  DEV_OPTION_SEND,
31
+ DEV_UNI_EVENT_CLEAR,
31
32
  } from '../const';
32
33
  import { DevStore } from '../devStore';
33
34
  import type { DevTool } from '../type';
@@ -39,6 +40,7 @@ import {
39
40
  saveTextFileH5,
40
41
  saveTextFileMicro,
41
42
  } from '../utils/index';
43
+ import type { EventBus } from '../devEventBus';
42
44
 
43
45
  /**
44
46
  * 事件中心
@@ -49,8 +51,11 @@ import {
49
51
  export class DevEvent {
50
52
  private store: DevStore;
51
53
 
52
- constructor(store: DevStore) {
54
+ private eventBus: EventBus;
55
+
56
+ constructor({ store, eventBus }: { store: DevStore; eventBus: EventBus }) {
53
57
  this.store = store;
58
+ this.eventBus = eventBus;
54
59
  this.acceptMessage();
55
60
  }
56
61
 
@@ -67,7 +72,7 @@ export class DevEvent {
67
72
  data.size = size;
68
73
  data.sizeFormat = sizeFormat;
69
74
 
70
- uni.$emit(DEV_APP_MESSAGE, data);
75
+ this.eventBus.emit(DEV_APP_MESSAGE, data);
71
76
 
72
77
  if (size > this.store.cacheMaxSize) {
73
78
  this.store?.clearDevCache();
@@ -136,8 +141,12 @@ export class DevEvent {
136
141
 
137
142
  this.resetInterceptSwitchTab();
138
143
 
144
+ this.resetUniEvent();
145
+
139
146
  this.store.setDevToolDestroy(true);
140
147
 
148
+ this.eventBus.clear();
149
+
141
150
  console.warn('[DevTool] 调试器已销毁');
142
151
  }
143
152
 
@@ -163,7 +172,7 @@ export class DevEvent {
163
172
  setTimeout(async () => {
164
173
  this.postMessage();
165
174
  }, 100);
166
- uni.$emit(EVENT_DEV_WINDOW, true);
175
+ this.eventBus.emit(EVENT_DEV_WINDOW, true);
167
176
  }
168
177
 
169
178
  /**
@@ -172,7 +181,7 @@ export class DevEvent {
172
181
  * @memberof DevEvent
173
182
  */
174
183
  closeDevToolWindow() {
175
- uni.$emit(EVENT_DEV_WINDOW, false);
184
+ this.eventBus.emit(EVENT_DEV_WINDOW, false);
176
185
  }
177
186
 
178
187
  /**
@@ -183,7 +192,7 @@ export class DevEvent {
183
192
  showDevToolButton() {
184
193
  const isDestroy = uni.getStorageSync(DEV_IS_DESTROY) ?? false;
185
194
  if (isDestroy) return;
186
- uni.$emit(EVENT_DEV_BUTTON, true);
195
+ this.eventBus.emit(EVENT_DEV_BUTTON, true);
187
196
  uni.setStorageSync(EVENT_DEV_BUTTON, true);
188
197
  this.store.setDevToolVisible(true);
189
198
  }
@@ -196,7 +205,7 @@ export class DevEvent {
196
205
  hideDevToolButton() {
197
206
  const isDestroy = uni.getStorageSync(DEV_IS_DESTROY) ?? false;
198
207
  if (isDestroy) return;
199
- uni.$emit(EVENT_DEV_BUTTON, false);
208
+ this.eventBus.emit(EVENT_DEV_BUTTON, false);
200
209
  uni.setStorageSync(EVENT_DEV_BUTTON, false);
201
210
  this.store.setDevToolVisible(false);
202
211
  }
@@ -281,7 +290,7 @@ export class DevEvent {
281
290
  sendDevToolOption() {
282
291
  const options = this.store.getDevToolOptions();
283
292
 
284
- uni.$emit(DEV_OPTION_SEND, options);
293
+ this.eventBus.emit(DEV_OPTION_SEND, options);
285
294
  }
286
295
 
287
296
  /**
@@ -290,7 +299,7 @@ export class DevEvent {
290
299
  * @memberof DevEvent
291
300
  */
292
301
  acceptMessage() {
293
- uni.$on(
302
+ this.eventBus.on(
294
303
  DEV_WINDOW_MESSAGE,
295
304
  (data: {
296
305
  type: string;
@@ -406,6 +415,8 @@ export class DevEvent {
406
415
  // 获取dev options 数据
407
416
  else if (data.type === DEV_OPTION_GET) {
408
417
  this.sendDevToolOption();
418
+ } else if (data.type === DEV_UNI_EVENT_CLEAR) {
419
+ this.uniEventClear();
409
420
  }
410
421
  },
411
422
  );
@@ -662,4 +673,23 @@ export class DevEvent {
662
673
  getDevToolDestroy() {
663
674
  return this.store.getDevToolDestroy();
664
675
  }
676
+
677
+ updateUniEventCount(type: DevTool.EventCountKey) {
678
+ this.store.updateUniEventCount(type);
679
+ this.postMessage();
680
+ }
681
+ updateUniEventList(eventList: DevTool.EventItem[]) {
682
+ this.store.updateUniEventList(eventList);
683
+ this.postMessage();
684
+ }
685
+ uniEventClear() {
686
+ this.store.uniEventClear();
687
+ }
688
+
689
+ resetUniEvent() {
690
+ uni.$on = backup.$on;
691
+ uni.$once = backup.$once;
692
+ uni.$emit = backup.$emit;
693
+ uni.$off = backup.$off;
694
+ }
665
695
  }
@@ -0,0 +1,94 @@
1
+ type EventHandler<T = any> = (payload: T) => void;
2
+
3
+ export class EventBus {
4
+ // 使用 Map 存储事件及其对应的回调函数数组
5
+ private events: Map<string, EventHandler[]> = new Map();
6
+
7
+ /**
8
+ * 订阅事件
9
+ * @param eventName 事件名称
10
+ * @param handler 事件处理函数
11
+ * @returns 返回一个取消订阅的函数
12
+ */
13
+ on<T = any>(eventName: string, handler: EventHandler<T>): () => void {
14
+ if (!this.events.has(eventName)) {
15
+ this.events.set(eventName, []);
16
+ }
17
+ const handlers = this.events.get(eventName)!;
18
+ handlers.push(handler as EventHandler);
19
+
20
+ // 返回取消订阅的函数
21
+ return () => {
22
+ this.off(eventName, handler);
23
+ };
24
+ }
25
+
26
+ /**
27
+ * 发布事件
28
+ * @param eventName 事件名称
29
+ * @param payload 事件携带的数据
30
+ */
31
+ emit<T = any>(eventName: string, payload: T): void {
32
+ if (this.events.has(eventName)) {
33
+ const handlers = [...this.events.get(eventName)!];
34
+ handlers.forEach((handler) => handler(payload));
35
+ }
36
+ }
37
+
38
+ /**
39
+ * 取消订阅事件
40
+ * @param eventName 事件名称
41
+ * @param handler 要取消的事件处理函数
42
+ */
43
+ off<T = any>(eventName: string, handler: EventHandler<T>): void {
44
+ if (this.events.has(eventName)) {
45
+ const handlers = this.events.get(eventName)!;
46
+ this.events.set(
47
+ eventName,
48
+ handlers.filter((h) => h !== handler),
49
+ );
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 只订阅一次事件,触发后自动取消订阅
55
+ * @param eventName 事件名称
56
+ * @param handler 事件处理函数
57
+ */
58
+ once<T = any>(eventName: string, handler: EventHandler<T>): void {
59
+ const wrapper = (payload: T) => {
60
+ handler(payload);
61
+ this.off(eventName, wrapper);
62
+ };
63
+ this.on(eventName, wrapper);
64
+ }
65
+
66
+ /**
67
+ * 清除指定事件的所有订阅者,如果未提供事件名称,则清除所有事件
68
+ * @param eventName 可选的事件名称
69
+ */
70
+ clear(eventName?: string): void {
71
+ if (eventName) {
72
+ this.events.delete(eventName);
73
+ } else {
74
+ this.events.clear();
75
+ }
76
+ }
77
+
78
+ /**
79
+ * 获取指定事件的订阅者数量,如果未提供事件名称,则返回所有事件的订阅者总数
80
+ * @param eventName 可选的事件名称
81
+ * @returns 订阅者数量
82
+ */
83
+ count(eventName?: string): number {
84
+ if (eventName) {
85
+ return this.events.has(eventName)
86
+ ? this.events.get(eventName)!.length
87
+ : 0;
88
+ }
89
+ return Array.from(this.events.values()).reduce(
90
+ (sum, handlers) => sum + handlers.length,
91
+ 0,
92
+ );
93
+ }
94
+ }
@@ -1,3 +1,4 @@
1
+ import { DEV_APP_MESSAGE } from '../const';
1
2
  import { backup } from '../core';
2
3
  import { DevEvent } from '../devEvent';
3
4
  import type { DevTool } from '../type';
@@ -20,6 +21,11 @@ export class DevIntercept {
20
21
 
21
22
  initPinia = false;
22
23
 
24
+ cache$on = new Map();
25
+ cache$once = new Map();
26
+ cache$emit = new Map();
27
+ cache$off = new Map();
28
+
23
29
  constructor(options: DevTool.DevInterceptOptions) {
24
30
  this.event = options.event;
25
31
  this.init(options);
@@ -41,6 +47,8 @@ export class DevIntercept {
41
47
  this.interceptSwitchTab();
42
48
  this.interceptNavigateTo();
43
49
 
50
+ this.interceptUniEvent();
51
+
44
52
  options.enableInterceptPromiseReject && this.interceptPromiseReject();
45
53
  }
46
54
 
@@ -681,4 +689,33 @@ export class DevIntercept {
681
689
  };
682
690
  uni.uploadFile = uploadFile.bind(uni);
683
691
  }
692
+
693
+ interceptUniEventFactory(type: DevTool.EventCountKey) {
694
+ const key = `$${type}` as '$on' | '$emit' | '$once' | '$off';
695
+
696
+ uni[`$${type}`] = (eventName: string, callback: (result: any) => void) => {
697
+ const stockList = new Error()?.stack?.split('\n');
698
+ const stock = stockList?.[2];
699
+ backup?.[key]?.(eventName, callback);
700
+
701
+ this.event.updateUniEventList([
702
+ {
703
+ eventName,
704
+ timer: getCurrentDate(),
705
+ stock,
706
+ type,
707
+ },
708
+ ]);
709
+
710
+ this.event.updateUniEventCount(type);
711
+ };
712
+ }
713
+
714
+ interceptUniEvent() {
715
+ // 判断是否是相同的位置注册,相同的位置注册只算一次
716
+ this.interceptUniEventFactory('on');
717
+ this.interceptUniEventFactory('once');
718
+ this.interceptUniEventFactory('emit');
719
+ this.interceptUniEventFactory('off');
720
+ }
684
721
  }
@@ -11,6 +11,7 @@ import {
11
11
  getWifiIp,
12
12
  isBoolean,
13
13
  } from '../utils/index';
14
+ import { backup } from '../core';
14
15
 
15
16
  /** 数据存储中心 */
16
17
  export class DevStore {
@@ -28,6 +29,13 @@ export class DevStore {
28
29
  wsList: [],
29
30
  netWorkStatus: {},
30
31
  uploadList: [],
32
+ eventList: [],
33
+ eventCount: {
34
+ on: 0,
35
+ once: 0,
36
+ emit: 0,
37
+ off: 0,
38
+ },
31
39
  };
32
40
 
33
41
  /** 调试配置 */
@@ -40,6 +48,9 @@ export class DevStore {
40
48
  private networkMaxSize = 1000;
41
49
  /** ws数据最大值 */
42
50
  private wsDataMaxSize = 1000;
51
+ /** 事件列表最大值 */
52
+ private eventMaxSize = 1000;
53
+
43
54
  /** 缓存最大值 */
44
55
  cacheMaxSize = 8 * 1024 * 1024 * 10;
45
56
 
@@ -314,6 +325,14 @@ export class DevStore {
314
325
  this.state.windowInfo = {};
315
326
  this.state.systemInfo = {};
316
327
  this.state.netWorkStatus = {};
328
+
329
+ this.state.eventList = [];
330
+ this.state.eventCount = {
331
+ on: 0,
332
+ once: 0,
333
+ emit: 0,
334
+ off: 0,
335
+ };
317
336
  }
318
337
 
319
338
  addUploadTask(index: number | string, task: UniApp.UploadTask) {
@@ -599,4 +618,58 @@ export class DevStore {
599
618
  }
600
619
  return '';
601
620
  }
621
+
622
+ /**
623
+ * 新增事件
624
+ *
625
+ * @param {DevTool.EventItem} event
626
+ * @memberof DevStore
627
+ */
628
+ addEventItem(event: DevTool.EventItem) {
629
+ if (!this.state.eventList) {
630
+ this.state.eventList = [];
631
+ }
632
+ this.state.eventList?.push(event);
633
+ }
634
+
635
+ /**
636
+ * 增加注册事件的数量
637
+ *
638
+ * @param {DevTool.EventCountKey} type
639
+ * @memberof DevStore
640
+ */
641
+ updateUniEventCount(type: DevTool.EventCountKey) {
642
+ if (!this.state.eventCount) {
643
+ this.state.eventCount = {
644
+ on: 0,
645
+ once: 0,
646
+ emit: 0,
647
+ off: 0,
648
+ };
649
+ }
650
+ this.state.eventCount[type] = this.state.eventCount[type] + 1;
651
+ }
652
+
653
+ updateUniEventList(evenList: DevTool.EventItem[]) {
654
+ const len = this.state.eventList?.length ?? 0;
655
+ const max = this.eventMaxSize;
656
+
657
+ if (len + evenList.length > max) {
658
+ this.state.eventList?.splice(0, len - max - evenList.length);
659
+ }
660
+ this.state.eventList?.push(...evenList);
661
+ console.log('this.state.eventList: ', this.state.eventList);
662
+
663
+ return this.state.eventList;
664
+ }
665
+
666
+ uniEventClear() {
667
+ this.state.eventCount = {
668
+ on: 0,
669
+ once: 0,
670
+ emit: 0,
671
+ off: 0,
672
+ };
673
+ this.state.eventList = [];
674
+ }
602
675
  }
package/dev/type.ts CHANGED
@@ -175,6 +175,25 @@ export declare namespace DevTool {
175
175
  totalBytesExpectedToSend?: number;
176
176
  };
177
177
 
178
+ type EventCount = {
179
+ on: number;
180
+ once: number;
181
+ emit: number;
182
+ off: number;
183
+ };
184
+ type EventCountKey = keyof EventCount;
185
+
186
+ type EventItem = {
187
+ /** 事件名称 */
188
+ eventName?: string;
189
+ /** 触发事件 */
190
+ timer?: string;
191
+ /** 调用位置 */
192
+ stock?: string;
193
+ /** 事件类型 */
194
+ type?: string;
195
+ };
196
+
178
197
  type WindowData = {
179
198
  devToolVisible?: boolean;
180
199
  consoleList?: ConsoleItem[];
@@ -191,6 +210,8 @@ export declare namespace DevTool {
191
210
  size?: number;
192
211
  sizeFormat?: string;
193
212
  uploadList?: UploadItem[];
213
+ eventCount?: EventCount;
214
+ eventList?: EventItem[];
194
215
  };
195
216
 
196
217
  type DevInterceptOptions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-uni-dev-tool",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "vite-uni-dev-tool, debug, uni-app, 一处编写,到处调试",
5
5
  "keywords": [
6
6
  "vite",