xxf_react 0.1.5 → 0.1.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/dist/fetch/Fetch.d.ts +9 -0
- package/dist/fetch/Fetch.d.ts.map +1 -0
- package/dist/fetch/Fetch.js +49 -0
- package/dist/fetch/index.d.ts +2 -0
- package/dist/fetch/index.d.ts.map +1 -0
- package/dist/fetch/index.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/sse/UseSSE.d.ts.map +1 -1
- package/dist/sse/UseSSE.js +17 -35
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Fetch.d.ts","sourceRoot":"","sources":["../../src/fetch/Fetch.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAsB,gBAAgB,CAClC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,GAAE,CAAC,WAAW,GAAG;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAAM,GAChD,OAAO,CAAC,QAAQ,CAAC,CAkDnB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 带超时时间的请求,默认没有超时时间限制
|
|
3
|
+
* @param input
|
|
4
|
+
* @param init
|
|
5
|
+
*/
|
|
6
|
+
export async function fetchWithTimeout(input, init = {}) {
|
|
7
|
+
const { timeout, signal, ...rest } = init;
|
|
8
|
+
if (timeout == null) {
|
|
9
|
+
return fetch(input, init);
|
|
10
|
+
}
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
let timeoutFired = false;
|
|
13
|
+
const onAbort = () => controller.abort();
|
|
14
|
+
if (signal) {
|
|
15
|
+
if (signal.aborted) {
|
|
16
|
+
controller.abort();
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const timer = setTimeout(() => {
|
|
23
|
+
timeoutFired = true;
|
|
24
|
+
controller.abort();
|
|
25
|
+
}, timeout);
|
|
26
|
+
try {
|
|
27
|
+
return await fetch(input, {
|
|
28
|
+
...rest,
|
|
29
|
+
signal: controller.signal,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (e.name === 'AbortError' && timeoutFired) {
|
|
34
|
+
const url = typeof input === 'string'
|
|
35
|
+
? input
|
|
36
|
+
: input instanceof Request
|
|
37
|
+
? input.url
|
|
38
|
+
: input.toString();
|
|
39
|
+
console.error(`[http timeout] ${url} (${timeout}ms)`);
|
|
40
|
+
}
|
|
41
|
+
throw e;
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
clearTimeout(timer);
|
|
45
|
+
if (signal) {
|
|
46
|
+
signal.removeEventListener('abort', onAbort);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Fetch';
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
|
package/dist/index.js
CHANGED
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
|
}
|