@kawaiininja/fetch 1.0.21 → 1.0.22
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/hooks/useFetch.js +25 -7
- package/package.json +1 -1
package/dist/hooks/useFetch.js
CHANGED
|
@@ -15,14 +15,21 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
15
15
|
const abortRef = useRef(new AbortController());
|
|
16
16
|
const mounted = useRef(true);
|
|
17
17
|
const isRequestingRef = useRef(false);
|
|
18
|
-
// 🛡️ STABILITY:
|
|
19
|
-
// We use a Ref to store the options so they don't trigger the request callback recreation
|
|
18
|
+
// 🛡️ STABILITY: Options hashing to prevent unnecessary effect triggers from object literals
|
|
20
19
|
const optionsRef = useRef(baseOptions);
|
|
20
|
+
const optionsHash = useMemo(() => {
|
|
21
|
+
try {
|
|
22
|
+
return JSON.stringify(baseOptions);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return Math.random();
|
|
26
|
+
}
|
|
27
|
+
}, [baseOptions]);
|
|
21
28
|
useEffect(() => {
|
|
22
29
|
if (debug)
|
|
23
|
-
console.log(`[useFetch] [${endpoint}]
|
|
30
|
+
console.log(`[useFetch] [${endpoint}] ⚙️ baseOptions updated`);
|
|
24
31
|
optionsRef.current = baseOptions;
|
|
25
|
-
}, [
|
|
32
|
+
}, [optionsHash, endpoint, debug]);
|
|
26
33
|
if (debug)
|
|
27
34
|
console.log(`[useFetch] [${endpoint}] 🔄 Hook Render`);
|
|
28
35
|
const [state, setState] = useState({
|
|
@@ -31,20 +38,31 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
31
38
|
error: null,
|
|
32
39
|
status: null,
|
|
33
40
|
});
|
|
41
|
+
// 🛡️ RECURSION GUARD: Prevent synchronous state-update cascades
|
|
42
|
+
const isUpdatingRef = useRef(false);
|
|
34
43
|
const safeSet = useCallback((fn) => {
|
|
35
|
-
if (mounted.current)
|
|
44
|
+
if (!mounted.current)
|
|
45
|
+
return;
|
|
46
|
+
if (isUpdatingRef.current)
|
|
47
|
+
return; // Block synchronous recursion
|
|
48
|
+
isUpdatingRef.current = true;
|
|
49
|
+
try {
|
|
36
50
|
setState((prev) => {
|
|
37
51
|
const updates = fn(prev);
|
|
38
52
|
if (debug) {
|
|
39
53
|
console.groupCollapsed(`[useFetch] [${endpoint}] 🔄 State Update`);
|
|
40
|
-
console.log("Current State:", prev);
|
|
41
54
|
console.log("Updates:", updates);
|
|
42
|
-
console.trace("Stack Trace");
|
|
43
55
|
console.groupEnd();
|
|
44
56
|
}
|
|
45
57
|
return { ...prev, ...updates };
|
|
46
58
|
});
|
|
47
59
|
}
|
|
60
|
+
finally {
|
|
61
|
+
// Use a microtask to release the lock after the current execution frame
|
|
62
|
+
Promise.resolve().then(() => {
|
|
63
|
+
isUpdatingRef.current = false;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
48
66
|
}, [endpoint, debug]);
|
|
49
67
|
const request = useCallback(async (params = {}) => {
|
|
50
68
|
// 🛡️ RECURSION GUARD: Instant rejection of concurrent loops
|