Skip to content

Commit 40ea0c1

Browse files
committed
remove cleanup function array
1 parent bfac2a3 commit 40ea0c1

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/whenIdle.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,20 @@ function HydrateOnIdle({ children, ...rest }: Props) {
1010
React.useEffect(() => {
1111
if (hydrated) return;
1212

13-
const cleanupFns: VoidFunction[] = [];
14-
15-
function cleanup() {
16-
for (let i = 0; i < cleanupFns.length; i++) {
17-
cleanupFns[i]();
18-
}
19-
}
20-
2113
// @ts-ignore
2214
if (requestIdleCallback) {
2315
// @ts-ignore
2416
const idleCallbackId = requestIdleCallback(hydrate, { timeout: 500 });
25-
cleanupFns.push(() => {
17+
return () => {
2618
// @ts-ignore
2719
cancelIdleCallback(idleCallbackId);
28-
});
20+
};
2921
} else {
3022
const id = setTimeout(hydrate, 2000);
31-
cleanupFns.push(() => {
23+
return () => {
3224
clearTimeout(id);
33-
});
25+
};
3426
}
35-
36-
return cleanup;
3727
}, [hydrated, hydrate]);
3828

3929
if (hydrated) {

0 commit comments

Comments
 (0)