Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definition file #28

Merged
merged 3 commits into from Feb 7, 2017

Conversation

alexperovich
Copy link
Contributor

Fixes #26

This change adds typescript definitions using an ejs template file to produce the overloads of submit and schedule.

I also added a default export from the module to improve interop with ES2015 modules and TypeScript import syntax.

@SGrondin
Copy link
Owner

SGrondin commented Jan 5, 2017

Wonderful! Thank you! I'll review as soon as I can.


declare module "bottleneck" {
namespace Bottleneck {
type Callback<T> = (err: any, result: T) => void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback can return more than one result, but I understand that having to define
type Callback2<T,U> = (err: any, result1: T, result2: U) => void; and then Callback3 and so on would make the whole thing exponentially complicated since submit() and submitPriority() are declared in a loop. If you think of a simple way to do it, please feel free to implement it :)

* Cancels all queued up requests and every added request will be automatically rejected.
* @param interrupt - If true, prevent the requests currently running from calling their callback when they're done. Default: false.
*/
stopAll(interrupt?: boolean): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stopAll returns the limiter (type Bottleneck)

/**
* Changes the penalty value used by the BLOCK strategy.
*/
changePenalty(penalty: number): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changePenalty returns the limiter (type Bottleneck)

* @param strategy - Which strategy to use if the queue gets longer than the high water mark. Default: Bottleneck.strategy.LEAK
* @param rejectOnDrop - When true if a job is dropped its callback will be called with the first argument set to an Error object. If the job was a promise it will be rejected. Default: false
*/
changeSettings(maxConcurrent?: number, minTime?: number, highWater?: number, strategy?: Bottleneck.Strategy, rejectOnDrop?: boolean): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changeSettings returns the limiter (type Bottleneck)

/**
* Changes the reservoir count.
*/
changeReservoir(reservoir: number): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changeReservoir returns the limiter (type Bottleneck)

* @param name - The event name.
* @param fn - The callback function.
*/
on(name: string, fn: Function): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the on() methods return the limiter (type Bottleneck)

/**
* Adds to the reservoir count.
*/
incrementReservoir(incrementBy: number): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incrementReservoir returns the limiter (type Bottleneck)

* Chain this limiter to another.
* @param other - The limiter that requests to this limiter must also follow.
*/
chain(other: Bottleneck): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chain returns the limiter (type Bottleneck)

@SGrondin
Copy link
Owner

Sorry for the long delay. This is beautiful! I've left a few comments.

@alexperovich
Copy link
Contributor Author

@SGrondin I have made the requested fixes and have updated the PR.

@SGrondin SGrondin merged commit fb9ba0d into SGrondin:master Feb 7, 2017
@SGrondin
Copy link
Owner

SGrondin commented Feb 7, 2017

Awesome! Thank you very much.

The callback can return more than one result, but I understand that having to define
type Callback2<T,U> = (err: any, result1: T, result2: U) => void; and then Callback3 and so on would make the whole thing exponentially complicated since submit() and submitPriority() are declared in a loop. Ideas?

Any thoughts about this?

@alexperovich
Copy link
Contributor Author

It would be possible to generate signatures using nested loops for those cases, but it would be nasty code. I don't think that case is very interesting. I haven't seen any libraries that use callbacks with multiple return values and if multiple returns are needed in application code TypeScripts Tuple type support can be used. T would be something like [string, int] in that case. If there is a compelling use case for multiple results in a callback I can add cases for 2/3 returns.

@alexperovich alexperovich deleted the typescriptDefinitions branch February 8, 2017 00:00
@SGrondin
Copy link
Owner

SGrondin commented Feb 8, 2017

Good point! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants