diff --git a/index.d.ts b/index.d.ts index 7625140..038ca51 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,32 @@ -declare function throat) => Promise>(size: number, fn: TFn): TFn; -declare function throat) => Promise>(fn: TFn, size: number): TFn; -declare function throat(size: number): (fn: () => Promise) => Promise; +/** + * 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( + size: number, + fn: (...args: TArgs) => Promise +): (...args: TArgs) => Promise; + +/** + * 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( + fn: (...args: TArgs) => Promise, + size: number +): (...args: TArgs) => Promise; + +/** + * 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 +): ( + fn: (...args: TArgs) => Promise, + ...args: TArgs +) => Promise; +export default throat; -export = throat; // get typings via: import throat = require("throat") diff --git a/index.js b/index.js index f8f1e81..9e83da6 100644 --- a/index.js +++ b/index.js @@ -77,6 +77,8 @@ module.exports = function (PromiseArgument) { } }; +module.exports.default = module.exports; + /* istanbul ignore next */ if (typeof Promise === 'function') { module.exports.Promise = Promise; diff --git a/package.json b/package.json index e2954a0..b74b795 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/__snapshots__/typescript.test.js.snap b/test/__snapshots__/typescript.test.js.snap index 2069b27..82d2a14 100644 --- a/test/__snapshots__/typescript.test.js.snap +++ b/test/__snapshots__/typescript.test.js.snap @@ -8,8 +8,8 @@ test/typescript-example.ts(7,7): error TS2322: Type 'Promise' 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' is not assignable to type 'Promise'. -test/typescript-example.ts(19,41): error TS2345: Argument of type '(x: number) => Promise' is not assignable to parameter of type '() => Promise'. -test/typescript-example.ts(20,42): error TS2345: Argument of type '(x: string) => Promise' is not assignable to parameter of type '() => Promise'. -test/typescript-example.ts(21,7): error TS2322: Type 'Promise' is not assignable to type 'Promise'. +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' is not assignable to type 'Promise'. " `; diff --git a/test/typescript-example.ts b/test/typescript-example.ts index bbea065..8532ba3 100644 --- a/test/typescript-example.ts +++ b/test/typescript-example.ts @@ -1,4 +1,4 @@ -import throat = require('../'); +import throat from '../'; const adderA = throat(5, (a: number, b: number) => Promise.resolve(a + b)); @@ -16,6 +16,7 @@ const throttle = throat(5); const t: Promise = throttle(() => Promise.resolve(5)); const t2: Promise = throttle(() => Promise.resolve('foo')); +const t3: Promise = throttle((x: number) => Promise.resolve(x), 10); const tFail: Promise = throttle((x: number) => Promise.resolve(x)); const tFail2: Promise = throttle((x: string) => Promise.resolve(x)); const tFail3: Promise = throttle(() => Promise.resolve(5));