Skip to content

Commit 02e92d0

Browse files
committed
impr: add debounced animation frame util
!nuf
1 parent 1479a26 commit 02e92d0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const pendingFrames = new Map<string, number>();
2+
3+
export function requestDebouncedAnimationFrame(
4+
frameId: string,
5+
callback: () => void
6+
): void {
7+
cancelIfPending(frameId);
8+
const frame = requestAnimationFrame(() => {
9+
pendingFrames.delete(frameId);
10+
callback();
11+
});
12+
pendingFrames.set(frameId, frame);
13+
}
14+
15+
function cancelIfPending(frameId: string): void {
16+
const pending = pendingFrames.get(frameId);
17+
if (pending !== undefined) {
18+
cancelAnimationFrame(pending);
19+
pendingFrames.delete(frameId);
20+
}
21+
}

0 commit comments

Comments
 (0)