Skip to content

Commit 02d82dc

Browse files
committed
[FIX] useRequestIdleCallback hook
1 parent 24cdad6 commit 02d82dc

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

apps/react-tools-demo/src/markdown/useRequestIdleCallback.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ export const UseRequestIdleCallback = () => {
88
const [iterations, setIterations] = useState(0);
99
const [log, setLog] = useState("");
1010
const [invoke] = useRequestIdleCallback(() => setLog("RequestIdleCallback executed"));
11+
12+
const start = async() => {
1113
invoke();
12-
const start = () => {
13-
invoke();
14-
for (let i = 0; i < 1_000_000; i++) {
15-
setIterations(i);
14+
for (let i = 0; i < 5_000; i++) {
15+
setTimeout(() => setIterations(i + 1), 5);
1616
}
1717
}
18+
const reset = () => {
19+
setLog("");
20+
setIterations(0);
21+
}
1822
return (<div>
1923
<p>Iterations are: {iterations}</p>
2024
<p>Log is: {log}</p>
2125
<button onClick={start}>START</button>
26+
<button onClick={reset}>RESET</button>
2227
</div>);
2328
}
2429
```
@@ -27,13 +32,13 @@ export const UseRequestIdleCallback = () => {
2732
> - a __iterations__ variable of type string.
2833
> - a __log__ variable of type string.
2934
> - a function __invoke__ returned from _useRequestIdleCallback_ hook, initialized with a cb that update __log__ variable with message _"RequestIdleCallback executed"_.
30-
> - a button start that when clicked executes __start__ function that executes __invoke__ function and updates __iterations__ variable inside a loop of 1 million iterations with iteration index.
35+
> - a button start that when clicked executes __start__ function that executes __invoke__ function and updates __iterations__ variable inside a loop with iteration index.
3136
3237

3338
## API
3439

3540
```tsx
36-
useRequestIdleCallback (cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void)=> void, opts?: IdleRequestOptions & { unsupportedBehavior: "animationFrame" | "timeout" | "immediatly" }): [() => void, () => void]
41+
useRequestIdleCallback (cb: (deadline?: IdleDeadline | DOMHighResTimeStamp | void) => void, opts?: {timeout: number , unsupportedBehavior?: "animationFrame" | "timeout" | "immediatly" }): [() => void, () => void]
3742
```
3843

3944
> ### Params

packages/react-tools/src/hooks/useRequestIdleCallback.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export const useRequestIdleCallback = (cb: (deadline?: IdleDeadline | DOMHighRes
1212

1313
const invoke = useRef(
1414
requestIdleCallback !== undefined
15-
? requestIdleCallback
15+
? (fn: typeof cb, opt: {timeout?: number})=> requestIdleCallback(cb, opt)
1616
: unsupportedBehavior
1717
? unsupportedBehavior === "immediatly"
1818
? undefined
1919
: unsupportedBehavior === "animationFrame"
20-
? requestAnimationFrame
21-
: setTimeout
20+
? (fn: typeof cb) =>requestAnimationFrame(fn)
21+
: (fn: typeof cb, opt: { timeout?: number }) =>setTimeout(()=>cb, opt.timeout)
2222
: () => { }
2323
);
2424
const cancel = useRef(
@@ -36,24 +36,11 @@ export const useRequestIdleCallback = (cb: (deadline?: IdleDeadline | DOMHighRes
3636
return [
3737
useCallback(() => {
3838
let id;
39-
if (requestIdleCallback !== undefined) {
40-
id = requestIdleCallback(cb, { timeout: timeout });
41-
} else {
42-
if (unsupportedBehavior) {
43-
switch (unsupportedBehavior) {
44-
case "animationFrame":
45-
id = requestAnimationFrame(cb);
46-
break;
47-
case "immediatly":
48-
cb();
49-
break;
50-
case "timeout":
51-
id = setTimeout(cb, timeout);
52-
}
53-
}
39+
if (invoke.current) {
40+
id = invoke.current(cb, { timeout: timeout });
5441
}
5542
idRequest.current = id ? id as unknown as number : undefined;
56-
}, [cb, timeout, unsupportedBehavior]),
43+
}, [cb, timeout]),
5744
useCallback(() => idRequest.current && cancel.current && cancel.current(idRequest.current), [])
5845
];
5946
}

0 commit comments

Comments
 (0)