Merge pull request #33240 from oddsund/master

throttle-debounce 2.1.0 - Add cancel method
This commit is contained in:
Nathan Shively-Sanders
2019-03-05 09:49:00 -08:00
committed by GitHub
2 changed files with 47 additions and 16 deletions
+23 -7
View File
@@ -1,8 +1,17 @@
// Type definitions for throttle-debounce 1.1
// Project: https://github.com/niksy/throttle-debounce
// Definitions by: Marek Buchar <https://github.com/czbuchi>, Frank Li <https://github.com/franklixuefei>
// Definitions by: Marek Buchar <https://github.com/czbuchi>, Frank Li <https://github.com/franklixuefei>, Thomas Oddsund <https://github.com/oddsund>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export {};
interface Cancel {
cancel: () => void;
}
export type throttle<T> = T & Cancel;
export type debounce<T> = throttle<T>;
/**
* Throttle execution of a function. Especially useful for rate limiting
* execution of handlers on events like resize and scroll.
@@ -34,8 +43,8 @@ export function throttle<T extends (...args: any[]) => any>(
delay: number,
noTrailing: boolean,
callback: T,
debounceMode?: boolean,
): T;
debounceMode?: boolean
): throttle<T>;
/**
* Throttle execution of a function. Especially useful for rate limiting
@@ -60,8 +69,8 @@ export function throttle<T extends (...args: any[]) => any>(
export function throttle<T extends (...args: any[]) => any>(
delay: number,
callback: T,
debounceMode?: boolean,
): T;
debounceMode?: boolean
): throttle<T>;
/**
* Debounce execution of a function. Debouncing, unlike throttling,
@@ -87,7 +96,11 @@ export function throttle<T extends (...args: any[]) => any>(
* @return
* A new, debounced function.
*/
export function debounce<T extends (...args: any[]) => any>(delay: number, atBegin: boolean, callback: T): T;
export function debounce<T extends (...args: any[]) => any>(
delay: number,
atBegin: boolean,
callback: T
): debounce<T>;
/**
* Debounce execution of a function. Debouncing, unlike throttling,
@@ -106,4 +119,7 @@ export function debounce<T extends (...args: any[]) => any>(delay: number, atBeg
* @return
* A new, debounced function.
*/
export function debounce<T extends (...args: any[]) => any>(delay: number, callback: T): T;
export function debounce<T extends (...args: any[]) => any>(
delay: number,
callback: T
): debounce<T>;
@@ -1,15 +1,30 @@
import throttleDebounce = require('throttle-debounce');
const { throttle, debounce } = throttleDebounce;
import { throttle, debounce } from "throttle-debounce";
type Func = () => void;
const func: Func = () => {};
const throttle1: Func = throttle(42, true, func, true);
const throttle2: Func = throttle(42, true, func);
const throttle3: Func = throttle(42, func, true);
const throttle4: Func = throttle(42, func);
const throttleWithoutCancel1: Func = throttle(42, true, func, true);
const throttleWithoutCancel2: Func = throttle(42, true, func);
const throttleWithoutCancel3: Func = throttle(42, func, true);
const throttleWithoutCancel4: Func = throttle(42, func);
const debounce1: Func = debounce(42, true, func);
const debounce2: Func = debounce(42, func);
throttleWithoutCancel1();
// $ExpectError
throttleWithoutCancel1.cancel();
const throttleWithCancel1: throttle<Func> = throttle(42, true, func, true);
const throttleWithCancel2: throttle<Func> = throttle(42, true, func);
const throttleWithCancel3: throttle<Func> = throttle(42, func, true);
const throttleWithCancel4: throttle<Func> = throttle(42, func);
throttleWithCancel1();
throttleWithCancel1.cancel();
const debounceWithoutCancel1: Func = debounce(42, true, func);
const debounceWithoutCancel2: Func = debounce(42, func);
debounceWithoutCancel1();
// $ExpectError
debounceWithoutCancel1.cancel();
const debounceWithCancel1: debounce<Func> = debounce(42, true, func);
const debounceWithCancel2: debounce<Func> = debounce(42, func);
debounceWithCancel1();
debounceWithCancel1.cancel();