|
| 1 | +import { useRef, SyntheticEvent } from 'react' |
| 2 | +import useMounted from './useMounted' |
| 3 | +import useEventCallback from './useEventCallback' |
| 4 | + |
| 5 | +const isSyntheticEvent = (event: any): event is SyntheticEvent => |
| 6 | + typeof event.persist === 'function' |
| 7 | + |
| 8 | +export type ThrottledHandler<TEvent> = ((event: TEvent) => void) & { |
| 9 | + clear(): void |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Creates a event handler function throttled by `requestAnimationFrame` that |
| 14 | + * returns the **most recent** event. Useful for noisy events that update react state. |
| 15 | + * |
| 16 | + * ```tsx |
| 17 | + * function Component() { |
| 18 | + * const [position, setPosition] = useState(); |
| 19 | + * const handleMove = useThrottledEventHandler<React.PointerEvent>( |
| 20 | + * (event) => { |
| 21 | + * setPosition({ |
| 22 | + * top: event.clientX, |
| 23 | + * left: event.clientY, |
| 24 | + * }) |
| 25 | + * } |
| 26 | + * ) |
| 27 | + * |
| 28 | + * return ( |
| 29 | + * <div onPointerMove={handleMove}> |
| 30 | + * <div style={position} /> |
| 31 | + * </div> |
| 32 | + * ); |
| 33 | + * } |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * @param handler An event handler function |
| 37 | + * @typeParam TEvent The event object passed to the handler function |
| 38 | + * @returns The event handler with a `clear` method attached for clearing any in-flight handler calls |
| 39 | + * |
| 40 | + */ |
| 41 | +export default function useThrottledEventHandler<TEvent = SyntheticEvent>( |
| 42 | + handler: (event: TEvent) => void, |
| 43 | +): ThrottledHandler<TEvent> { |
| 44 | + const isMounted = useMounted() |
| 45 | + const eventHandler = useEventCallback(handler) |
| 46 | + |
| 47 | + const nextEventInfoRef = useRef<{ |
| 48 | + event: TEvent | null |
| 49 | + handle: null | number |
| 50 | + }>({ |
| 51 | + event: null, |
| 52 | + handle: null, |
| 53 | + }) |
| 54 | + |
| 55 | + const clear = () => { |
| 56 | + cancelAnimationFrame(nextEventInfoRef.current.handle!) |
| 57 | + nextEventInfoRef.current.handle = null |
| 58 | + } |
| 59 | + |
| 60 | + const handlePointerMoveAnimation = () => { |
| 61 | + const { current: next } = nextEventInfoRef |
| 62 | + |
| 63 | + if (next.handle && next.event) { |
| 64 | + if (isMounted()) { |
| 65 | + next.handle = null |
| 66 | + eventHandler(next.event) |
| 67 | + } |
| 68 | + } |
| 69 | + next.event = null |
| 70 | + } |
| 71 | + |
| 72 | + const throttledHandler = (event: TEvent) => { |
| 73 | + if (!isMounted()) return |
| 74 | + |
| 75 | + if (isSyntheticEvent(event)) { |
| 76 | + event.persist() |
| 77 | + } |
| 78 | + // Special handling for a React.Konva event which reuses the |
| 79 | + // event object as it bubbles, setting target |
| 80 | + else if ('evt' in event) { |
| 81 | + event = { ...event } |
| 82 | + } |
| 83 | + |
| 84 | + nextEventInfoRef.current.event = event |
| 85 | + if (!nextEventInfoRef.current.handle) { |
| 86 | + nextEventInfoRef.current.handle = requestAnimationFrame( |
| 87 | + handlePointerMoveAnimation, |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + throttledHandler.clear = clear |
| 93 | + |
| 94 | + return throttledHandler |
| 95 | +} |
0 commit comments