Skip to content

Commit

Permalink
fix: allow exra args and use default export in TypeScript
Browse files Browse the repository at this point in the history
[fixes #55]

BREAKING CHANGE: If using TypeScript you must upgrade to at least
TypeScrTypeScript@3

BREAKING CHANGE: If using TypeScript, swap:

```ts
import throat = require('throthroat');
```

to

```ts
import throat from 'throat';
```
  • Loading branch information
ForbesLindesay committed May 24, 2019
1 parent 1c6a45b commit 871d501
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
35 changes: 31 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
declare function throat<TResult, TFn extends (...args: Array<any>) => Promise<TResult>>(size: number, fn: TFn): TFn;
declare function throat<TResult, TFn extends (...args: Array<any>) => Promise<TResult>>(fn: TFn, size: number): TFn;
declare function throat(size: number): <TResult>(fn: () => Promise<TResult>) => Promise<TResult>;
/**
* Throttle the given function to only run `size` times in parallel.
* Extra calls will be queued until one of the earlier calls completes.
*/
declare function throat<TResult, TArgs extends any[]>(
size: number,
fn: (...args: TArgs) => Promise<TResult>
): (...args: TArgs) => Promise<TResult>;

/**
* Throttle the given function to only run `size` times in parallel.
* Extra calls will be queued until one of the earlier calls completes.
*/
declare function throat<TResult, TArgs extends any[]>(
fn: (...args: TArgs) => Promise<TResult>,
size: number
): (...args: TArgs) => Promise<TResult>;

/**
* Create a throttle that only allows `size` calls in parallel.
* Extra calls will be queued until one of the earlier calls completes.
*
* To create an exclusive lock, just use a `size` of `1`.
*/
declare function throat(
size: number
): <TResult, TArgs extends any[] = []>(
fn: (...args: TArgs) => Promise<TResult>,
...args: TArgs
) => Promise<TResult>;
export default throat;

export = throat; // get typings via: import throat = require("throat")
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ module.exports = function (PromiseArgument) {
}
};

module.exports.default = module.exports;

/* istanbul ignore next */
if (typeof Promise === 'function') {
module.exports.Promise = Promise;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"sauce-test": "^1.0.0",
"test-result": "^2.0.0",
"testit": "^3.1.0",
"typescript": "^2.3.4"
"typescript": "^3.4.5"
},
"jest": {
"testEnvironment": "node"
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/typescript.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ test/typescript-example.ts(7,7): error TS2322: Type 'Promise<number>' is not ass
Type 'number' is not assignable to type 'string'.
test/typescript-example.ts(12,42): error TS2345: Argument of type '\\"foo\\"' is not assignable to parameter of type 'number'.
test/typescript-example.ts(13,7): error TS2322: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
test/typescript-example.ts(19,41): error TS2345: Argument of type '(x: number) => Promise<number>' is not assignable to parameter of type '() => Promise<number>'.
test/typescript-example.ts(20,42): error TS2345: Argument of type '(x: string) => Promise<string>' is not assignable to parameter of type '() => Promise<string>'.
test/typescript-example.ts(21,7): error TS2322: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
test/typescript-example.ts(20,32): error TS2554: Expected 2 arguments, but got 1.
test/typescript-example.ts(21,33): error TS2554: Expected 2 arguments, but got 1.
test/typescript-example.ts(22,7): error TS2322: Type 'Promise<number>' is not assignable to type 'Promise<string>'.
"
`;
3 changes: 2 additions & 1 deletion test/typescript-example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import throat = require('../');
import throat from '../';

const adderA = throat(5, (a: number, b: number) => Promise.resolve(a + b));

Expand All @@ -16,6 +16,7 @@ const throttle = throat(5);

const t: Promise<number> = throttle(() => Promise.resolve(5));
const t2: Promise<string> = throttle(() => Promise.resolve('foo'));
const t3: Promise<number> = throttle((x: number) => Promise.resolve(x), 10);
const tFail: Promise<number> = throttle((x: number) => Promise.resolve(x));
const tFail2: Promise<string> = throttle((x: string) => Promise.resolve(x));
const tFail3: Promise<string> = throttle(() => Promise.resolve(5));

0 comments on commit 871d501

Please sign in to comment.