UNPKG

3.44 kBTypeScriptView Raw
1export {};
2
3interface CancelOptions {
4 upcomingOnly?: boolean;
5}
6
7interface Cancel {
8 cancel: (options?: CancelOptions) => void;
9}
10
11interface NoReturn<T extends (...args: any[]) => any> {
12 (...args: Parameters<T>): void;
13}
14
15export type throttle<T extends (...args: any[]) => any> = NoReturn<T> & Cancel;
16export type debounce<T extends (...args: any[]) => any> = NoReturn<T> & Cancel;
17
18interface ThrottleOptions {
19 noTrailing?: boolean;
20 noLeading?: boolean;
21 debounceMode?: boolean;
22}
23
24interface DebounceOptions {
25 atBegin?: boolean;
26}
27
28/**
29 * Throttle execution of a function. Especially useful for rate limiting
30 * execution of handlers on events like resize and scroll.
31 *
32 * @param delay
33 * A zero-or-greater delay in milliseconds. For event callbacks, values around
34 * 100 or 250 (or even higher) are most useful.
35 *
36 * @param callback
37 * A function to be executed after delay milliseconds. The `this` context and
38 * all arguments are passed through, as-is, to `callback` when the
39 * throttled-function is executed.
40 *
41 * @param options
42 * An object to configure options.
43 *
44 * @param options.noTrailing
45 * Optional, defaults to false. If noTrailing is true, callback will only execute
46 * every `delay` milliseconds while the throttled-function is being called. If
47 * noTrailing is false or unspecified, callback will be executed one final time
48 * after the last throttled-function call. (After the throttled-function has not
49 * been called for `delay` milliseconds, the internal counter is reset)
50 *
51 * @param options.noLeading
52 * Optional, defaults to false. If noLeading is false, the first throttled-function
53 * call will execute callback immediately. If noLeading is true, the first the
54 * callback execution will be skipped. It should be noted that callback will never
55 * executed if both noLeading = true and noTrailing = true.
56 *
57 * @param options.debounceMode If `debounceMode` is true (at begin), schedule
58 * `callback` to execute after `delay` ms. If `debounceMode` is false (at end),
59 * schedule `callback` to execute after `delay` ms.
60 *
61 * @return
62 * A new, throttled, function.
63 */
64export function throttle<T extends (...args: any[]) => any>(
65 delay: number,
66 callback: T,
67 options?: ThrottleOptions,
68): throttle<T>;
69
70/**
71 * Debounce execution of a function. Debouncing, unlike throttling,
72 * guarantees that a function is only executed a single time, either at the
73 * very beginning of a series of calls, or at the very end.
74 *
75 * @param delay
76 * A zero-or-greater delay in milliseconds. For event callbacks, values around
77 * 100 or 250 (or even higher) are most useful.
78 *
79 * @param callback
80 * A function to be executed after delay milliseconds. The `this` context and
81 * all arguments are passed through, as-is, to `callback` when the
82 * debounced-function is executed.
83 *
84 * @param options
85 * An object to configure options.
86 *
87 * @param options.atBegin
88 * If atBegin is false or unspecified, callback will only be executed `delay`
89 * milliseconds after the last debounced-function call. If atBegin is true,
90 * callback will be executed only at the first debounced-function call. (After
91 * the throttled-function has not been called for `delay` milliseconds, the
92 * internal counter is reset).
93 *
94 * @return
95 * A new, debounced function.
96 */
97export function debounce<T extends (...args: any[]) => any>(
98 delay: number,
99 callback: T,
100 options?: DebounceOptions,
101): debounce<T>;