Skip to content

Commit a05e8e4

Browse files
committed
[FIX] usePromiseSuspensible invalidate function immutable
1 parent 229739c commit a05e8e4

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

packages/react-tools-lib/src/hooks/api-dom/usePromiseSuspensible.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DependencyList, useReducer } from "react";
1+
import { DependencyList, useReducer, useRef } from "react";
22
import { useEffectOnce } from "../lifecycle";
33
import { isDeepEqual } from "../../utils";
44

@@ -21,6 +21,11 @@ function usePromiseSuspensible<T>(promise: () => Promise<T>, deps: DependencyLis
2121
function usePromiseSuspensible<T>(promise: () => Promise<T>, deps: DependencyList, options: { cache?: "unmount" | number, cleanOnError?: boolean, identifier?: string, invalidateManually?: boolean } = {}): Awaited<ReturnType<typeof promise>> | [Awaited<ReturnType<typeof promise>>, () => void] {
2222
let index = -1;
2323
const [, reRender] = useReducer(t => t + 0.00000000001, 0);
24+
const invalidate = useRef(() => {
25+
index !== -1 && promiseCache.splice(index, 1);
26+
index = -1;
27+
reRender();
28+
});
2429

2530
useEffectOnce(() => () => {
2631
if (options.cache === "unmount") {
@@ -48,11 +53,7 @@ function usePromiseSuspensible<T>(promise: () => Promise<T>, deps: DependencyLis
4853
if (options.invalidateManually) {
4954
return [
5055
promiseCache[ind].response,
51-
() => {
52-
index !== -1 && promiseCache.splice(index, 1);
53-
index = -1;
54-
reRender();
55-
}
56+
invalidate.current
5657
] as [Awaited<ReturnType<typeof promise>>, () => void]
5758
}
5859
return promiseCache[ind].response as Awaited<ReturnType<typeof promise>>;

0 commit comments

Comments
 (0)