MCPcopy Index your code
hub / github.com/darkreader/darkreader / throttle

Function throttle

src/utils/throttle.ts:1–30  ·  view source on GitHub ↗
(callback: T)

Source from the content-addressed store, hash-verified

1export function throttle<T extends(...args: any[]) => any>(callback: T): T & {cancel: () => void} {
2 let pending = false;
3 let frameId: number | null = null;
4 let lastArgs: any[];
5
6 const throttled: T = ((...args: any[]) => {
7 lastArgs = args;
8 if (frameId) {
9 pending = true;
10 } else {
11 callback(...lastArgs);
12 frameId = requestAnimationFrame(() => {
13 frameId = null;
14 if (pending) {
15 callback(...lastArgs);
16 pending = false;
17 }
18 });
19 }
20 }) as any;
21
22 const cancel = () => {
23 // TODO: resolve cast once types are updated
24 cancelAnimationFrame(frameId!);
25 pending = false;
26 frameId = null;
27 };
28
29 return Object.assign(throttled, {cancel});
30}
31
32type Task = () => void;
33

Callers 4

watchForNodePositionFunction · 0.90
index.tsFile · 0.90
deepWatchForInlineStylesFunction · 0.90
SliderFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected