From 49d632d31a54df67dc4747b386c64315701fd322 Mon Sep 17 00:00:00 2001 From: Thomas Stenberg Oddsund Date: Wed, 20 Feb 2019 20:51:27 +0100 Subject: [PATCH 1/2] Throttle and debounce enhance the function it wraps as of 2.1.0 --- types/throttle-debounce/index.d.ts | 30 ++++++++++++++----- .../throttle-debounce-tests.ts | 28 +++++++++++------ 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/types/throttle-debounce/index.d.ts b/types/throttle-debounce/index.d.ts index 34bfbbdb5b..5b1f197a31 100644 --- a/types/throttle-debounce/index.d.ts +++ b/types/throttle-debounce/index.d.ts @@ -1,8 +1,17 @@ // Type definitions for throttle-debounce 1.1 // Project: https://github.com/niksy/throttle-debounce -// Definitions by: Marek Buchar , Frank Li +// Definitions by: Marek Buchar , Frank Li , Thomas Oddsund // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +export {}; + +interface Cancel { + cancel: () => void; +} + +export type throttle = T & Cancel; +export type debounce = throttle; + /** * 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 any>( delay: number, noTrailing: boolean, callback: T, - debounceMode?: boolean, -): T; + debounceMode?: boolean +): throttle; /** * Throttle execution of a function. Especially useful for rate limiting @@ -60,8 +69,8 @@ export function throttle any>( export function throttle any>( delay: number, callback: T, - debounceMode?: boolean, -): T; + debounceMode?: boolean +): throttle; /** * Debounce execution of a function. Debouncing, unlike throttling, @@ -87,7 +96,11 @@ export function throttle any>( * @return * A new, debounced function. */ -export function debounce any>(delay: number, atBegin: boolean, callback: T): T; +export function debounce any>( + delay: number, + atBegin: boolean, + callback: T +): debounce; /** * Debounce execution of a function. Debouncing, unlike throttling, @@ -106,4 +119,7 @@ export function debounce any>(delay: number, atBeg * @return * A new, debounced function. */ -export function debounce any>(delay: number, callback: T): T; +export function debounce any>( + delay: number, + callback: T +): debounce; diff --git a/types/throttle-debounce/throttle-debounce-tests.ts b/types/throttle-debounce/throttle-debounce-tests.ts index 0090eb1039..506d3742ad 100644 --- a/types/throttle-debounce/throttle-debounce-tests.ts +++ b/types/throttle-debounce/throttle-debounce-tests.ts @@ -1,15 +1,25 @@ -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); +// $ExpectError +throttleWithoutCancel1.cancel(); +const throttleWithCancel1: throttle = throttle(42, true, func, true); +const throttleWithCancel2: throttle = throttle(42, true, func); +const throttleWithCancel3: throttle = throttle(42, func, true); +const throttleWithCancel4: throttle = throttle(42, func); +throttleWithCancel1.cancel(); -const debounce1: Func = debounce(42, true, func); -const debounce2: Func = debounce(42, func); +const debounceWithoutCancel1: Func = debounce(42, true, func); +const debounceWithoutCancel2: Func = debounce(42, func); +// $ExpectError +debounceWithoutCancel1.cancel(); +const debounceWithCancel1: debounce = debounce(42, true, func); +const debounceWithCancel2: debounce = debounce(42, func); +debounceWithCancel1.cancel(); From 851667c52342e5671609d5edc9859136daa310a5 Mon Sep 17 00:00:00 2001 From: Thomas Stenberg Oddsund Date: Wed, 20 Feb 2019 23:45:22 +0100 Subject: [PATCH 2/2] Added tests where the wrapped function is called. --- types/throttle-debounce/throttle-debounce-tests.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/throttle-debounce/throttle-debounce-tests.ts b/types/throttle-debounce/throttle-debounce-tests.ts index 506d3742ad..ff4f757dbc 100644 --- a/types/throttle-debounce/throttle-debounce-tests.ts +++ b/types/throttle-debounce/throttle-debounce-tests.ts @@ -8,18 +8,23 @@ 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); + +throttleWithoutCancel1(); // $ExpectError throttleWithoutCancel1.cancel(); const throttleWithCancel1: throttle = throttle(42, true, func, true); const throttleWithCancel2: throttle = throttle(42, true, func); const throttleWithCancel3: throttle = throttle(42, func, true); const throttleWithCancel4: throttle = 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 = debounce(42, true, func); const debounceWithCancel2: debounce = debounce(42, func); +debounceWithCancel1(); debounceWithCancel1.cancel();