-
-
Notifications
You must be signed in to change notification settings - Fork 31
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 (#17)
- Loading branch information
1 parent
05609e0
commit b734458
Showing
4 changed files
with
58 additions
and
50 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,43 +1,57 @@ | ||
export class AbortError extends Error { | ||
declare class AbortErrorClass extends Error { | ||
readonly name: 'AbortError'; | ||
|
||
/** | ||
* Abort pending execution. All unresolved promised are rejected with a `AbortError` error. | ||
*/ | ||
Abort pending execution. All unresolved promised are rejected with a `AbortError` error. | ||
*/ | ||
constructor(); | ||
} | ||
|
||
export type ThrottledFunction<Arguments extends unknown[], Return> = (( | ||
...arguments: Arguments | ||
) => Promise<Return>) & { | ||
declare namespace pThrottle { | ||
type ThrottledFunction<Arguments extends unknown[], Return> = (( | ||
...arguments: Arguments | ||
) => Promise<Return>) & { | ||
/** | ||
Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error. | ||
*/ | ||
abort(): void; | ||
}; | ||
|
||
type AbortError = AbortErrorClass; | ||
} | ||
|
||
declare const pThrottle: { | ||
/** | ||
* Abort pending executions. All unresolved promises are rejected with a `pThrottle.AbortError` error. | ||
*/ | ||
abort(): void; | ||
[Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions. | ||
@param fn - Promise-returning/async function or a normal function. | ||
@param limit - Maximum number of calls within an `interval`. | ||
@param interval - Timespan for `limit` in milliseconds. | ||
@returns A throttled version of `fn`. | ||
@example | ||
``` | ||
import pThrottle from 'p-throttle'; | ||
const throttled = pThrottle(async index => { | ||
return index * 2; | ||
}, 2, 1000); | ||
for (let i = 1; i <= 6; i++) { | ||
throttled(i).then(console.log); | ||
} | ||
``` | ||
*/ | ||
<Arguments extends unknown[], Return>( | ||
fn: (...arguments: Arguments) => PromiseLike<Return> | Return, | ||
limit: number, | ||
interval: number | ||
): pThrottle.ThrottledFunction<Arguments, Return>; | ||
|
||
AbortError: typeof AbortErrorClass; | ||
|
||
// TODO: Remove this for the next major release | ||
default: typeof pThrottle; | ||
}; | ||
|
||
/** | ||
* [Throttle](https://css-tricks.com/debouncing-throttling-explained-examples/) promise-returning/async/normal functions. | ||
* | ||
* @param fn - Promise-returning/async function or a normal function. | ||
* @param limit - Maximum number of calls within an `interval`. | ||
* @param interval - Timespan for `limit` in milliseconds. | ||
* @returns A throttled version of `fn`. | ||
* | ||
* @example | ||
* | ||
* import pThrottle from 'p-throttle'; | ||
* | ||
* const throttled = pThrottle(async index => { | ||
* return index * 2; | ||
* }, 2, 1000); | ||
* | ||
* for (let i = 1; i <= 6; i++) { | ||
* throttled(i).then(console.log); | ||
* } | ||
*/ | ||
export default function<Arguments extends unknown[], Return>( | ||
fn: (...arguments: Arguments) => PromiseLike<Return> | Return, | ||
limit: number, | ||
interval: number | ||
): ThrottledFunction<Arguments, Return>; | ||
export = pThrottle; |
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