Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {useEffect, useRef, useState} from 'react'
2
3import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
4
5export function useThrottledValue<T>(value: T, time: number) {
6 const pendingValueRef = useRef(value)
7 const [throttledValue, setThrottledValue] = useState(value)
8
9 useEffect(() => {
10 pendingValueRef.current = value
11 }, [value])
12
13 const handleTick = useNonReactiveCallback(() => {
14 if (pendingValueRef.current !== throttledValue) {
15 setThrottledValue(pendingValueRef.current)
16 }
17 })
18
19 useEffect(() => {
20 const id = setInterval(handleTick, time)
21 return () => {
22 clearInterval(id)
23 }
24 }, [handleTick, time])
25
26 return throttledValue
27}