qy-better-lib 0.0.1

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.
Files changed (185) hide show
  1. package/.vscode/extensions.json +3 -0
  2. package/LICENSE +24 -0
  3. package/README.md +2 -0
  4. package/index.html +13 -0
  5. package/package.json +37 -0
  6. package/packages/core/DOCUMENTATION.md +2681 -0
  7. package/packages/core/LICENSE +24 -0
  8. package/packages/core/package.json +67 -0
  9. package/packages/core/types/index.d.ts +39 -0
  10. package/packages/core/types/json.d.ts +4 -0
  11. package/packages/core/types/vue.shim.d.ts +5 -0
  12. package/packages/core/vitest.config.ts +15 -0
  13. package/packages/core/yarn.lock +1233 -0
  14. package/packages/hooks/DOCUMENTATION.md +634 -0
  15. package/packages/hooks/LICENSE +24 -0
  16. package/packages/hooks/__tests__/use-chart/index.test.ts +287 -0
  17. package/packages/hooks/__tests__/use-emit/index.test.ts +248 -0
  18. package/packages/hooks/__tests__/use-fullscreen/index.test.ts +162 -0
  19. package/packages/hooks/__tests__/use-image/index.test.ts +230 -0
  20. package/packages/hooks/__tests__/use-layout-flow/index.test.ts +382 -0
  21. package/packages/hooks/__tests__/use-mqtt/index.test.ts +392 -0
  22. package/packages/hooks/__tests__/use-print/index.test.ts +378 -0
  23. package/packages/hooks/__tests__/use-watermark/index.test.ts +277 -0
  24. package/packages/hooks/__tests__/use-websocket/index.test.ts +402 -0
  25. package/packages/hooks/package.json +104 -0
  26. package/packages/hooks/types/index.d.ts +1 -0
  27. package/packages/hooks/types/json.d.ts +4 -0
  28. package/packages/hooks/types/vue.shim.d.ts +5 -0
  29. package/packages/hooks/yarn.lock +1832 -0
  30. package/packages/lowcode/core/package.json +60 -0
  31. package/packages/lowcode/core/types/index.d.ts +1 -0
  32. package/packages/lowcode/core/types/json.d.ts +4 -0
  33. package/packages/lowcode/core/types/vue.shim.d.ts +5 -0
  34. package/packages/lowcode/core/vite.dist.min.config.js +40 -0
  35. package/packages/lowcode/core/vite.dist.min.config.js.map +1 -0
  36. package/packages/lowcode/core/vite.lib.config.js +74 -0
  37. package/packages/lowcode/core/vite.lib.config.js.map +1 -0
  38. package/packages/lowcode/core/yarn.lock +1132 -0
  39. package/packages/lowcode/mobile/package.json +63 -0
  40. package/packages/lowcode/mobile/types/index.d.ts +1 -0
  41. package/packages/lowcode/mobile/types/json.d.ts +4 -0
  42. package/packages/lowcode/mobile/types/vue.shim.d.ts +77 -0
  43. package/packages/lowcode/mobile/vite.config.js +11 -0
  44. package/packages/lowcode/mobile/vite.config.js.map +1 -0
  45. package/packages/lowcode/mobile/vite.dist.min.config.js +71 -0
  46. package/packages/lowcode/mobile/vite.dist.min.config.js.map +1 -0
  47. package/packages/lowcode/mobile/vite.lib.config.js +137 -0
  48. package/packages/lowcode/mobile/vite.lib.config.js.map +1 -0
  49. package/packages/lowcode/mobile/yarn.lock +3888 -0
  50. package/packages/lowcode/pc/package.json +64 -0
  51. package/packages/lowcode/pc/vite.config.js +11 -0
  52. package/packages/lowcode/pc/vite.config.js.map +1 -0
  53. package/packages/lowcode/pc/vite.dist.min.config.js +71 -0
  54. package/packages/lowcode/pc/vite.dist.min.config.js.map +1 -0
  55. package/packages/lowcode/pc/vite.lib.config.js +137 -0
  56. package/packages/lowcode/pc/vite.lib.config.js.map +1 -0
  57. package/packages/lowcode/pc/yarn.lock +4026 -0
  58. package/packages/pc_components/DOCUMENTATION.md +566 -0
  59. package/packages/pc_components/LICENSE +24 -0
  60. package/packages/pc_components/lib/commDialog/index.d.ts +3 -0
  61. package/packages/pc_components/lib/commDialog/index.js +8 -0
  62. package/packages/pc_components/lib/commDialog/src/index.vue.d.ts +102 -0
  63. package/packages/pc_components/lib/commDialog/src/index.vue.js +43 -0
  64. package/packages/pc_components/lib/commDialog/src/index.vue2.js +4 -0
  65. package/packages/pc_components/lib/commDialog/src/props.d.ts +49 -0
  66. package/packages/pc_components/lib/commDialog/src/props.js +48 -0
  67. package/packages/pc_components/lib/commDialog/src/type.d.ts +39 -0
  68. package/packages/pc_components/lib/commForm/index.d.ts +3 -0
  69. package/packages/pc_components/lib/commForm/index.js +8 -0
  70. package/packages/pc_components/lib/commForm/src/index.vue.d.ts +95 -0
  71. package/packages/pc_components/lib/commForm/src/index.vue.js +50 -0
  72. package/packages/pc_components/lib/commForm/src/index.vue2.js +4 -0
  73. package/packages/pc_components/lib/commForm/src/props.d.ts +49 -0
  74. package/packages/pc_components/lib/commForm/src/props.js +47 -0
  75. package/packages/pc_components/lib/commForm/src/type.d.ts +8 -0
  76. package/packages/pc_components/lib/contextMenu/index.d.ts +5 -0
  77. package/packages/pc_components/lib/contextMenu/index.js +8 -0
  78. package/packages/pc_components/lib/contextMenu/src/index.vue.d.ts +91 -0
  79. package/packages/pc_components/lib/contextMenu/src/index.vue.js +153 -0
  80. package/packages/pc_components/lib/contextMenu/src/index.vue2.js +4 -0
  81. package/packages/pc_components/lib/contextMenu/src/props.d.ts +32 -0
  82. package/packages/pc_components/lib/contextMenu/src/props.js +27 -0
  83. package/packages/pc_components/lib/contextMenu/src/type.d.ts +11 -0
  84. package/packages/pc_components/lib/describeView/index.d.ts +2 -0
  85. package/packages/pc_components/lib/describeView/index.js +7 -0
  86. package/packages/pc_components/lib/describeView/src/index.vue.d.ts +61 -0
  87. package/packages/pc_components/lib/describeView/src/index.vue.js +84 -0
  88. package/packages/pc_components/lib/describeView/src/index.vue2.js +4 -0
  89. package/packages/pc_components/lib/describeView/src/props.d.ts +35 -0
  90. package/packages/pc_components/lib/describeView/src/props.js +33 -0
  91. package/packages/pc_components/lib/describeView/src/type.d.ts +10 -0
  92. package/packages/pc_components/lib/index.d.ts +18 -0
  93. package/packages/pc_components/lib/index.js +40 -0
  94. package/packages/pc_components/lib/simpleVirtualList/index.d.ts +2 -0
  95. package/packages/pc_components/lib/simpleVirtualList/index.js +7 -0
  96. package/packages/pc_components/lib/simpleVirtualList/src/index.vue.d.ts +28 -0
  97. package/packages/pc_components/lib/simpleVirtualList/src/index.vue.js +101 -0
  98. package/packages/pc_components/lib/simpleVirtualList/src/index.vue2.js +4 -0
  99. package/packages/pc_components/lib/simpleVirtualList/src/type.d.ts +12 -0
  100. package/packages/pc_components/lib/styles/comm-form.scss +4 -0
  101. package/packages/pc_components/lib/styles/context-menu.scss +47 -0
  102. package/packages/pc_components/lib/styles/describe-view.scss +25 -0
  103. package/packages/pc_components/lib/styles/index.scss +6 -0
  104. package/packages/pc_components/lib/styles/simple-virtual-list.scss +25 -0
  105. package/packages/pc_components/lib/styles/table-extend.scss +100 -0
  106. package/packages/pc_components/lib/styles/text-button.scss +71 -0
  107. package/packages/pc_components/lib/tableExtend/index.d.ts +3 -0
  108. package/packages/pc_components/lib/tableExtend/index.js +7 -0
  109. package/packages/pc_components/lib/tableExtend/props.d.ts +130 -0
  110. package/packages/pc_components/lib/tableExtend/props.js +128 -0
  111. package/packages/pc_components/lib/tableExtend/src/colVisible.vue.d.ts +3 -0
  112. package/packages/pc_components/lib/tableExtend/src/colVisible.vue.js +128 -0
  113. package/packages/pc_components/lib/tableExtend/src/colVisible.vue2.js +4 -0
  114. package/packages/pc_components/lib/tableExtend/src/index.vue.d.ts +186 -0
  115. package/packages/pc_components/lib/tableExtend/src/index.vue.js +234 -0
  116. package/packages/pc_components/lib/tableExtend/src/index.vue2.js +4 -0
  117. package/packages/pc_components/lib/tableExtend/src/valueCol.vue.d.ts +24 -0
  118. package/packages/pc_components/lib/tableExtend/src/valueCol.vue.js +29 -0
  119. package/packages/pc_components/lib/tableExtend/src/valueCol.vue2.js +4 -0
  120. package/packages/pc_components/lib/tableExtend/type.d.ts +165 -0
  121. package/packages/pc_components/lib/tableExtend/type.js +9 -0
  122. package/packages/pc_components/lib/tableExtend/useTable.d.ts +27 -0
  123. package/packages/pc_components/lib/tableExtend/useTable.js +115 -0
  124. package/packages/pc_components/lib/textButton/index.d.ts +2 -0
  125. package/packages/pc_components/lib/textButton/index.js +7 -0
  126. package/packages/pc_components/lib/textButton/src/index.vue.d.ts +42 -0
  127. package/packages/pc_components/lib/textButton/src/index.vue.js +44 -0
  128. package/packages/pc_components/lib/textButton/src/index.vue2.js +4 -0
  129. package/packages/pc_components/lib/textButton/src/props.d.ts +23 -0
  130. package/packages/pc_components/lib/textButton/src/props.js +30 -0
  131. package/packages/pc_components/lib/textButton/src/type.d.ts +30 -0
  132. package/packages/pc_components/lib/utils/index.d.ts +77 -0
  133. package/packages/pc_components/lib/utils/index.js +124 -0
  134. package/packages/pc_components/package.json +73 -0
  135. package/packages/pc_components/types/index.d.ts +9 -0
  136. package/packages/pc_components/types/json.d.ts +4 -0
  137. package/packages/pc_components/types/vue.shim.d.ts +5 -0
  138. package/packages/pc_components/yarn.lock +1641 -0
  139. package/postcss.config.js +19 -0
  140. package/public/vite.svg +1 -0
  141. package/src/App.vue +37 -0
  142. package/src/api/index.ts +42 -0
  143. package/src/assets/vue.svg +1 -0
  144. package/src/document/core.md +2680 -0
  145. package/src/document/hooks.md +632 -0
  146. package/src/document/pc_components.md +566 -0
  147. package/src/locales/en.json +1 -0
  148. package/src/locales/zh-cn.json +1 -0
  149. package/src/main.ts +13 -0
  150. package/src/plugins/element-plus.ts +16 -0
  151. package/src/plugins/i18n.ts +17 -0
  152. package/src/plugins/index.ts +13 -0
  153. package/src/plugins/store.ts +12 -0
  154. package/src/router/index.ts +20 -0
  155. package/src/shared/chartType.ts +279 -0
  156. package/src/shared/const.ts +160 -0
  157. package/src/shared/lib.ts +129 -0
  158. package/src/shared/request.ts +255 -0
  159. package/src/shared/type.ts +48 -0
  160. package/src/shared/utils/date.ts +83 -0
  161. package/src/shared/utils/utils.ts +85 -0
  162. package/src/store/index.ts +22 -0
  163. package/src/store/modules/app.ts +17 -0
  164. package/src/store/modules/shared.ts +21 -0
  165. package/src/styles/index.scss +169 -0
  166. package/src/types/app.d.ts +23 -0
  167. package/src/types/echarts.d.ts +5 -0
  168. package/src/types/index.d.ts +1 -0
  169. package/src/types/json.d.ts +4 -0
  170. package/src/types/scss.d.ts +9 -0
  171. package/src/types/vue.shim.d.ts +5 -0
  172. package/src/views/docs/docs-content.vue +379 -0
  173. package/src/views/docs/docs-header.vue +130 -0
  174. package/src/views/docs/docs-sidebar.vue +76 -0
  175. package/src/views/docs/hooks.ts +600 -0
  176. package/src/views/docs/index.vue +30 -0
  177. package/src/views/docs/styles/docs-content.scss +106 -0
  178. package/src/views/docs/styles/docs-header.scss +20 -0
  179. package/src/views/docs/styles/docs-sidebar.scss +182 -0
  180. package/src/views/docs/styles/index.scss +14 -0
  181. package/src/views/docs/type.ts +101 -0
  182. package/src/views/index.vue +34 -0
  183. package/tsconfig.json +37 -0
  184. package/tsconfig.node.json +8 -0
  185. package/vite.config.ts +54 -0
@@ -0,0 +1,402 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import { use_web_socket } from '../../src/use-websocket';
3
+
4
+ // 模拟WebSocket对象
5
+ class MockWebSocket {
6
+ readyState: number;
7
+ url: string;
8
+ onopen: ((event: Event) => void) | null;
9
+ onerror: ((event: Event) => void) | null;
10
+ onclose: ((event: CloseEvent) => void) | null;
11
+ onmessage: ((event: MessageEvent) => void) | null;
12
+
13
+ constructor(url: string) {
14
+ this.url = url;
15
+ this.readyState = WebSocket.CONNECTING;
16
+ this.onopen = null;
17
+ this.onerror = null;
18
+ this.onclose = null;
19
+ this.onmessage = null;
20
+ }
21
+
22
+ send(data: string) {
23
+ // 模拟发送消息
24
+ }
25
+
26
+ close() {
27
+ this.readyState = WebSocket.CLOSED;
28
+ if (this.onclose) {
29
+ const closeEvent = new CloseEvent('close', { code: 1000, reason: 'Normal closure' });
30
+ this.onclose(closeEvent);
31
+ }
32
+ }
33
+
34
+ // 模拟连接成功
35
+ simulateOpen() {
36
+ this.readyState = WebSocket.OPEN;
37
+ if (this.onopen) {
38
+ this.onopen(new Event('open'));
39
+ }
40
+ }
41
+
42
+ // 模拟连接错误
43
+ simulateError() {
44
+ if (this.onerror) {
45
+ this.onerror(new Event('error'));
46
+ }
47
+ }
48
+
49
+ // 模拟接收消息
50
+ simulateMessage(data: any) {
51
+ if (this.onmessage) {
52
+ const messageEvent = new MessageEvent('message', { data });
53
+ this.onmessage(messageEvent);
54
+ }
55
+ }
56
+ }
57
+
58
+ // 模拟WebSocket构造函数
59
+ const mockWebSocketClass = vi.fn().mockImplementation((url: string) => new MockWebSocket(url));
60
+
61
+ // 模拟全局对象
62
+ beforeEach(() => {
63
+ vi.clearAllMocks();
64
+ global.WebSocket = mockWebSocketClass as any;
65
+ // 模拟setTimeout和clearTimeout
66
+ vi.useFakeTimers();
67
+ });
68
+
69
+ afterEach(() => {
70
+ vi.resetModules();
71
+ vi.useRealTimers();
72
+ });
73
+
74
+ describe('use_web_socket', () => {
75
+ it('should initialize with default values', () => {
76
+ const mockReceive = vi.fn();
77
+ const { ws, create, close, send_message, get_status, reconnect, start_heartbeat, stop_heartbeat } = use_web_socket({
78
+ server: 'ws://localhost:8080',
79
+ receive: mockReceive,
80
+ });
81
+
82
+ expect(ws).toBeUndefined();
83
+ expect(typeof create).toBe('function');
84
+ expect(typeof close).toBe('function');
85
+ expect(typeof send_message).toBe('function');
86
+ expect(typeof get_status).toBe('function');
87
+ expect(typeof reconnect).toBe('function');
88
+ expect(typeof start_heartbeat).toBe('function');
89
+ expect(typeof stop_heartbeat).toBe('function');
90
+ });
91
+
92
+ it('should create and connect WebSocket successfully', () => {
93
+ const mockReceive = vi.fn();
94
+ const mockOnOpen = vi.fn();
95
+
96
+ const { create, get_status } = use_web_socket({
97
+ server: 'ws://localhost:8080',
98
+ receive: mockReceive,
99
+ on_open: mockOnOpen,
100
+ auto_connect: false,
101
+ });
102
+
103
+ // 创建连接
104
+ create();
105
+
106
+ expect(mockWebSocketClass).toHaveBeenCalledWith('ws://localhost:8080');
107
+
108
+ // 模拟连接成功
109
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
110
+ if (wsInstance) {
111
+ wsInstance.simulateOpen();
112
+ expect(mockOnOpen).toHaveBeenCalled();
113
+ }
114
+ });
115
+
116
+ it('should handle WebSocket error', () => {
117
+ const mockReceive = vi.fn();
118
+ const mockOnError = vi.fn();
119
+
120
+ const { create } = use_web_socket({
121
+ server: 'ws://localhost:8080',
122
+ receive: mockReceive,
123
+ on_error: mockOnError,
124
+ auto_connect: false,
125
+ });
126
+
127
+ // 创建连接
128
+ create();
129
+
130
+ // 模拟连接错误
131
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
132
+ if (wsInstance) {
133
+ wsInstance.simulateError();
134
+ expect(mockOnError).toHaveBeenCalled();
135
+ }
136
+ });
137
+
138
+ it('should handle WebSocket close', () => {
139
+ const mockReceive = vi.fn();
140
+ const mockOnClose = vi.fn();
141
+
142
+ const { create } = use_web_socket({
143
+ server: 'ws://localhost:8080',
144
+ receive: mockReceive,
145
+ on_close: mockOnClose,
146
+ auto_connect: false,
147
+ });
148
+
149
+ // 创建连接
150
+ create();
151
+
152
+ // 模拟连接成功
153
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
154
+ if (wsInstance) {
155
+ wsInstance.simulateOpen();
156
+
157
+ // 关闭连接
158
+ wsInstance.close();
159
+
160
+ expect(mockOnClose).toHaveBeenCalled();
161
+ }
162
+ });
163
+
164
+ it('should send message successfully', () => {
165
+ const mockReceive = vi.fn();
166
+
167
+ const { create, send_message } = use_web_socket({
168
+ server: 'ws://localhost:8080',
169
+ receive: mockReceive,
170
+ auto_connect: false,
171
+ });
172
+
173
+ // 创建连接
174
+ create();
175
+
176
+ // 模拟连接成功
177
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
178
+ if (wsInstance) {
179
+ wsInstance.simulateOpen();
180
+
181
+ // 发送消息
182
+ const sendSpy = vi.spyOn(wsInstance, 'send');
183
+ const result = send_message('test message');
184
+
185
+ expect(result).toBe(true);
186
+ expect(sendSpy).toHaveBeenCalledWith('test message');
187
+ }
188
+ });
189
+
190
+ it('should handle send message when not connected', () => {
191
+ const mockReceive = vi.fn();
192
+
193
+ const { send_message } = use_web_socket({
194
+ server: 'ws://localhost:8080',
195
+ receive: mockReceive,
196
+ auto_connect: false,
197
+ });
198
+
199
+ // 未连接时发送消息
200
+ const result = send_message('test message');
201
+
202
+ expect(result).toBe(false);
203
+ });
204
+
205
+ it('should receive message', () => {
206
+ const mockReceive = vi.fn();
207
+
208
+ const { create } = use_web_socket({
209
+ server: 'ws://localhost:8080',
210
+ receive: mockReceive,
211
+ auto_connect: false,
212
+ });
213
+
214
+ // 创建连接
215
+ create();
216
+
217
+ // 模拟连接成功
218
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
219
+ if (wsInstance) {
220
+ wsInstance.simulateOpen();
221
+
222
+ // 模拟接收消息
223
+ wsInstance.simulateMessage('test message');
224
+
225
+ expect(mockReceive).toHaveBeenCalled();
226
+ }
227
+ });
228
+
229
+ it('should reconnect automatically', () => {
230
+ const mockReceive = vi.fn();
231
+
232
+ const { create } = use_web_socket({
233
+ server: 'ws://localhost:8080',
234
+ receive: mockReceive,
235
+ auto_reconnect: true,
236
+ reconnect_interval: 1000,
237
+ auto_connect: false,
238
+ });
239
+
240
+ // 创建连接
241
+ create();
242
+ expect(mockWebSocketClass).toHaveBeenCalledTimes(1);
243
+
244
+ // 模拟连接成功
245
+ const wsInstance1 = (global.WebSocket as any).mock.instances[0];
246
+ if (wsInstance1) {
247
+ wsInstance1.simulateOpen();
248
+
249
+ // 关闭连接(非干净关闭)
250
+ wsInstance1.close();
251
+
252
+ // 快进时间,触发重连
253
+ vi.advanceTimersByTime(1000);
254
+
255
+ expect(mockWebSocketClass).toHaveBeenCalledTimes(2);
256
+ }
257
+ });
258
+
259
+ it('should start and stop heartbeat', () => {
260
+ const mockReceive = vi.fn();
261
+
262
+ const { create, start_heartbeat, stop_heartbeat } = use_web_socket({
263
+ server: 'ws://localhost:8080',
264
+ receive: mockReceive,
265
+ heartbeat_interval: 5000,
266
+ auto_connect: false,
267
+ });
268
+
269
+ // 创建连接
270
+ create();
271
+
272
+ // 模拟连接成功
273
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
274
+ if (wsInstance) {
275
+ wsInstance.simulateOpen();
276
+
277
+ // 启动心跳
278
+ start_heartbeat();
279
+
280
+ // 快进时间,触发心跳
281
+ const sendSpy = vi.spyOn(wsInstance, 'send');
282
+ vi.advanceTimersByTime(5000);
283
+
284
+ expect(sendSpy).toHaveBeenCalledWith('ping');
285
+
286
+ // 停止心跳
287
+ stop_heartbeat();
288
+
289
+ // 快进时间,不应再触发心跳
290
+ sendSpy.mockClear();
291
+ vi.advanceTimersByTime(5000);
292
+
293
+ expect(sendSpy).not.toHaveBeenCalled();
294
+ }
295
+ });
296
+
297
+ it('should get correct connection status', () => {
298
+ const mockReceive = vi.fn();
299
+
300
+ const { create, get_status, close } = use_web_socket({
301
+ server: 'ws://localhost:8080',
302
+ receive: mockReceive,
303
+ auto_connect: false,
304
+ });
305
+
306
+ // 初始状态
307
+ expect(get_status()).toBe('CLOSED');
308
+
309
+ // 创建连接
310
+ create();
311
+
312
+ // 模拟连接成功
313
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
314
+ if (wsInstance) {
315
+ wsInstance.simulateOpen();
316
+ }
317
+
318
+ // 关闭连接
319
+ close();
320
+ expect(get_status()).toBe('CLOSED');
321
+ });
322
+
323
+ it('should handle connection with auto_connect', () => {
324
+ const mockReceive = vi.fn();
325
+
326
+ use_web_socket({
327
+ server: 'ws://localhost:8080',
328
+ receive: mockReceive,
329
+ auto_connect: true,
330
+ });
331
+
332
+ expect(mockWebSocketClass).toHaveBeenCalledWith('ws://localhost:8080');
333
+ });
334
+
335
+ it('should handle connection error with auto_reconnect', () => {
336
+ const mockReceive = vi.fn();
337
+
338
+ use_web_socket({
339
+ server: 'ws://localhost:8080',
340
+ receive: mockReceive,
341
+ auto_reconnect: true,
342
+ reconnect_interval: 1000,
343
+ auto_connect: true,
344
+ });
345
+
346
+ expect(mockWebSocketClass).toHaveBeenCalledTimes(1);
347
+
348
+ // 模拟连接错误
349
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
350
+ if (wsInstance) {
351
+ wsInstance.simulateError();
352
+
353
+ // 快进时间,触发重连
354
+ vi.advanceTimersByTime(1000);
355
+
356
+ expect(mockWebSocketClass).toHaveBeenCalledTimes(2);
357
+ }
358
+ });
359
+
360
+ it('should handle message queue when not connected', () => {
361
+ const mockReceive = vi.fn();
362
+
363
+ const { send_message, create } = use_web_socket({
364
+ server: 'ws://localhost:8080',
365
+ receive: mockReceive,
366
+ auto_connect: false,
367
+ });
368
+
369
+ // 未连接时发送消息
370
+ send_message('message 1');
371
+ send_message('message 2');
372
+
373
+ // 创建连接
374
+ create();
375
+
376
+ // 模拟连接成功
377
+ const wsInstance = (global.WebSocket as any).mock.instances[0];
378
+ if (wsInstance) {
379
+ const sendSpy = vi.spyOn(wsInstance, 'send');
380
+ wsInstance.simulateOpen();
381
+
382
+ // 验证队列中的消息是否被发送
383
+ expect(sendSpy).toHaveBeenCalledTimes(2);
384
+ }
385
+ });
386
+
387
+ it('should limit message queue size', () => {
388
+ const mockReceive = vi.fn();
389
+
390
+ const { send_message } = use_web_socket({
391
+ server: 'ws://localhost:8080',
392
+ receive: mockReceive,
393
+ auto_connect: false,
394
+ max_message_queue_size: 2,
395
+ });
396
+
397
+ // 未连接时发送多条消息
398
+ send_message('message 1');
399
+ send_message('message 2');
400
+ send_message('message 3'); // 这应该替换最旧的消息
401
+ });
402
+ });
@@ -0,0 +1,104 @@
1
+ {
2
+ "name": "@qy_better_lib/hooks",
3
+ "private": false,
4
+ "version": "0.2.9",
5
+ "description": "qy better lib hooks",
6
+ "author": "luhuiming",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "build:lib": "vite build --config vite.lib.config.ts",
10
+ "build:dist-min": "vite build --config vite.dist.min.config.ts",
11
+ "build": "yarn build:lib && yarn build:dist-min",
12
+ "test": "vitest run",
13
+ "test:watch": "vitest"
14
+ },
15
+ "main": "lib/index.mjs",
16
+ "module": "lib/index.mjs",
17
+ "types": "lib/index.d.ts",
18
+ "typesVersions": {
19
+ "*": {
20
+ "lib/index.mjs": [
21
+ "lib/index.d.ts"
22
+ ],
23
+ "@qy-better-lib/hooks": [
24
+ "lib/index.d.ts"
25
+ ],
26
+ "*": [
27
+ "*",
28
+ "dist/*",
29
+ "lib/*",
30
+ "lib/*.d.ts",
31
+ "lib/*/index.d.ts"
32
+ ]
33
+ }
34
+ },
35
+ "exports": {
36
+ ".": {
37
+ "import": "./lib/index.mjs",
38
+ "types": "./lib/index.d.ts"
39
+ },
40
+ "./*": {
41
+ "import": "./lib/*/index.mjs",
42
+ "types": "./lib/*/index.d.ts"
43
+ },
44
+ "./use-chart": {
45
+ "import": "./lib/use-chart/index.mjs",
46
+ "types": "./lib/use-chart/index.d.ts"
47
+ },
48
+ "./use-layout-flow": {
49
+ "import": "./lib/use-layout-flow/index.mjs",
50
+ "types": "./lib/use-layout-flow/index.d.ts"
51
+ },
52
+ "./use-mqtt": {
53
+ "import": "./lib/use-mqtt/index.mjs",
54
+ "types": "./lib/use-mqtt/index.d.ts"
55
+ }
56
+ },
57
+ "devDependencies": {
58
+ "@antv/x6": "^2.18.1",
59
+ "@qy_better_lib/core": "^0.2.8",
60
+ "@types/dagre": "^0.7.53",
61
+ "@types/node": "^24.5.2",
62
+ "@vitejs/plugin-vue": "^6.0.1",
63
+ "dagre": "^0.8.5",
64
+ "echarts": "5.6.0",
65
+ "mitt": "^3.0.1",
66
+ "mqtt": "^5.14.1",
67
+ "vite": "^7.1.7",
68
+ "vite-plugin-dts": "^4.5.4",
69
+ "vitest": "^4.0.18",
70
+ "vue": "^3.5.22"
71
+ },
72
+ "peerDependencies": {
73
+ "@antv/x6": "^2.18.1",
74
+ "@qy_better_lib/core": "^0.2.0",
75
+ "dagre": "^0.8.5",
76
+ "echarts": "5.6.0",
77
+ "mitt": "^3.0.1",
78
+ "mqtt": "^5.14.1",
79
+ "vue": "^3.5.22"
80
+ },
81
+ "peerDependenciesMeta": {
82
+ "@antv/x6": {
83
+ "optional": true
84
+ },
85
+ "dagre": {
86
+ "optional": true
87
+ },
88
+ "echarts": {
89
+ "optional": true
90
+ },
91
+ "mitt": {
92
+ "optional": true
93
+ },
94
+ "mqtt": {
95
+ "optional": true
96
+ },
97
+ "@qy_better_lib/core": {
98
+ "optional": false
99
+ }
100
+ },
101
+ "dependencies": {
102
+ "@qy_better_lib/hooks": "^0.2.6"
103
+ }
104
+ }
@@ -0,0 +1 @@
1
+ declare type $TODO = any
@@ -0,0 +1,4 @@
1
+ declare module "*.json" {
2
+ const value: any;
3
+ export default value;
4
+ }
@@ -0,0 +1,5 @@
1
+ declare module '*.vue' {
2
+ import { ComponentOptions } from 'vue'
3
+ const Component: ComponentOptions
4
+ export default Component
5
+ }