xxf_react 0.1.5 → 0.1.6
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/sse/UseSSE.d.ts.map +1 -1
- package/dist/sse/UseSSE.js +17 -35
- package/package.json +1 -1
package/dist/sse/UseSSE.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UseSSE.d.ts","sourceRoot":"","sources":["../../src/sse/UseSSE.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"UseSSE.d.ts","sourceRoot":"","sources":["../../src/sse/UseSSE.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,kBAAkB,EAAC,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAItC,KAAK,YAAY,GAAG;IAChB,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACnE,SAAS,EAAE,CAAC,KAAK,EAAE,kBAAkB,KAAK,IAAI,CAAC;IAC/C,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,wBAAgB,MAAM,CAAC,EACI,GAAG,EACH,OAAO,EACP,SAAS,EACT,OAAc,GACjB,EAAE,YAAY;;EA2CrC"}
|
package/dist/sse/UseSSE.js
CHANGED
|
@@ -6,52 +6,34 @@ export function useSSE({ url, headers, onMessage, enabled = true, }) {
|
|
|
6
6
|
const callbackRef = useRef(onMessage);
|
|
7
7
|
callbackRef.current = onMessage;
|
|
8
8
|
const [status, setStatus] = useState(SSEStatus.idle);
|
|
9
|
-
// ---------- Step 1: resolve async url and headers ----------
|
|
10
|
-
const [resolvedUrl, setResolvedUrl] = useState(null);
|
|
11
|
-
const [resolvedHeaders, setResolvedHeaders] = useState({});
|
|
12
9
|
useEffect(() => {
|
|
13
10
|
if (!enabled)
|
|
14
11
|
return;
|
|
15
|
-
let canceled = false;
|
|
16
|
-
(async () => {
|
|
17
|
-
try {
|
|
18
|
-
const u = url instanceof Promise ? await url : url || null;
|
|
19
|
-
const h = headers instanceof Promise ? await headers : headers || {};
|
|
20
|
-
if (!canceled) {
|
|
21
|
-
setResolvedUrl(u);
|
|
22
|
-
setResolvedHeaders(h);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
console.error("useSSE resolve error:", err);
|
|
27
|
-
}
|
|
28
|
-
})();
|
|
29
|
-
return () => {
|
|
30
|
-
canceled = true;
|
|
31
|
-
};
|
|
32
|
-
}, [url, headers, enabled]);
|
|
33
|
-
// ---------- Step 2: create SSEManager ----------
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
if (!enabled || !resolvedUrl)
|
|
36
|
-
return;
|
|
37
|
-
let canceled = false;
|
|
12
|
+
let canceled = false; // 用于处理 cleanup
|
|
38
13
|
let manager = null;
|
|
39
14
|
let unsubscribe = null;
|
|
40
15
|
let stateUnsubscribe = null;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
16
|
+
async function setupSSE() {
|
|
17
|
+
// 先解析异步 url 和 headers
|
|
18
|
+
const resolvedUrl = typeof url === "function" || url instanceof Promise ? await url : url;
|
|
19
|
+
if (!resolvedUrl)
|
|
20
|
+
return;
|
|
21
|
+
const resolvedHeaders = headers instanceof Promise ? await headers : headers || {};
|
|
22
|
+
if (canceled)
|
|
23
|
+
return; // 组件卸载或依赖变化时直接返回
|
|
24
|
+
manager = sseRegistry.getOrCreate(resolvedUrl, () => new SSEManager(resolvedUrl, resolvedHeaders));
|
|
25
|
+
unsubscribe = manager.subscribe((event) => {
|
|
44
26
|
callbackRef.current(event);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
27
|
+
});
|
|
28
|
+
stateUnsubscribe = manager.subscribeStatus((s) => setStatus(s));
|
|
29
|
+
}
|
|
30
|
+
setupSSE().catch(console.error);
|
|
50
31
|
return () => {
|
|
51
32
|
canceled = true;
|
|
52
33
|
unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
|
|
53
34
|
stateUnsubscribe === null || stateUnsubscribe === void 0 ? void 0 : stateUnsubscribe();
|
|
54
35
|
};
|
|
55
|
-
|
|
36
|
+
//}, [url, headers, enabled]);
|
|
37
|
+
}, [url, enabled]);
|
|
56
38
|
return { status };
|
|
57
39
|
}
|