yuang-framework-ui-common 1.0.121 → 1.0.123
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getLocalStorageItem, setLocalStorageItem } from "../../utils/storageUtils";
|
|
2
|
+
import { getIsDebug } from '../../utils/htmlUtils';
|
|
2
3
|
|
|
3
4
|
// 🔥 1. 优化常量:支持模块隔离的存储键
|
|
4
5
|
export const getSseClientIdKey = (moduleCode?: string) => {
|
|
@@ -96,14 +97,18 @@ export class FrameworkSseClient {
|
|
|
96
97
|
|
|
97
98
|
this.reconnectAttempts = 0;
|
|
98
99
|
this.openHandler?.();
|
|
99
|
-
|
|
100
|
+
if(getIsDebug()) {
|
|
101
|
+
console.log('[SSE] 连接已建立(Fetch 流式)');
|
|
102
|
+
}
|
|
100
103
|
|
|
101
104
|
while (true) {
|
|
102
105
|
if (this.isManualClose) break;
|
|
103
106
|
|
|
104
107
|
const { done, value } = await reader.read();
|
|
105
108
|
if (done) {
|
|
106
|
-
|
|
109
|
+
if(getIsDebug()) {
|
|
110
|
+
console.log('[SSE] 流式响应已结束');
|
|
111
|
+
}
|
|
107
112
|
break;
|
|
108
113
|
}
|
|
109
114
|
|
|
@@ -137,12 +142,14 @@ export class FrameworkSseClient {
|
|
|
137
142
|
|
|
138
143
|
// 兼容非标准消息格式
|
|
139
144
|
if (!cleanMessage.startsWith('data:') && !cleanMessage.startsWith('event:')) {
|
|
140
|
-
|
|
141
|
-
|
|
145
|
+
if(getIsDebug()) {
|
|
146
|
+
console.log('[SSE] 兼容非标准消息格式:', cleanMessage);
|
|
147
|
+
}
|
|
148
|
+
const messageEvent = {
|
|
142
149
|
data: cleanMessage,
|
|
143
150
|
type: 'message'
|
|
144
151
|
} as MessageEvent;
|
|
145
|
-
this.messageHandler(cleanMessage,
|
|
152
|
+
this.messageHandler(cleanMessage, messageEvent);
|
|
146
153
|
return;
|
|
147
154
|
}
|
|
148
155
|
|
|
@@ -171,23 +178,26 @@ export class FrameworkSseClient {
|
|
|
171
178
|
// 🔥 核心修复1:解析到 sseClientId 时即时处理
|
|
172
179
|
if (eventType === 'sseClientId') {
|
|
173
180
|
data = data.replace(/"/g,'');
|
|
174
|
-
|
|
181
|
+
if(getIsDebug()) {
|
|
182
|
+
console.log(`[SSE] [${this.moduleCode}] 获取到 sseClientId:`, data);
|
|
183
|
+
}
|
|
175
184
|
setLocalStorageItem(getSseClientIdKey(this.moduleCode), data, 60 * 30)
|
|
176
185
|
// 即时触发回调,同步到 Hook 的响应式变量
|
|
177
186
|
this.onSseClientIdUpdate?.(data);
|
|
178
187
|
return;
|
|
179
188
|
}
|
|
180
189
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
if (eventType === 'sseMessage') {
|
|
191
|
+
// 生成标准 MessageEvent
|
|
192
|
+
const messageEvent = new MessageEvent(eventType, {
|
|
193
|
+
data,
|
|
194
|
+
lastEventId: messageId,
|
|
195
|
+
origin: new URL(this.url).origin,
|
|
196
|
+
source: null,
|
|
197
|
+
ports: []
|
|
198
|
+
});
|
|
199
|
+
this.messageHandler(data, messageEvent);
|
|
200
|
+
}
|
|
191
201
|
}
|
|
192
202
|
|
|
193
203
|
private handleReconnectSse(): void {
|
|
@@ -202,7 +212,9 @@ export class FrameworkSseClient {
|
|
|
202
212
|
}
|
|
203
213
|
|
|
204
214
|
this.reconnectAttempts++;
|
|
205
|
-
|
|
215
|
+
if(getIsDebug()) {
|
|
216
|
+
console.log(`[SSE] 准备第 ${this.reconnectAttempts} 次重连,间隔 ${this.reconnectInterval}ms`);
|
|
217
|
+
}
|
|
206
218
|
this.reconnectTimer = window.setTimeout(() => {
|
|
207
219
|
this.connectSse();
|
|
208
220
|
this.reconnectTimer = null;
|
|
@@ -223,13 +235,17 @@ export class FrameworkSseClient {
|
|
|
223
235
|
close(): void {
|
|
224
236
|
this.isManualClose = true;
|
|
225
237
|
this.cleanup();
|
|
226
|
-
|
|
238
|
+
if(getIsDebug()) {
|
|
239
|
+
console.log(`[SSE] [${this.moduleCode}] 连接已手动关闭`);
|
|
240
|
+
}
|
|
227
241
|
}
|
|
228
242
|
|
|
229
243
|
reconnectSse(): void {
|
|
230
244
|
this.isManualClose = false;
|
|
231
245
|
this.connectSse();
|
|
232
|
-
|
|
246
|
+
if(getIsDebug()) {
|
|
247
|
+
console.log(`[SSE] [${this.moduleCode}] 触发手动重连`);
|
|
248
|
+
}
|
|
233
249
|
}
|
|
234
250
|
|
|
235
251
|
isSseConnected(): boolean {
|
|
@@ -284,7 +300,9 @@ export function useSse(
|
|
|
284
300
|
|
|
285
301
|
// 消息处理回调(仅处理业务消息)
|
|
286
302
|
const messageHandler: SseMessageHandler = (data, event) => {
|
|
287
|
-
|
|
303
|
+
if(getIsDebug()) {
|
|
304
|
+
console.log(`[SSE Hook] [${moduleCode}] 接收消息:`, data, event);
|
|
305
|
+
}
|
|
288
306
|
// 业务消息才加入列表,避免 clientId 事件混入
|
|
289
307
|
if (event?.type !== 'sseClientId') {
|
|
290
308
|
sseMessageList.value.push(data);
|
|
@@ -295,7 +313,9 @@ export function useSse(
|
|
|
295
313
|
// 连接成功回调
|
|
296
314
|
const openHandler: SseOpenHandler = () => {
|
|
297
315
|
isSseConnected.value = true;
|
|
298
|
-
|
|
316
|
+
if(getIsDebug()) {
|
|
317
|
+
console.log(`[${moduleCode}] SSE 连接成功`);
|
|
318
|
+
}
|
|
299
319
|
};
|
|
300
320
|
|
|
301
321
|
// 错误处理回调
|
|
@@ -307,7 +327,9 @@ export function useSse(
|
|
|
307
327
|
|
|
308
328
|
// 🔥 核心修复3:clientId 更新回调(即时同步响应式变量)
|
|
309
329
|
const onSseClientIdUpdate = (clientId: string) => {
|
|
310
|
-
|
|
330
|
+
if(getIsDebug()) {
|
|
331
|
+
console.log(`[SSE Hook] [${moduleCode}] 同步 sseClientId:`, clientId);
|
|
332
|
+
}
|
|
311
333
|
sseClientId.value = clientId; // 即时更新响应式变量
|
|
312
334
|
};
|
|
313
335
|
|
|
@@ -380,7 +402,9 @@ export function useSse(
|
|
|
380
402
|
sseClient.close();
|
|
381
403
|
sseClient = null;
|
|
382
404
|
isSseConnected.value = false;
|
|
383
|
-
|
|
405
|
+
if(getIsDebug()) {
|
|
406
|
+
console.log(`[${moduleCode}] SSE 连接已断开`);
|
|
407
|
+
}
|
|
384
408
|
}
|
|
385
409
|
};
|
|
386
410
|
|
|
@@ -390,7 +414,9 @@ export function useSse(
|
|
|
390
414
|
const reconnectSse = () => {
|
|
391
415
|
if (sseClient) {
|
|
392
416
|
sseClient.reconnectSse();
|
|
393
|
-
|
|
417
|
+
if(getIsDebug()) {
|
|
418
|
+
console.log(`[${moduleCode}] 已触发 SSE 手动重连`);
|
|
419
|
+
}
|
|
394
420
|
} else {
|
|
395
421
|
console.warn(`[${moduleCode}] 暂无有效 SSE 连接,请先建立连接`);
|
|
396
422
|
}
|