zcw-shared 1.22.0 → 1.23.0
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/dist/functions/algorithm/visualizeGarbageCollection.d.ts +44 -0
- package/dist/functions/algorithm/visualizeGarbageCollection.js +263 -0
- package/dist/functions/algorithm/visualizeGarbageCollection.js.map +1 -0
- package/dist/functions/dom/createThrottledEventListener.d.ts +11 -0
- package/dist/functions/dom/createThrottledEventListener.js +51 -0
- package/dist/functions/dom/createThrottledEventListener.js.map +1 -0
- package/dist/functions/string/testRegex.d.ts +20 -0
- package/dist/functions/string/testRegex.js +300 -0
- package/dist/functions/string/testRegex.js.map +1 -0
- package/dist/functions/string/visualizeRegexSteps.d.ts +39 -0
- package/dist/functions/string/visualizeRegexSteps.js +446 -0
- package/dist/functions/string/visualizeRegexSteps.js.map +1 -0
- package/dist/functions/tencent-cloud/upload.cos.js +44 -36
- package/dist/functions/tencent-cloud/upload.cos.js.map +1 -1
- package/dist/reactive/useReactiveSSE.d.ts +46 -0
- package/dist/reactive/useReactiveSSE.js +212 -0
- package/dist/reactive/useReactiveSSE.js.map +1 -0
- package/dist/reactive/useReactiveWebsocket.d.ts +50 -0
- package/dist/reactive/useReactiveWebsocket.js +250 -0
- package/dist/reactive/useReactiveWebsocket.js.map +1 -0
- package/package.json +11 -3
- package/references/browser.d.ts +4 -0
- package/references/eventsource.d.ts +121 -0
- package/references/websocket.d.ts +121 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
export var SSEStatus;
|
|
2
|
+
(function (SSEStatus) {
|
|
3
|
+
SSEStatus["CONNECTING"] = "CONNECTING";
|
|
4
|
+
SSEStatus["CONNECTED"] = "CONNECTED";
|
|
5
|
+
SSEStatus["DISCONNECTED"] = "DISCONNECTED";
|
|
6
|
+
SSEStatus["ERROR"] = "ERROR";
|
|
7
|
+
})(SSEStatus || (SSEStatus = {}));
|
|
8
|
+
export function useReactiveSSE(url, options = {}, deps) {
|
|
9
|
+
const defaultOptions = {
|
|
10
|
+
withCredentials: false,
|
|
11
|
+
immediate: true,
|
|
12
|
+
reconnect: {
|
|
13
|
+
enabled: true,
|
|
14
|
+
delay: 3000,
|
|
15
|
+
maxRetries: 5,
|
|
16
|
+
retryDelayMultiplier: 1.5
|
|
17
|
+
},
|
|
18
|
+
onConnected: () => { },
|
|
19
|
+
onDisconnected: () => { },
|
|
20
|
+
onError: () => { },
|
|
21
|
+
onMessage: () => { }
|
|
22
|
+
};
|
|
23
|
+
const config = { ...defaultOptions, ...options };
|
|
24
|
+
if (options.reconnect) {
|
|
25
|
+
config.reconnect = { ...defaultOptions.reconnect, ...options.reconnect };
|
|
26
|
+
}
|
|
27
|
+
const state = {
|
|
28
|
+
status: SSEStatus.DISCONNECTED,
|
|
29
|
+
data: null,
|
|
30
|
+
lastEventId: '',
|
|
31
|
+
error: null,
|
|
32
|
+
retryCount: 0,
|
|
33
|
+
es: null
|
|
34
|
+
};
|
|
35
|
+
const subscribers = new Set();
|
|
36
|
+
const notify = () => {
|
|
37
|
+
subscribers.forEach(listener => listener());
|
|
38
|
+
};
|
|
39
|
+
let reconnectTimer = null;
|
|
40
|
+
let isManualClose = false;
|
|
41
|
+
let isReconnecting = false;
|
|
42
|
+
const customListeners = new Map();
|
|
43
|
+
const clearReconnect = () => {
|
|
44
|
+
if (reconnectTimer !== null) {
|
|
45
|
+
deps.clearTimeout(reconnectTimer);
|
|
46
|
+
reconnectTimer = null;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const attemptReconnect = () => {
|
|
50
|
+
deps.log?.(`[SSE-重连] 尝试重连: enabled=${config.reconnect.enabled}, isManualClose=${isManualClose}, isReconnecting=${isReconnecting}`);
|
|
51
|
+
if (!config.reconnect.enabled || isManualClose || isReconnecting) {
|
|
52
|
+
deps.log?.('[SSE-重连] 跳过重连');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const maxRetries = config.reconnect.maxRetries || 0;
|
|
56
|
+
if (maxRetries > 0 && state.retryCount >= maxRetries) {
|
|
57
|
+
deps.log?.(`[SSE-重连] 已达到最大重连次数 ${maxRetries}`);
|
|
58
|
+
state.status = SSEStatus.ERROR;
|
|
59
|
+
state.error = new Error(`重连失败:已达到最大重连次数 ${maxRetries}`);
|
|
60
|
+
notify();
|
|
61
|
+
isReconnecting = false;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const delay = (config.reconnect.delay || 3000) * Math.pow(config.reconnect.retryDelayMultiplier || 1, state.retryCount);
|
|
65
|
+
deps.log?.(`[SSE-重连] 将在 ${delay}ms 后重连(第 ${state.retryCount + 1} 次)`);
|
|
66
|
+
isReconnecting = true;
|
|
67
|
+
clearReconnect();
|
|
68
|
+
state.status = SSEStatus.CONNECTING;
|
|
69
|
+
notify();
|
|
70
|
+
reconnectTimer = deps.setTimeout(() => {
|
|
71
|
+
state.retryCount++;
|
|
72
|
+
deps.log?.(`[SSE-重连] 开始第 ${state.retryCount} 次重连`);
|
|
73
|
+
try {
|
|
74
|
+
connect();
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
isReconnecting = false;
|
|
78
|
+
}
|
|
79
|
+
}, delay);
|
|
80
|
+
};
|
|
81
|
+
const connect = () => {
|
|
82
|
+
if (state.es && state.es.readyState !== deps.EventSource.CLOSED) {
|
|
83
|
+
deps.log?.('[SSE] 已有活跃连接,跳过');
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
isManualClose = false;
|
|
87
|
+
state.status = SSEStatus.CONNECTING;
|
|
88
|
+
state.error = null;
|
|
89
|
+
notify();
|
|
90
|
+
try {
|
|
91
|
+
const eventSourceInit = config.withCredentials
|
|
92
|
+
? { withCredentials: true }
|
|
93
|
+
: undefined;
|
|
94
|
+
const es = new deps.EventSource(url, eventSourceInit);
|
|
95
|
+
es.onopen = (event) => {
|
|
96
|
+
if (isManualClose) {
|
|
97
|
+
deps.log?.('[SSE] 连接建立但已标记为手动关闭,忽略');
|
|
98
|
+
es.close();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
deps.log?.('[SSE] 连接成功');
|
|
102
|
+
state.status = SSEStatus.CONNECTED;
|
|
103
|
+
state.error = null;
|
|
104
|
+
state.retryCount = 0;
|
|
105
|
+
state.es = es;
|
|
106
|
+
customListeners.forEach((listeners, eventType) => {
|
|
107
|
+
listeners.forEach(listener => {
|
|
108
|
+
deps.log?.(`[SSE] 注册事件监听器: ${eventType}`);
|
|
109
|
+
es.addEventListener(eventType, listener);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
notify();
|
|
113
|
+
config.onConnected(es);
|
|
114
|
+
};
|
|
115
|
+
es.onmessage = (event) => {
|
|
116
|
+
deps.log?.('[SSE] 收到消息:', event.data);
|
|
117
|
+
state.data = event.data;
|
|
118
|
+
state.lastEventId = event.lastEventId;
|
|
119
|
+
notify();
|
|
120
|
+
config.onMessage(es, event);
|
|
121
|
+
};
|
|
122
|
+
es.onerror = (event) => {
|
|
123
|
+
deps.log?.('[SSE] 连接错误');
|
|
124
|
+
state.error = new Error('SSE 连接错误');
|
|
125
|
+
notify();
|
|
126
|
+
config.onError(es, event);
|
|
127
|
+
if (es.readyState === deps.EventSource.CLOSED) {
|
|
128
|
+
deps.log?.('[SSE] 连接已关闭');
|
|
129
|
+
if (state.es === es) {
|
|
130
|
+
state.status = SSEStatus.DISCONNECTED;
|
|
131
|
+
state.es = null;
|
|
132
|
+
notify();
|
|
133
|
+
}
|
|
134
|
+
config.onDisconnected(es, event);
|
|
135
|
+
if (!isManualClose && !isReconnecting) {
|
|
136
|
+
attemptReconnect();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
deps.log?.('[SSE] 创建连接失败:', error);
|
|
143
|
+
state.status = SSEStatus.ERROR;
|
|
144
|
+
state.error = error instanceof Error ? error : new Error('SSE 创建失败');
|
|
145
|
+
notify();
|
|
146
|
+
attemptReconnect();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const disconnect = () => {
|
|
150
|
+
deps.log?.('[SSE] 手动断开连接');
|
|
151
|
+
isManualClose = true;
|
|
152
|
+
isReconnecting = false;
|
|
153
|
+
clearReconnect();
|
|
154
|
+
const es = state.es;
|
|
155
|
+
if (es) {
|
|
156
|
+
state.es = null;
|
|
157
|
+
try {
|
|
158
|
+
es.close();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
deps.log?.('[SSE] 关闭连接失败(忽略):', error);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
state.status = SSEStatus.DISCONNECTED;
|
|
165
|
+
state.retryCount = 0;
|
|
166
|
+
notify();
|
|
167
|
+
};
|
|
168
|
+
const addEventListener = (type, listener) => {
|
|
169
|
+
if (!customListeners.has(type)) {
|
|
170
|
+
customListeners.set(type, new Set());
|
|
171
|
+
}
|
|
172
|
+
customListeners.get(type).add(listener);
|
|
173
|
+
if (state.es) {
|
|
174
|
+
state.es.addEventListener(type, listener);
|
|
175
|
+
}
|
|
176
|
+
return () => {
|
|
177
|
+
const listeners = customListeners.get(type);
|
|
178
|
+
if (listeners) {
|
|
179
|
+
listeners.delete(listener);
|
|
180
|
+
if (listeners.size === 0) {
|
|
181
|
+
customListeners.delete(type);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (state.es) {
|
|
185
|
+
state.es.removeEventListener(type, listener);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
const subscribe = (listener) => {
|
|
190
|
+
subscribers.add(listener);
|
|
191
|
+
return () => subscribers.delete(listener);
|
|
192
|
+
};
|
|
193
|
+
const dispose = () => {
|
|
194
|
+
disconnect();
|
|
195
|
+
subscribers.clear();
|
|
196
|
+
customListeners.clear();
|
|
197
|
+
isManualClose = false;
|
|
198
|
+
isReconnecting = false;
|
|
199
|
+
};
|
|
200
|
+
if (config.immediate) {
|
|
201
|
+
connect();
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
state,
|
|
205
|
+
connect,
|
|
206
|
+
disconnect,
|
|
207
|
+
addEventListener,
|
|
208
|
+
subscribe,
|
|
209
|
+
dispose
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=useReactiveSSE.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReactiveSSE.js","sourceRoot":"","sources":["../../src/reactive/useReactiveSSE.ts"],"names":[],"mappings":"AAMA,MAAM,CAAN,IAAY,SAKX;AALD,WAAY,SAAS;IACnB,sCAAyB,CAAA;IACzB,oCAAuB,CAAA;IACvB,0CAA6B,CAAA;IAC7B,4BAAe,CAAA;AACjB,CAAC,EALW,SAAS,KAAT,SAAS,QAKpB;AA2ID,MAAM,UAAU,cAAc,CAC5B,GAAW,EACX,UAAiC,EAAE,EACnC,IAAwB;IAGxB,MAAM,cAAc,GAAoC;QACtD,eAAe,EAAE,KAAK;QACtB,SAAS,EAAE,IAAI;QACf,SAAS,EAAE;YACT,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,IAAI;YACX,UAAU,EAAE,CAAC;YACb,oBAAoB,EAAE,GAAG;SAC1B;QACD,WAAW,EAAE,GAAG,EAAE,GAAE,CAAC;QACrB,cAAc,EAAE,GAAG,EAAE,GAAE,CAAC;QACxB,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC;QACjB,SAAS,EAAE,GAAG,EAAE,GAAE,CAAC;KACpB,CAAA;IAED,MAAM,MAAM,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAA;IAChD,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;QACtB,MAAM,CAAC,SAAS,GAAG,EAAE,GAAG,cAAc,CAAC,SAAS,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAA;IAC1E,CAAC;IAGD,MAAM,KAAK,GAAqB;QAC9B,MAAM,EAAE,SAAS,CAAC,YAAY;QAC9B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,GAAG,EAAE;YACV,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;YAC3C,IAAI,SAAS,EAAE,CAAC;gBACd,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;gBAC1B,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;oBACzB,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;gBAC9B,CAAC;YACH,CAAC;YACD,IAAI,KAAK,CAAC,EAAE,EAAE,CAAC;gBACb,KAAK,CAAC,EAAE,CAAC,mBAAmB,CAAC,IAAI,EAAE,QAAe,CAAC,CAAA;YACrD,CAAC;QACH,CAAC,CAAA;IACH,CAAC,CAAA;IAGD,MAAM,SAAS,GAAG,CAAC,QAAoB,EAAgB,EAAE;QACvD,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QACzB,OAAO,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;IAC3C,CAAC,CAAA;IAGD,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,UAAU,EAAE,CAAA;QACZ,WAAW,CAAC,KAAK,EAAE,CAAA;QACnB,eAAe,CAAC,KAAK,EAAE,CAAA;QAEvB,aAAa,GAAG,KAAK,CAAA;QACrB,cAAc,GAAG,KAAK,CAAA;IACxB,CAAC,CAAA;IAGD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,OAAO,EAAE,CAAA;IACX,CAAC;IAED,OAAO;QACL,KAAK;QACL,OAAO;QACP,UAAU;QACV,gBAAgB;QAChB,SAAS;QACT,OAAO;KACR,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { WebSocketConstructor, WebSocket } from '../../references/websocket.d';
|
|
2
|
+
import type { setTimeout, clearTimeout } from '../../references/timer.d';
|
|
3
|
+
import type { Blob } from '../../references/blob.d';
|
|
4
|
+
export type WebSocketMessage = string | ArrayBufferLike | Blob | ArrayBufferView;
|
|
5
|
+
export declare enum WebSocketStatus {
|
|
6
|
+
CONNECTING = "CONNECTING",
|
|
7
|
+
CONNECTED = "CONNECTED",
|
|
8
|
+
DISCONNECTED = "DISCONNECTED",
|
|
9
|
+
ERROR = "ERROR"
|
|
10
|
+
}
|
|
11
|
+
export interface ReconnectOptions {
|
|
12
|
+
enabled?: boolean;
|
|
13
|
+
delay?: number;
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
retryDelayMultiplier?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface UseReactiveWebsocketOptions {
|
|
18
|
+
protocols?: string | string[];
|
|
19
|
+
immediate?: boolean;
|
|
20
|
+
heartbeat?: number;
|
|
21
|
+
heartbeatMessage?: WebSocketMessage;
|
|
22
|
+
heartbeatTimeout?: number;
|
|
23
|
+
reconnect?: ReconnectOptions;
|
|
24
|
+
onConnected?: (ws: WebSocket) => void;
|
|
25
|
+
onDisconnected?: (ws: WebSocket, event: any) => void;
|
|
26
|
+
onError?: (ws: WebSocket, event: any) => void;
|
|
27
|
+
onMessage?: (ws: WebSocket, event: any) => void;
|
|
28
|
+
}
|
|
29
|
+
export interface ReactiveWebSocketState {
|
|
30
|
+
status: WebSocketStatus;
|
|
31
|
+
data: any;
|
|
32
|
+
error: Error | null;
|
|
33
|
+
retryCount: number;
|
|
34
|
+
ws: WebSocket | null;
|
|
35
|
+
}
|
|
36
|
+
export interface ReactiveWebSocketReturn {
|
|
37
|
+
state: ReactiveWebSocketState;
|
|
38
|
+
send: (data: WebSocketMessage) => boolean;
|
|
39
|
+
connect: () => void;
|
|
40
|
+
disconnect: (code?: number, reason?: string) => void;
|
|
41
|
+
subscribe: (listener: () => void) => () => void;
|
|
42
|
+
dispose: () => void;
|
|
43
|
+
}
|
|
44
|
+
export interface UseReactiveWebsocketDeps {
|
|
45
|
+
WebSocket: WebSocketConstructor;
|
|
46
|
+
setTimeout: typeof setTimeout;
|
|
47
|
+
clearTimeout: typeof clearTimeout;
|
|
48
|
+
log?: (...args: any[]) => void;
|
|
49
|
+
}
|
|
50
|
+
export declare function useReactiveWebsocket(url: string, options: UseReactiveWebsocketOptions | undefined, deps: UseReactiveWebsocketDeps): ReactiveWebSocketReturn;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
export var WebSocketStatus;
|
|
2
|
+
(function (WebSocketStatus) {
|
|
3
|
+
WebSocketStatus["CONNECTING"] = "CONNECTING";
|
|
4
|
+
WebSocketStatus["CONNECTED"] = "CONNECTED";
|
|
5
|
+
WebSocketStatus["DISCONNECTED"] = "DISCONNECTED";
|
|
6
|
+
WebSocketStatus["ERROR"] = "ERROR";
|
|
7
|
+
})(WebSocketStatus || (WebSocketStatus = {}));
|
|
8
|
+
export function useReactiveWebsocket(url, options = {}, deps) {
|
|
9
|
+
const defaultOptions = {
|
|
10
|
+
protocols: [],
|
|
11
|
+
immediate: true,
|
|
12
|
+
heartbeat: 0,
|
|
13
|
+
heartbeatMessage: 'ping',
|
|
14
|
+
heartbeatTimeout: 5000,
|
|
15
|
+
reconnect: {
|
|
16
|
+
enabled: true,
|
|
17
|
+
delay: 3000,
|
|
18
|
+
maxRetries: 5,
|
|
19
|
+
retryDelayMultiplier: 1.5
|
|
20
|
+
},
|
|
21
|
+
onConnected: () => { },
|
|
22
|
+
onDisconnected: () => { },
|
|
23
|
+
onError: () => { },
|
|
24
|
+
onMessage: () => { }
|
|
25
|
+
};
|
|
26
|
+
const config = { ...defaultOptions, ...options };
|
|
27
|
+
if (options.reconnect) {
|
|
28
|
+
config.reconnect = { ...defaultOptions.reconnect, ...options.reconnect };
|
|
29
|
+
}
|
|
30
|
+
const state = {
|
|
31
|
+
status: WebSocketStatus.DISCONNECTED,
|
|
32
|
+
data: null,
|
|
33
|
+
error: null,
|
|
34
|
+
retryCount: 0,
|
|
35
|
+
ws: null
|
|
36
|
+
};
|
|
37
|
+
const subscribers = new Set();
|
|
38
|
+
const notify = () => {
|
|
39
|
+
subscribers.forEach(listener => listener());
|
|
40
|
+
};
|
|
41
|
+
let heartbeatTimer = null;
|
|
42
|
+
let reconnectTimer = null;
|
|
43
|
+
let isManualClose = false;
|
|
44
|
+
let isReconnecting = false;
|
|
45
|
+
let lastMessageTime = 0;
|
|
46
|
+
const clearHeartbeat = () => {
|
|
47
|
+
if (heartbeatTimer !== null) {
|
|
48
|
+
deps.clearTimeout(heartbeatTimer);
|
|
49
|
+
heartbeatTimer = null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const clearReconnect = () => {
|
|
53
|
+
if (reconnectTimer !== null) {
|
|
54
|
+
deps.clearTimeout(reconnectTimer);
|
|
55
|
+
reconnectTimer = null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const startHeartbeat = () => {
|
|
59
|
+
clearHeartbeat();
|
|
60
|
+
if (config.heartbeat <= 0 || !state.ws) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const checkHeartbeat = () => {
|
|
64
|
+
if (!state.ws || state.ws.readyState !== deps.WebSocket.OPEN) {
|
|
65
|
+
deps.log?.('[心跳] 连接未打开,停止心跳检测');
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const now = Date.now();
|
|
69
|
+
const timeSinceLastMessage = now - lastMessageTime;
|
|
70
|
+
const timeoutThreshold = config.heartbeat + config.heartbeatTimeout;
|
|
71
|
+
deps.log?.(`[心跳] 检查: 距上次消息 ${timeSinceLastMessage}ms, 超时阈值 ${timeoutThreshold}ms`);
|
|
72
|
+
if (timeSinceLastMessage > timeoutThreshold) {
|
|
73
|
+
deps.log?.('[心跳] ⚠️ 检测到超时,主动断开并重连');
|
|
74
|
+
clearHeartbeat();
|
|
75
|
+
const oldWs = state.ws;
|
|
76
|
+
state.ws = null;
|
|
77
|
+
state.status = WebSocketStatus.DISCONNECTED;
|
|
78
|
+
notify();
|
|
79
|
+
try {
|
|
80
|
+
oldWs.close();
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
deps.log?.('[心跳] 关闭连接失败(忽略):', error);
|
|
84
|
+
}
|
|
85
|
+
if (!isManualClose && !isReconnecting) {
|
|
86
|
+
deps.log?.('[心跳] 开始重连流程');
|
|
87
|
+
attemptReconnect();
|
|
88
|
+
}
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
deps.log?.('[心跳] 发送心跳消息');
|
|
93
|
+
state.ws.send(config.heartbeatMessage);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
deps.log?.('[心跳] 发送失败:', error);
|
|
97
|
+
}
|
|
98
|
+
heartbeatTimer = deps.setTimeout(checkHeartbeat, config.heartbeat);
|
|
99
|
+
};
|
|
100
|
+
heartbeatTimer = deps.setTimeout(checkHeartbeat, config.heartbeat);
|
|
101
|
+
};
|
|
102
|
+
const attemptReconnect = () => {
|
|
103
|
+
if (!config.reconnect.enabled || isManualClose || isReconnecting) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const maxRetries = config.reconnect.maxRetries || 0;
|
|
107
|
+
if (maxRetries > 0 && state.retryCount >= maxRetries) {
|
|
108
|
+
state.status = WebSocketStatus.ERROR;
|
|
109
|
+
state.error = new Error(`重连失败:已达到最大重连次数 ${maxRetries}`);
|
|
110
|
+
notify();
|
|
111
|
+
isReconnecting = false;
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const delay = (config.reconnect.delay || 3000) * Math.pow(config.reconnect.retryDelayMultiplier || 1, state.retryCount);
|
|
115
|
+
isReconnecting = true;
|
|
116
|
+
clearReconnect();
|
|
117
|
+
reconnectTimer = deps.setTimeout(() => {
|
|
118
|
+
state.retryCount++;
|
|
119
|
+
try {
|
|
120
|
+
connect();
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
isReconnecting = false;
|
|
124
|
+
}
|
|
125
|
+
}, delay);
|
|
126
|
+
};
|
|
127
|
+
const connect = () => {
|
|
128
|
+
if (state.ws && state.ws.readyState !== deps.WebSocket.CLOSED) {
|
|
129
|
+
deps.log?.('[连接] 已有活跃连接,跳过');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
isManualClose = false;
|
|
133
|
+
state.status = WebSocketStatus.CONNECTING;
|
|
134
|
+
state.error = null;
|
|
135
|
+
notify();
|
|
136
|
+
try {
|
|
137
|
+
const ws = new deps.WebSocket(url, config.protocols);
|
|
138
|
+
ws.onopen = (event) => {
|
|
139
|
+
if (isManualClose) {
|
|
140
|
+
deps.log?.('[连接] 连接建立但已标记为手动关闭,忽略');
|
|
141
|
+
ws.close();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
state.status = WebSocketStatus.CONNECTED;
|
|
145
|
+
state.error = null;
|
|
146
|
+
state.retryCount = 0;
|
|
147
|
+
state.ws = ws;
|
|
148
|
+
notify();
|
|
149
|
+
lastMessageTime = Date.now();
|
|
150
|
+
deps.log?.('[连接] 连接成功,记录时间:', lastMessageTime);
|
|
151
|
+
if (config.heartbeat > 0) {
|
|
152
|
+
deps.log?.(`[连接] 启动心跳检测: 间隔=${config.heartbeat}ms, 超时=${config.heartbeatTimeout}ms`);
|
|
153
|
+
}
|
|
154
|
+
startHeartbeat();
|
|
155
|
+
config.onConnected(ws);
|
|
156
|
+
};
|
|
157
|
+
ws.onmessage = (event) => {
|
|
158
|
+
state.data = event.data;
|
|
159
|
+
const oldTime = lastMessageTime;
|
|
160
|
+
lastMessageTime = Date.now();
|
|
161
|
+
deps.log?.(`[消息] 收到消息,更新时间: ${oldTime} -> ${lastMessageTime}`);
|
|
162
|
+
notify();
|
|
163
|
+
config.onMessage(ws, event);
|
|
164
|
+
};
|
|
165
|
+
ws.onerror = (event) => {
|
|
166
|
+
deps.log?.('[错误] WebSocket 错误');
|
|
167
|
+
state.error = new Error('WebSocket 连接错误');
|
|
168
|
+
notify();
|
|
169
|
+
config.onError(ws, event);
|
|
170
|
+
};
|
|
171
|
+
ws.onclose = (event) => {
|
|
172
|
+
clearHeartbeat();
|
|
173
|
+
deps.log?.(`[关闭] 连接关闭: code=${event.code}, wasClean=${event.wasClean}, isManualClose=${isManualClose}`);
|
|
174
|
+
if (state.ws === ws) {
|
|
175
|
+
state.status = WebSocketStatus.DISCONNECTED;
|
|
176
|
+
state.ws = null;
|
|
177
|
+
notify();
|
|
178
|
+
}
|
|
179
|
+
config.onDisconnected(ws, event);
|
|
180
|
+
const shouldReconnect = !isManualClose && !event.wasClean && !isReconnecting;
|
|
181
|
+
deps.log?.(`[关闭] 是否重连: ${shouldReconnect}`);
|
|
182
|
+
if (shouldReconnect) {
|
|
183
|
+
attemptReconnect();
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
state.status = WebSocketStatus.ERROR;
|
|
189
|
+
state.error = error instanceof Error ? error : new Error('WebSocket 创建失败');
|
|
190
|
+
notify();
|
|
191
|
+
attemptReconnect();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
const disconnect = (code, reason) => {
|
|
195
|
+
deps.log?.('[断开] 手动断开连接');
|
|
196
|
+
isManualClose = true;
|
|
197
|
+
isReconnecting = false;
|
|
198
|
+
clearHeartbeat();
|
|
199
|
+
clearReconnect();
|
|
200
|
+
const ws = state.ws;
|
|
201
|
+
if (ws) {
|
|
202
|
+
state.ws = null;
|
|
203
|
+
try {
|
|
204
|
+
ws.close(code, reason);
|
|
205
|
+
}
|
|
206
|
+
catch (error) {
|
|
207
|
+
deps.log?.('[断开] 关闭连接失败(忽略):', error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
state.status = WebSocketStatus.DISCONNECTED;
|
|
211
|
+
state.retryCount = 0;
|
|
212
|
+
notify();
|
|
213
|
+
};
|
|
214
|
+
const send = (data) => {
|
|
215
|
+
if (!state.ws || state.ws.readyState !== deps.WebSocket.OPEN) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
try {
|
|
219
|
+
state.ws.send(data);
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
state.error = error instanceof Error ? error : new Error('发送消息失败');
|
|
224
|
+
notify();
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
const subscribe = (listener) => {
|
|
229
|
+
subscribers.add(listener);
|
|
230
|
+
return () => subscribers.delete(listener);
|
|
231
|
+
};
|
|
232
|
+
const dispose = () => {
|
|
233
|
+
disconnect();
|
|
234
|
+
subscribers.clear();
|
|
235
|
+
isManualClose = false;
|
|
236
|
+
isReconnecting = false;
|
|
237
|
+
};
|
|
238
|
+
if (config.immediate) {
|
|
239
|
+
connect();
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
state,
|
|
243
|
+
send,
|
|
244
|
+
connect,
|
|
245
|
+
disconnect,
|
|
246
|
+
subscribe,
|
|
247
|
+
dispose
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=useReactiveWebsocket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReactiveWebsocket.js","sourceRoot":"","sources":["../../src/reactive/useReactiveWebsocket.ts"],"names":[],"mappings":"AAYA,MAAM,CAAN,IAAY,eAKX;AALD,WAAY,eAAe;IACzB,4CAAyB,CAAA;IACzB,0CAAuB,CAAA;IACvB,gDAA6B,CAAA;IAC7B,kCAAe,CAAA;AACjB,CAAC,EALW,eAAe,KAAf,eAAe,QAK1B;AA6ID,MAAM,UAAU,oBAAoB,CAClC,GAAW,EACX,UAAuC,EAAE,EACzC,IAA8B;IAG9B,MAAM,cAAc,GAA0C;QAC5D,SAAS,EAAE,EAAE;QACb,SAAS,EAAE,IAAI;QACf,SAAS,EAAE,CAAC;QACZ,gBAAgB,EAAE,MAAM;QACxB,gBAAgB,EAAE,IAAI;QACtB,SAAS,EAAE;YACT,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,IAAI;YACX,UAAU,EAAE,CAAC;YACb,oBAAoB,EAAE,GAAG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;QACZ,WAAW,CAAC,KAAK,EAAE,CAAA;QAEnB,aAAa,GAAG,KAAK,CAAA;QACrB,cAAc,GAAG,KAAK,CAAA;IACxB,CAAC,CAAA;IAGD,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACrB,OAAO,EAAE,CAAA;IACX,CAAC;IAED,OAAO;QACL,KAAK;QACL,IAAI;QACJ,OAAO;QACP,UAAU;QACV,SAAS;QACT,OAAO;KACR,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zcw-shared",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.23.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"references",
|
|
6
6
|
"dist",
|
|
@@ -23,15 +23,18 @@
|
|
|
23
23
|
"publish:prerelease": "npx tsx scripts/publish.ts prerelease",
|
|
24
24
|
"docs:dev": "vitepress dev docs",
|
|
25
25
|
"docs:build": "NODE_OPTIONS=--max-old-space-size=4096 vitepress build docs",
|
|
26
|
-
"docs:preview": "vitepress preview docs"
|
|
26
|
+
"docs:preview": "vitepress preview docs",
|
|
27
|
+
"deploy:tcb": "npx tsx scripts/deploy-tcb.ts",
|
|
28
|
+
"deploy:cos": "npx tsx scripts/deploy-cos.ts"
|
|
27
29
|
},
|
|
28
30
|
"devDependencies": {
|
|
29
31
|
"@cloudbase/manager-node": "^4.3.2",
|
|
32
|
+
"cos-nodejs-sdk-v5": "^2.14.4",
|
|
30
33
|
"tsx": "^4.20.3",
|
|
31
34
|
"typescript": "^5.8.3",
|
|
32
35
|
"vitepress": "^1.6.4",
|
|
33
36
|
"vue": "^3.5.22",
|
|
34
|
-
"zcw-shared": "^1.
|
|
37
|
+
"zcw-shared": "^1.22.0",
|
|
35
38
|
"zcw-vue-ui": "^1.11.3"
|
|
36
39
|
},
|
|
37
40
|
"exports": {
|
|
@@ -66,6 +69,7 @@
|
|
|
66
69
|
"./functions/algorithm/threeSum": "./dist/functions/algorithm/threeSum.js",
|
|
67
70
|
"./functions/algorithm/topologicalSort": "./dist/functions/algorithm/topologicalSort.js",
|
|
68
71
|
"./functions/algorithm/trap": "./dist/functions/algorithm/trap.js",
|
|
72
|
+
"./functions/algorithm/visualizeGarbageCollection": "./dist/functions/algorithm/visualizeGarbageCollection.js",
|
|
69
73
|
"./functions/android/buildProject": "./dist/functions/android/buildProject.js",
|
|
70
74
|
"./functions/android/detectAndroidModules": "./dist/functions/android/detectAndroidModules.js",
|
|
71
75
|
"./functions/android/modifyGradle": "./dist/functions/android/modifyGradle.js",
|
|
@@ -115,6 +119,7 @@
|
|
|
115
119
|
"./functions/design-tokens/generateVariables": "./dist/functions/design-tokens/generateVariables.js",
|
|
116
120
|
"./functions/design-tokens/mixColors": "./dist/functions/design-tokens/mixColors.js",
|
|
117
121
|
"./functions/design-tokens/parseDesignTokens": "./dist/functions/design-tokens/parseDesignTokens.js",
|
|
122
|
+
"./functions/dom/createThrottledEventListener": "./dist/functions/dom/createThrottledEventListener.js",
|
|
118
123
|
"./functions/dom/demonstrateResourceLoading": "./dist/functions/dom/demonstrateResourceLoading.js",
|
|
119
124
|
"./functions/dom/detectOverflow": "./dist/functions/dom/detectOverflow.js",
|
|
120
125
|
"./functions/dom/getElementRect": "./dist/functions/dom/getElementRect.js",
|
|
@@ -188,6 +193,7 @@
|
|
|
188
193
|
"./functions/string/palindrome": "./dist/functions/string/palindrome.js",
|
|
189
194
|
"./functions/string/similarity": "./dist/functions/string/similarity.js",
|
|
190
195
|
"./functions/string/template": "./dist/functions/string/template.js",
|
|
196
|
+
"./functions/string/testRegex": "./dist/functions/string/testRegex.js",
|
|
191
197
|
"./functions/string/wordCount": "./dist/functions/string/wordCount.js",
|
|
192
198
|
"./functions/tencent-cloud/deploy.tcb": "./dist/functions/tencent-cloud/deploy.tcb.js",
|
|
193
199
|
"./functions/tencent-cloud/getSecret": "./dist/functions/tencent-cloud/getSecret.js",
|
|
@@ -234,6 +240,8 @@
|
|
|
234
240
|
"./reactive/createReactivePermission": "./dist/reactive/createReactivePermission.js",
|
|
235
241
|
"./reactive/createReactivePinia": "./dist/reactive/createReactivePinia.js",
|
|
236
242
|
"./reactive/useReactiveCookie": "./dist/reactive/useReactiveCookie.js",
|
|
243
|
+
"./reactive/useReactiveSSE": "./dist/reactive/useReactiveSSE.js",
|
|
244
|
+
"./reactive/useReactiveWebsocket": "./dist/reactive/useReactiveWebsocket.js",
|
|
237
245
|
"./vue-hooks/browser/useBattery": "./dist/vue-hooks/browser/useBattery.js",
|
|
238
246
|
"./vue-hooks/browser/useBluetooth": "./dist/vue-hooks/browser/useBluetooth.js",
|
|
239
247
|
"./vue-hooks/browser/useCamera": "./dist/vue-hooks/browser/useCamera.js",
|
package/references/browser.d.ts
CHANGED
|
@@ -111,6 +111,10 @@ export interface Window {
|
|
|
111
111
|
// Navigator API
|
|
112
112
|
navigator: Navigator
|
|
113
113
|
|
|
114
|
+
// 事件监听API
|
|
115
|
+
addEventListener(type: string, listener: EventListener, options?: boolean | AddEventListenerOptions): void
|
|
116
|
+
removeEventListener(type: string, listener: EventListener, options?: boolean | EventListenerOptions): void
|
|
117
|
+
|
|
114
118
|
// 开发环境标识
|
|
115
119
|
__DEV__?: boolean
|
|
116
120
|
|