-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#6)
- Loading branch information
1 parent
7a7706e
commit 8736973
Showing
4 changed files
with
56 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,52 @@ | ||
export interface Options { | ||
/** | ||
* Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds. | ||
* | ||
* @default false | ||
*/ | ||
readonly leading?: boolean; | ||
declare namespace pDebounce { | ||
interface Options { | ||
/** | ||
Call the `fn` on the [leading edge of the timeout](https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-1). Meaning immediately, instead of waiting for `wait` milliseconds. | ||
@default false | ||
*/ | ||
readonly leading?: boolean; | ||
} | ||
} | ||
|
||
/** | ||
* [Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions. | ||
* | ||
* @param fn - Promise-returning/async function to debounce. | ||
* @param wait - Milliseconds to wait before calling `fn`. | ||
* @returns Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called. | ||
*/ | ||
export default function pDebounce<ArgumentsType extends unknown[], ReturnType>( | ||
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType, | ||
wait: number, | ||
options?: Options | ||
): (...arguments: ArgumentsType) => Promise<ReturnType>; | ||
declare const pDebounce: { | ||
/** | ||
[Debounce](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning & async functions. | ||
@param fn - Promise-returning/async function to debounce. | ||
@param wait - Milliseconds to wait before calling `fn`. | ||
@returns Returns a function that delays calling `fn` until after `wait` milliseconds have elapsed since the last time it was called. | ||
@example | ||
``` | ||
import pDebounce = require('p-debounce'); | ||
const expensiveCall = async input => input; | ||
const debouncedFn = pDebounce(expensiveCall, 200); | ||
for (const i of [1, 2, 3]) { | ||
debouncedFn(i).then(console.log); | ||
} | ||
//=> 3 | ||
//=> 3 | ||
//=> 3 | ||
``` | ||
*/ | ||
<ArgumentsType extends unknown[], ReturnType>( | ||
fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType, | ||
wait: number, | ||
options?: pDebounce.Options | ||
): (...arguments: ArgumentsType) => Promise<ReturnType>; | ||
|
||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function pDebounce<ArgumentsType extends unknown[], ReturnType>( | ||
// fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType> | ReturnType, | ||
// wait: number, | ||
// options?: pDebounce.Options | ||
// ): (...arguments: ArgumentsType) => Promise<ReturnType>; | ||
// export = pDebounce; | ||
default: typeof pDebounce; | ||
}; | ||
|
||
export = pDebounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters