mirror of
https://github.com/status-im/wakuconnect-chat-sdk.git
synced 2025-01-13 13:45:34 +00:00
a5fdd22d51
* feat: add blurview to topbar and composer with scroll position * fix: icon button component and adds scrolls var position for the top bar * fix: changes from review
39 lines
668 B
TypeScript
39 lines
668 B
TypeScript
import { useMemo } from 'react'
|
|
|
|
const throttle = <Args extends unknown[]>(
|
|
fn: (...args: Args) => void,
|
|
cooldown: number
|
|
) => {
|
|
let lastArgs: Args | undefined
|
|
|
|
const run = () => {
|
|
if (lastArgs) {
|
|
fn(...lastArgs)
|
|
lastArgs = undefined
|
|
}
|
|
}
|
|
|
|
const throttled = (...args: Args) => {
|
|
const isOnCooldown = !!lastArgs
|
|
|
|
lastArgs = args
|
|
|
|
if (isOnCooldown) {
|
|
return
|
|
}
|
|
|
|
window.setTimeout(run, cooldown)
|
|
}
|
|
|
|
return throttled
|
|
}
|
|
|
|
const useThrottle = <Args extends unknown[]>(
|
|
cb: (...args: Args) => void,
|
|
cooldown: number
|
|
) => {
|
|
return useMemo(() => throttle(cb, cooldown), [cb, cooldown])
|
|
}
|
|
|
|
export { useThrottle }
|