mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-20 20:46:15 +08:00
impr: add debounced animation frame util
!nuf
This commit is contained in:
parent
1479a26927
commit
02e92d0f3b
1 changed files with 21 additions and 0 deletions
21
frontend/src/ts/utils/debounced-animation-frame.ts
Normal file
21
frontend/src/ts/utils/debounced-animation-frame.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
const pendingFrames = new Map<string, number>();
|
||||
|
||||
export function requestDebouncedAnimationFrame(
|
||||
frameId: string,
|
||||
callback: () => void
|
||||
): void {
|
||||
cancelIfPending(frameId);
|
||||
const frame = requestAnimationFrame(() => {
|
||||
pendingFrames.delete(frameId);
|
||||
callback();
|
||||
});
|
||||
pendingFrames.set(frameId, frame);
|
||||
}
|
||||
|
||||
function cancelIfPending(frameId: string): void {
|
||||
const pending = pendingFrames.get(frameId);
|
||||
if (pending !== undefined) {
|
||||
cancelAnimationFrame(pending);
|
||||
pendingFrames.delete(frameId);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue