Created
October 1, 2023 09:04
-
-
Save 8mist/a9f0d6e83d3c7e7dc10749634705a732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debounceToNextFrame = <F extends (...args: any[]) => void>( | |
fn: F, | |
): ((...funcArgs: Parameters<F>) => void) & { | |
cancel: () => void; | |
} => { | |
let frameReference: number; | |
const cancel = (): void => { | |
cancelAnimationFrame(frameReference); | |
}; | |
return Object.assign( | |
(...args: Parameters<F>): void => { | |
cancel(); | |
frameReference = requestAnimationFrame((): void => fn(...args)); | |
}, | |
{ cancel }, | |
); | |
}; | |
export default debounceToNextFrame; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment