|
1 | | -import { useCallback, useEffect, useMemo, useRef } from "react"; |
2 | | - |
3 | | -export function createThrottledTrigger({ |
4 | | - callback, |
5 | | - throttleMs, |
6 | | - now = Date.now, |
7 | | - setTimer = setTimeout, |
8 | | - clearTimer = clearTimeout, |
9 | | -}) { |
10 | | - const state = { |
11 | | - timer: null, |
12 | | - lastRunAt: null, |
13 | | - }; |
14 | | - |
15 | | - const invoke = () => { |
16 | | - state.lastRunAt = now(); |
17 | | - callback?.(); |
18 | | - }; |
19 | | - |
20 | | - const schedule = (waitMs) => { |
21 | | - state.timer = setTimer(() => { |
22 | | - state.timer = null; |
23 | | - invoke(); |
24 | | - }, waitMs); |
25 | | - }; |
26 | | - |
27 | | - const runNow = () => { |
28 | | - if (state.timer != null) { |
29 | | - clearTimer(state.timer); |
30 | | - state.timer = null; |
31 | | - } |
32 | | - invoke(); |
33 | | - }; |
34 | | - |
35 | | - const trigger = () => { |
36 | | - if (!Number.isFinite(throttleMs) || throttleMs <= 0) { |
37 | | - invoke(); |
38 | | - return; |
39 | | - } |
40 | | - |
41 | | - if (state.timer != null) return; |
42 | | - |
43 | | - if (state.lastRunAt == null) { |
44 | | - invoke(); |
45 | | - return; |
46 | | - } |
47 | | - |
48 | | - const elapsed = now() - state.lastRunAt; |
49 | | - if (elapsed >= throttleMs) { |
50 | | - invoke(); |
51 | | - return; |
52 | | - } |
53 | | - |
54 | | - schedule(throttleMs - elapsed); |
55 | | - }; |
56 | | - |
57 | | - const dispose = () => { |
58 | | - if (state.timer != null) { |
59 | | - clearTimer(state.timer); |
60 | | - state.timer = null; |
61 | | - } |
62 | | - }; |
63 | | - |
64 | | - return { trigger, runNow, dispose }; |
65 | | -} |
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 2 | +import useThrottleFn from "react-use/esm/useThrottleFn.js"; |
66 | 3 |
|
67 | 4 | export function useThrottledTrigger({ callback, throttleMs = 150 }) { |
68 | 5 | const callbackRef = useRef(callback); |
| 6 | + const skipNextThrottledCallRef = useRef(false); |
| 7 | + const [triggerVersion, setTriggerVersion] = useState(0); |
69 | 8 |
|
70 | 9 | useEffect(() => { |
71 | 10 | callbackRef.current = callback; |
72 | 11 | }, [callback]); |
73 | 12 |
|
74 | | - const controller = useMemo( |
75 | | - () => |
76 | | - createThrottledTrigger({ |
77 | | - callback: () => callbackRef.current?.(), |
78 | | - throttleMs, |
79 | | - }), |
80 | | - [throttleMs], |
| 13 | + useThrottleFn( |
| 14 | + () => { |
| 15 | + if (skipNextThrottledCallRef.current) { |
| 16 | + skipNextThrottledCallRef.current = false; |
| 17 | + return; |
| 18 | + } |
| 19 | + callbackRef.current?.(); |
| 20 | + }, |
| 21 | + throttleMs, |
| 22 | + [triggerVersion], |
81 | 23 | ); |
82 | 24 |
|
83 | | - useEffect(() => () => controller.dispose(), [controller]); |
84 | | - |
85 | 25 | const trigger = useCallback(() => { |
86 | | - controller.trigger(); |
87 | | - }, [controller]); |
| 26 | + if (!Number.isFinite(throttleMs) || throttleMs <= 0) { |
| 27 | + callbackRef.current?.(); |
| 28 | + return; |
| 29 | + } |
| 30 | + setTriggerVersion((version) => version + 1); |
| 31 | + }, [throttleMs]); |
88 | 32 |
|
89 | 33 | const runNow = useCallback(() => { |
90 | | - controller.runNow(); |
91 | | - }, [controller]); |
| 34 | + if (!Number.isFinite(throttleMs) || throttleMs <= 0) { |
| 35 | + callbackRef.current?.(); |
| 36 | + return; |
| 37 | + } |
| 38 | + skipNextThrottledCallRef.current = true; |
| 39 | + callbackRef.current?.(); |
| 40 | + setTriggerVersion((version) => version + 1); |
| 41 | + }, [throttleMs]); |
92 | 42 |
|
93 | 43 | return { trigger, runNow }; |
94 | 44 | } |
|
0 commit comments