-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.d.ts
More file actions
79 lines (60 loc) · 2.26 KB
/
index.d.ts
File metadata and controls
79 lines (60 loc) · 2.26 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
export type Options = {
/**
Time in milliseconds to wait until the `input` function is called.
@default 0
*/
readonly wait?: number;
/**
The maximum time the `input` function is allowed to be delayed before it's invoked.
This can be used to control the rate of calls handled in a constant stream. For example, a media player sending updates every few milliseconds but wants to be handled only once a second.
@default Infinity
*/
readonly maxWait?: number;
/**
Trigger the function on the leading edge of the `wait` interval.
For example, this can be useful for preventing accidental double-clicks on a "submit" button from firing a second time.
@default false
*/
readonly before?: boolean;
/**
Trigger the function on the trailing edge of the `wait` interval.
@default true
*/
readonly after?: boolean;
};
export type BeforeOptions = {
readonly before: true;
} & Options;
export type NoBeforeNoAfterOptions = {
readonly after: false;
readonly before?: false;
} & Options;
export type DebouncedFunction<ArgumentsType extends unknown[], ReturnType> = {
(...arguments: ArgumentsType): ReturnType;
cancel(): void;
};
/**
[Debounce](https://davidwalsh.name/javascript-debounce-function) a function.
@param input - Function to debounce.
@returns A debounced function that delays calling the `input` function until after `wait` milliseconds have elapsed since the last time the debounced function was called.
It comes with a `.cancel()` method to cancel any scheduled `input` function calls.
@example
```
import debounceFn from 'debounce-fn';
window.onresize = debounceFn(() => {
// Do something on window resize
}, {wait: 100});
```
*/
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options: BeforeOptions
): DebouncedFunction<ArgumentsType, ReturnType>;
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options: NoBeforeNoAfterOptions
): DebouncedFunction<ArgumentsType, undefined>;
export default function debounceFn<ArgumentsType extends unknown[], ReturnType>(
input: (...arguments: ArgumentsType) => ReturnType,
options?: Options
): DebouncedFunction<ArgumentsType, ReturnType | undefined>;