-
Notifications
You must be signed in to change notification settings - Fork 28
/
throttle.ts
51 lines (45 loc) · 1.36 KB
/
throttle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import type { λ } from "./types.ts";
const { performance } = globalThis;
export const cancel = Symbol("throttle.cancel");
/**
* Create a throttled function that invokes `fun` at most every `ms` milliseconds.
*
* `fun` is invoked with the last arguments passed to the throttled function.
*
* Calling `[throttle.cancel]()` on the throttled function will cancel the currently
* scheduled invocation.
*/
const throttle = Object.assign(
(fun: λ, ms: number, { leading = true, trailing = true } = {}) => {
let toId: any;
let lastInvoke = -Infinity;
let lastArgs: any[] | undefined;
const invoke = () => {
lastInvoke = performance.now();
toId = undefined;
fun(...lastArgs!);
};
return Object.assign(
(...args: any[]) => {
if (!leading && !trailing) return;
lastArgs = args;
const dt = performance.now() - lastInvoke;
if (dt >= ms && toId === undefined && leading) invoke();
else if (toId === undefined && trailing) {
toId = setTimeout(invoke, dt >= ms ? ms : ms - dt);
}
},
{ [cancel]: () => clearTimeout(toId) },
);
},
{ cancel },
) as
& (<T extends λ>(
fun: T,
ms: number,
opts?: { leading?: boolean; trailing: boolean },
) => λ<Parameters<T>, void> & { [cancel](): void })
& {
cancel: typeof cancel;
};
export default throttle;