-
Notifications
You must be signed in to change notification settings - Fork 28
/
debounce.ts
35 lines (32 loc) · 916 Bytes
/
debounce.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
import type { λ } from "./types.ts";
export const cancel = Symbol("debounce.cancel");
/**
* Creates a debounced function that delays invoking `fun` until `ms` milliseconds
* have passed since the last invocation of the debounced function.
*
* `fun` is invoked with the last arguments passed to the debounced function.
*
* Calling `[debounce.cancel]()` on the debounced function will cancel the currently
* scheduled invocation.
*/
const debounce = Object.assign(
(fun: λ, ms: number) => {
let toId: any;
return Object.assign(
(...args: any[]) => {
clearTimeout(toId);
toId = setTimeout(() => fun(...args), ms);
},
{ [cancel]: () => clearTimeout(toId) },
) as any;
},
{ cancel },
) as
& (<T extends λ>(
fun: T,
ms: number,
) => λ<Parameters<T>, void> & { [cancel](): void })
& {
cancel: typeof cancel;
};
export default debounce;