From 31b07658597085c0d08ac7666614f8bd5fb91f15 Mon Sep 17 00:00:00 2001 From: zensh Date: Sun, 18 Sep 2016 19:12:25 +0800 Subject: [PATCH] improve typings --- .gitignore | 1 + README.md | 71 +++++++- README_zh.md => docs/api-zh.md | 90 ---------- examples/function-error.js | 22 +++ examples/simple.ts | 13 ++ package.json | 11 +- test.ts | 7 + test/typings.test.ts | 297 +++++++++++++++++++++++++++++++++ thunks.d.ts | 211 +++++++++++------------ thunks.es6.js | 2 +- thunks.js | 2 +- tsconfig.json | 13 ++ typings.json | 7 + 13 files changed, 525 insertions(+), 222 deletions(-) rename README_zh.md => docs/api-zh.md (88%) create mode 100644 examples/function-error.js create mode 100644 examples/simple.ts create mode 100644 test.ts create mode 100644 test/typings.test.ts create mode 100644 tsconfig.json create mode 100644 typings.json diff --git a/.gitignore b/.gitignore index 0f39a5e..1dbb6bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules/ debug/ coverage/ +typings/ test.js npm-debug.log diff --git a/README.md b/README.md index 541dc91..2d14ea7 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A small and magical composer for all JavaScript asynchronous. [![Coverage Status][coveralls-image]][coveralls-url] [![Downloads][downloads-image]][downloads-url] -[中文说明](https://github.com/thunks/thunks/blob/master/README_zh.md) +[中文说明](https://github.com/thunks/thunks/blob/master/docs/api-zh.md) [thunks 的作用域和异常处理设计](https://github.com/thunks/thunks/blob/master/docs/scope-and-error-catch.md) @@ -39,6 +39,8 @@ ES5+, support node.js and browsers. - [thunk.delay(delay)](#thunkdelaydelay) - [thunk.stop([messagge])](#thunkstopmessagge) - [thunk.cancel()](#thunkcancel) +- [TypesSript Typings](#typescript-typings) +- [What functions are thunkable?](#what-functions-are-thunkable) - [License MIT](#license) ## Implementations: @@ -255,10 +257,12 @@ The parameter `thunkable` value could be: ```js let thunk1 = thunk(1) - let thunk2 = thunk(thunk1) // thunk2 equals to thunk1 + thunk(thunk1)(function (error, value) { + console.log(error, value) // null 1 + }) ``` -2. `function (callback) {}`, by calling it, results woule be gathered and be passed to the next `thunkFunction` function +2. a thunkLike function `function (callback) {}`, by calling it, results woule be gathered and be passed to the next `thunkFunction` function ```js thunk(function (callback) { @@ -278,20 +282,25 @@ The parameter `thunkable` value could be: }) ``` -4. objects which implements methods of `toThunk` +4. objects which implements the method `toThunk` ```js - let then = Thenjs(1) // then.toThunk() return a thunk function - - thunk(then)(function (error, value) { + let obj = { + toThunk: function () { + return function (done) { done(null, 1) } + } + } + // `obj` has `toThunk` method that return a thunk function + thunk(obj)(function (error, value) { console.log(error, value) // null 1 }) ``` -5. objects which implements methods of `toPromise` +5. objects which implements the method `toPromise` ```js const Rx = require('rxjs') + // Observable instance has `toPromise` method that return a promise thunk(Rx.Observable.fromPromise(Promise.resolve(123)))(function (error, value) { console.log(error, value) // null 123 }) @@ -615,6 +624,52 @@ thunk.delay(100)(function () { This will cancel all control flow process in the current thunk's scope. +## TypesSript Typings + +```typescript +import * as thunks from '../' +const thunk = thunks() + +thunk(function * () { + while (true) { + yield function (done) { setTimeout(done, 1000) } + console.log('Dang!') + } +})() +``` + +## What functions are thunkable? + +thunks supports so many [thunkable](#thunkthunkable) objects. There are three kind of functions: + +- thunk like function `function (callback) { callback(err, someValue) }` +- generator function `function * () { yield something }` +- async/await function `async function () { await somePromise }` + +thunks can't suports common function (not thunk like function). thunks uses `fn.length === 1` to recognize thunk like function. + +Use common function in this way will throw error: +```js +thunk(function () {})(function (err) { + console.log(1, err) // 1 [Error: Not thunkable function: function () {}] +}) + +thunk(function (a, b) {})(function (err) { + console.log(2, err) // 2 [Error: Not thunkable function: function (a, b) {}] +}) + +thunk(function () { let callback = arguments[0]; callback() })(function (err) { + console.log(3, err) // 3 [Error: Not thunkable function: function () { let callback = arguments[0]; callback() }] +}) + +thunk()(function () { + return function () {} // can't return a non-thunkable function. +})(function (err) { + console.log(4, err) // 4 [Error: Not thunkable function: function () {}] +}) +``` +So pay attention to that **we can't return a non-thunkable function** in thunk. If we return a thunkable function, thunk will evaluate it to get a asynchronous value. + ## License thunks is licensed under the [MIT](https://github.com/thunks/tman/blob/master/LICENSE) license. Copyright © 2016 thunks. diff --git a/README_zh.md b/docs/api-zh.md similarity index 88% rename from README_zh.md rename to docs/api-zh.md index a2acc87..18af8a2 100644 --- a/README_zh.md +++ b/docs/api-zh.md @@ -9,14 +9,6 @@ A small and magical composer for all JavaScript asynchronous. [![Downloads][downloads-image]][downloads-url] [![Talk topic][talk-image]][talk-url] -## [Toa](https://github.com/toajs/toa): A powerful web framework rely on thunks. - -[thunks 的作用域和异常处理设计](https://github.com/thunks/thunks/blob/master/docs/scope-and-error-catch.md) - -## Compatibility - -ES5+, support node.js and browsers. - ## `thunk` 是什么? 0. [ALGOL thunks in 1961](http://archive.computerhistory.org/resources/text/algol/ACM_Algol_bulletin/1064045/frontmatter.pdf) @@ -31,88 +23,6 @@ ES5+, support node.js and browsers. 5. `callback` 的返回值如果是 **`thunk`** 函数,则等该 **`thunk`** 执行完毕将结果输入新 **`thunk`** 函数运行;如果是其它值,则当做正确结果进入新的 **`thunk`** 函数运行; -## Demo - -```js -const thunk = require('thunks')() -const fs = require('fs') -const size = thunk.thunkify(fs.stat) - -// generator -thunk(function * () { - - // sequential - console.log(yield size('.gitignore')) - console.log(yield size('thunks.js')) - console.log(yield size('package.json')) - -})(function * (error, res) { - //parallel - console.log(yield [ - size('.gitignore'), - size('thunks.js'), - size('package.json') - ]) -})() -``` - -```js -const thunk = require('thunks')() -const fs = require('fs') -const size = thunk.thunkify(fs.stat) - -// sequential -size('.gitignore')(function (error, res) { - console.log(error, res) - return size('thunks.js') - -})(function (error, res) { - console.log(error, res) - return size('package.json') - -})(function (error, res) { - console.log(error, res) -}) - -// parallel -thunk.all([ - size('.gitignore'), - size('thunks.js'), - size('package.json') -])(function (error, res) { - console.log(error, res) -}) - -// sequential -thunk.seq([ - size('.gitignore'), - size('thunks.js'), - size('package.json') -])(function (error, res) { - console.log(error, res) -}) -``` - -## Install - -**Node.js:** - -```sh -npm install thunks -``` - -**bower:** - -```sh -bower install thunks -``` - -**Browser:** - -```html - -``` - ## API ```js diff --git a/examples/function-error.js b/examples/function-error.js new file mode 100644 index 0000000..2f02db3 --- /dev/null +++ b/examples/function-error.js @@ -0,0 +1,22 @@ +'use strict' + +var thunks = require('..') +var thunk = thunks() + +thunk(function () {})(function (err) { + console.log(1, err) // 1 [Error: Not thunkable function: function () {}] +}) + +thunk(function (a, b) {})(function (err) { + console.log(2, err) // 2 [Error: Not thunkable function: function (a, b) {}] +}) + +thunk(function () { let callback = arguments[0]; callback() })(function (err) { + console.log(3, err) // 3 [Error: Not thunkable function: function () { let callback = arguments[0]; callback() }] +}) + +thunk()(function () { + return function () {} // can't return a not thunkable function. +})(function (err) { + console.log(4, err) // 4 [Error: Not thunkable function: function () {}] +}) diff --git a/examples/simple.ts b/examples/simple.ts new file mode 100644 index 0000000..ef1192e --- /dev/null +++ b/examples/simple.ts @@ -0,0 +1,13 @@ +'use strict' + +// `ts-node examples/simple.ts` + +import * as thunks from '../' +const thunk = thunks() + +thunk(function * () { + while (true) { + yield function (done) { setTimeout(done, 1000) } + console.log('Dang!') + } +})() diff --git a/package.json b/package.json index 4deed97..47e1f37 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,9 @@ "authors": [ "Yan Qing " ], - "version": "4.7.1", + "version": "4.7.2", "main": "thunks.js", - "typings": "./thunks.d.ts", + "typings": "thunks.d.ts", "jsnext:main": "thunks.es6.js", "repository": { "type": "git", @@ -34,17 +34,20 @@ "bluebird": "^3.4.6", "co": "^4.6.0", "istanbul": "^0.4.5", - "jsbench": "^1.0.2", + "jsbench": "^1.1.0", "promise": "^7.1.1", "regenerator": "^0.8.46", "should": "^11.1.0", "standard": "^8.0.0", "thenjs": "^2.0.3", - "tman": "^1.4.3" + "tman": "^1.4.5", + "ts-node": "^1.3.0", + "typescript": "^2.0.2" }, "scripts": { "test": "standard && tman test/index", "test-cov": "istanbul cover _tman test/index", + "test-typings": "tman -r ts-node/register test/typings.test.ts", "bench": "node benchmark/index" }, "files": [ diff --git a/test.ts b/test.ts new file mode 100644 index 0000000..f768b77 --- /dev/null +++ b/test.ts @@ -0,0 +1,7 @@ + +type mtypes = boolean | number | string | Array | void; + +export function testfn (): any { + let test: mtypes = [1,2,3]; + console.log(test) +} \ No newline at end of file diff --git a/test/typings.test.ts b/test/typings.test.ts new file mode 100644 index 0000000..3855a12 --- /dev/null +++ b/test/typings.test.ts @@ -0,0 +1,297 @@ +'use strict' + +// `tman -r ts-node/register test/typings.test.ts` + +/// + +import * as thunks from '../' +import * as assert from 'assert' + +const tman = require('tman') + +tman.suite('thunks typings', () => { + tman.it('thunks exports', function () { + assert.strictEqual(thunks.NAME, 'thunks') + assert.ok(typeof thunks.VERSION, 'string') + assert.strictEqual(thunks.pruneErrorStack, true) + assert.strictEqual(thunks.isGeneratorFn(function * () {}), true) + assert.strictEqual(thunks.isThunkableFn(function * () {}), true) + assert.strictEqual(thunks.isThunkableFn(function (done) { done() }), true) + assert.strictEqual(thunks.isAsyncFn(function (done) { done() }), false) + }) + + tman.it('thunks(options)', function () { + assert.ok(typeof thunks(), 'function') + assert.ok(typeof thunks(function (err) {}), 'function') + assert.ok(typeof thunks({onerror: function (err) {}}), 'function') + + assert.ok(typeof thunks(new thunks.Scope()), 'function') + assert.ok(typeof thunks(new thunks.Scope(function (err) {})), 'function') + assert.ok(typeof thunks(new thunks.Scope({onerror: function (err) {}})), 'function') + }) + + tman.it('thunk(void)', function () { + let thunk = thunks() + return thunk()(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, undefined) + })(function (err) { + assert.strictEqual(err, null) + })(function () {})()() // alway return ThunkFunction + }) + + tman.it('thunk(boolean)', function () { + let thunk = thunks() + return thunk(false)(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, false) + }) + }) + + tman.it('thunk(number)', function () { + let thunk = thunks() + return thunk(1)(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + }) + }) + + tman.it('thunk(string)', function () { + let thunk = thunks() + return thunk('hello')(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 'hello') + }) + }) + + tman.it('thunk(Array)', function () { + let thunk = thunks() + return thunk([1, '2'])(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, '2']) + }) + }) + + tman.it('thunk(Object)', function () { + let thunk = thunks() + let date = new Date() + return thunk(date)(function (err, res: Date) { + assert.strictEqual(err, null) + assert.strictEqual(res.toString(), date.toString()) + }) + }) + + tman.it('thunk(ThunkLikeFunction)', function () { + let thunk = thunks() + return thunk(function (done) { + done(new Error('some error')) + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.it('thunk(GeneratorFunction)', function () { + let thunk = thunks() + return thunk(function * () { + yield function (done) { setTimeout(done, 10) } + return 1 + })(function * (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + throw new Error('some error') + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + // TS will transform async function to generator function, but it is not good to recognize. + // babel do it better than TS. + // tman.it('thunk(AsyncFunction)', function () { + // let thunk = thunks() + // return thunk(async function () { + // await Promise.resolve() + // return 1 + // })(function (err, res) { + // assert.strictEqual(err, null) + // assert.strictEqual(res, 1) + // }) + // }) + + tman.it('thunk(PromiseLike)', function () { + let thunk = thunks() + return thunk({then: function (resolve, reject) { resolve(1) }})(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + return {then: function (resolve, reject) { reject(new Error('some error')) }} + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.it('thunk(Promise)', function () { + let thunk = thunks() + return thunk(Promise.resolve(1))(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + return Promise.reject(new Error('some error')) + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.it('thunk(ToThunk)', function () { + let thunk = thunks() + return thunk({toThunk: function () { return function (done) { done(null, 1) } }})(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + return {toThunk: function () { return function (done) { done(new Error('some error'), 1) }}} + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.it('thunk(ToPromise)', function () { + let thunk = thunks() + return thunk({toPromise: function () { return Promise.resolve(1) }})(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + return {toPromise: function () { return Promise.reject(new Error('some error')) }} + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.it('thunk(Generator)', function () { + let thunk = thunks() + return thunk((function * () { return yield 1 })())(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + return (function * () { throw new Error('some error') })() + })(function (err, res) { + assert.strictEqual(err.message, 'some error') + assert.strictEqual(res, undefined) + }) + }) + + tman.suite('thunk method', function () { + let thunk = thunks() + + tman.it('thunk.all(...args)', function () { + return thunk.all(thunk(1), Promise.resolve(2))(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, 2]) + }) + }) + + tman.it('thunk.all(array)', function () { + return thunk.all([thunk(1), Promise.resolve(2)])(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, 2]) + }) + }) + + tman.it('thunk.all(object)', function () { + return thunk.all({a: thunk(1), b: Promise.resolve(2)})(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, {a: 1, b: 2}) + }) + }) + + tman.it('thunk.seq(...args)', function () { + return thunk.seq(thunk(1), Promise.resolve(2))(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, 2]) + }) + }) + + tman.it('thunk.seq(array)', function () { + return thunk.seq([thunk(1), Promise.resolve(2)])(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, 2]) + }) + }) + + tman.it('thunk.race(...args)', function () { + return thunk.race(thunk(1), Promise.resolve(2))(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + }) + }) + + tman.it('thunk.race(array)', function () { + return thunk.race([thunk(1), Promise.resolve(2)])(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + }) + }) + + tman.it('thunk.persist(thunkable)', function () { + return thunk.persist(thunk(1))(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + }) + }) + + tman.it('thunk.thunkify(fn)', function () { + let test = function (val1: number, val2: string, done: (...args: any[]) => void) { + done(null, val1, val2) + } + let fn = thunk.thunkify(test) + return fn(1, '2')(function (err, res) { + assert.strictEqual(err, null) + assert.deepEqual(res, [1, '2']) + }) + }) + + tman.it('thunk.lift(fn)', function () { + let test = function (a, b) { return a + b } + let fn = thunk.lift(test) + return fn(thunk(1), Promise.resolve(2))(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 3) + }) + }) + + tman.it('thunk.delay(number)', function () { + return thunk.delay(100)(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, undefined) + }) + }) + }) + + tman.it('thunk.stop(message)', function (done) { + let thunk = thunks({ + onstop: function (sig) { + assert.strictEqual(sig.message, 'stop!') + assert.strictEqual(sig.status, 19) + assert.strictEqual(sig.code, 'SIGSTOP') + done() + } + }) + + thunk(1)(function (err, res) { + assert.strictEqual(err, null) + assert.strictEqual(res, 1) + thunk.stop('stop!') + })(function () { + assert.strictEqual('should not run', false) + }) + }) + + tman.it('thunk.cancel()', function (done) { + let thunk = thunks() + thunk.seq(thunk.delay(10), thunk.delay(10), thunk.delay(10))(function () { + assert.strictEqual('should not run', false) + }) + + setTimeout(thunk.cancel, 10) + setTimeout(done, 100) + }) +}) diff --git a/thunks.d.ts b/thunks.d.ts index e0fbf70..449de3b 100644 --- a/thunks.d.ts +++ b/thunks.d.ts @@ -4,169 +4,144 @@ * Definitions by: zensh */ -interface IThunks { - (): Ithunk -} +type primitives = boolean | number | string | Array | Object | void; +type thunkable = ThunkLikeFunction | GeneratorFunction | AsyncFunction | PromiseLike | ToThunk | ToPromise | Generator; +type FunctionWithCallback = FnWithCb0 | FnWithCb1 | FnWithCb2 | FnWithCb3 | FnWithCb4 | FnWithCb5 | FnWithCb6 | FnWithCb7 | FnWithCb8; -interface IThunks { - (options?: IThunksOptions): Ithunk +interface Callback { + (err?: Error, res?: primitives): primitives | thunkable; } -interface IThunks { - (errorHandler: (error: Error) => any): Ithunk +interface ThunkLikeFunction { + (fn: Callback): void; } -interface Ithunk extends IThunk { - all(...args: Array): Ithunk, - all(ThunkSequence: Array): Ithunk, - all(Object: any): Ithunk, - seq(...args: Array): Ithunk, - seq(ThunkSequence: Array): Ithunk, - race(ThunkSequence: Array): Ithunk, - race(...args: Array): Ithunk, - thunkify(FunctionWithCallback: IFunctionWithCallback): Ithunk, - lift(thunkable: Ithunk): Ithunk, - persist(thunkable: Ithunk): Ithunk, - delay(Time: number): Ithunk, - stop(message?: string): void, - cancel(): void +interface ThunkFunction { + (fn?: Callback | GeneratorFunction | AsyncFunction): ThunkFunction; } -interface IThunk { - (thunkable: Ithunk): Ithunk +interface thunk { + (thunkable?: primitives | thunkable): ThunkFunction; + all(...args: Array): ThunkFunction; + all(array: Array): ThunkFunction; + all(object: Object): ThunkFunction; + seq(...args: Array): ThunkFunction; + seq(array: Array): ThunkFunction; + race(...args: Array): ThunkFunction; + race(array: Array): ThunkFunction; + persist(thunkable: thunkable): ThunkFunction; + thunkify(FnWithCb: FunctionWithCallback): (...args: Array) => ThunkFunction; + lift(fn: (...args: Array) => primitives): (...args: Array) => ThunkFunction; + delay(Time?: number): ThunkFunction; + stop(message?: string): void; + cancel(): void; } -interface IThunk { - (thunkable: IThunkFn): Ithunk +// https://github.com/Microsoft/TypeScript/issues/1360 +interface NodeCallback { + (err?: Error, ...args: Array): void; } -interface IThunk { - (thunkable: IPromise): Ithunk +interface FnWithCb0 { + (callback: NodeCallback): void; } - -interface IThunk { - (thunkable: IToThunkFn): Ithunk +interface FnWithCb1 { + (arg1: primitives, callback: NodeCallback): void; } - -interface IThunk { - (thunkable: IToPromiseFn): Ithunk +interface FnWithCb2 { + (arg1: primitives, arg2: primitives, callback: NodeCallback): void; } - -interface IThunk { - (thunkable: IIterator): Ithunk +interface FnWithCb3 { + (arg1: primitives, arg2: primitives, arg3: primitives, callback: NodeCallback): void; } - -interface IThunk { - (thunkable?: any): Ithunk +interface FnWithCb4 { + (arg1: primitives, arg2: primitives, arg3: primitives, arg4: primitives, callback: NodeCallback): void; } - -interface IThunkFn { - (callback: (error?: Error, value?: any) => any): any +interface FnWithCb5 { + (arg1: primitives, arg2: primitives, arg3: primitives, arg4: primitives, arg5: primitives, callback: NodeCallback): void; } - -interface IToThunkFn { - toThunk:() => Ithunk +interface FnWithCb6 { + (arg1: primitives, arg2: primitives, arg3: primitives, arg4: primitives, arg5: primitives, arg6: primitives, callback: NodeCallback): void; } - -interface IToPromiseFn { - toPromise:() => IPromise +interface FnWithCb7 { + (arg1: primitives, arg2: primitives, arg3: primitives, arg4: primitives, arg5: primitives, arg6: primitives, arg7: primitives, callback: NodeCallback): void; } - -interface IThunksOptions { - onstop: (sig: ISigStop) => any, - onerror: (error: Error) => any, - debug: (value: any) => any +interface FnWithCb8 { + (arg1: primitives, arg2: primitives, arg3: primitives, arg4: primitives, arg5: primitives, arg6: primitives, arg7: primitives, arg8: primitives, callback: NodeCallback): void; } -interface ISigStop { - (message: string): void - status: number, - code: string -} -// https://github.com/Microsoft/TypeScript/issues/1360 -interface IFunctionWithCallback { - (callback: (...args: any[]) => any): any +interface ToThunk { + toThunk(): ThunkLikeFunction; } -interface IFunctionWithCallback { - (arg: any, callback: (...args: any[]) => any): any +interface ToPromise { + toPromise(): PromiseLike; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, callback: (...args: any[]) => any): any -} -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, callback: (...args: any[]) => any): any +interface GeneratorFunction extends Function { + (err?: Error, res?: primitives): Generator; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, callback: (...args: any[]) => any): any +interface GeneratorFunctionConstructor { + new (...args: string[]): GeneratorFunction; + (...args: string[]): GeneratorFunction; + prototype: GeneratorFunction; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, callback: (...args: any[]) => any): any +interface IteratorResult { + done: boolean; + value: primitives | thunkable; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, callback: (...args: any[]) => any): any +interface Generator { + constructor: GeneratorFunctionConstructor; + next(value?: primitives | thunkable): IteratorResult; + throw(err?: Error): IteratorResult; + return(value?: primitives | thunkable): IteratorResult; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, callback: (...args: any[]) => any): any +interface AsyncFunction extends Function { + (err?: Error, res?: primitives): PromiseLike; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, arg8: any, callback: (...args: any[]) => any): any +interface AsyncFunctionConstructor { + new (...args: string[]): AsyncFunction; + (...args: string[]): AsyncFunction; + prototype: AsyncFunction; } -interface IFunctionWithCallback { - (arg1: any, arg2: any, arg3: any, arg4: any, arg5: any, arg6: any, arg7: any, arg8: any, arg9: any, callback: (...args: any[]) => any): any +interface PromiseLike { + then(onfulfilled?: (value: primitives | thunkable) => primitives | thunkable, onrejected?: (reason: Error) => primitives | thunkable): PromiseLike; } -interface IPromiseLike { - /** - * Attaches callbacks for the resolution and/or rejection of the Promise. - * @param onfulfilled The callback to execute when the Promise is resolved. - * @param onrejected The callback to execute when the Promise is rejected. - * @returns A Promise for the completion of which ever callback is executed. - */ - then(onfulfilled?: (value: T) => TResult | IPromiseLike, onrejected?: (reason: any) => TResult | IPromiseLike): IPromiseLike - then(onfulfilled?: (value: T) => TResult | IPromiseLike, onrejected?: (reason: any) => void): IPromiseLike +interface SigStop { + message: string; + status: number; + code: string; } -/** -* Represents the completion of an asynchronous operation -*/ -interface IPromise { - /** - * Attaches callbacks for the resolution and/or rejection of the Promise. - * @param onfulfilled The callback to execute when the Promise is resolved. - * @param onrejected The callback to execute when the Promise is rejected. - * @returns A Promise for the completion of which ever callback is executed. - */ - then(onfulfilled?: (value: T) => TResult | IPromiseLike, onrejected?: (reason: any) => TResult | IPromiseLike): IPromise - then(onfulfilled?: (value: T) => TResult | IPromiseLike, onrejected?: (reason: any) => void): IPromise - - /** - * Attaches a callback for only the rejection of the Promise. - * @param onrejected The callback to execute when the Promise is rejected. - * @returns A Promise for the completion of the callback. - */ - catch(onrejected?: (reason: any) => T | IPromiseLike): IPromise +interface scopeOnerror { + (error: Error): Error | boolean | void; } -interface IIteratorResult { - done: boolean - value?: T +interface scopeOptions { + onerror?: scopeOnerror; + onstop?: (sig: SigStop) => void; + debug?: (value: any) => void; } -interface IIterator { - next(value?: any): IIteratorResult - return?(value?: any): IIteratorResult - throw?(e?: any): IIteratorResult +declare function thunks (options?: scopeOnerror | scopeOptions): thunk; +declare module thunks { + export const NAME: string; + export const VERSION: string; + export const pruneErrorStack: boolean; + export function isGeneratorFn(fn: any): boolean; + export function isAsyncFn(fn: any): boolean; + export function isThunkableFn(fn: any): boolean; + export class Scope { + constructor(options?: scopeOnerror | scopeOptions); + } } -declare var Thunk: IThunks - -export default Thunk +export = thunks; diff --git a/thunks.es6.js b/thunks.es6.js index 8152011..374b322 100644 --- a/thunks.es6.js +++ b/thunks.es6.js @@ -377,7 +377,7 @@ function pruneErrorStack (error) { } thunks.NAME = 'thunks' -thunks.VERSION = '4.7.1' +thunks.VERSION = '4.7.2' thunks.pruneErrorStack = true thunks.Scope = Scope thunks.isGeneratorFn = (fn) => isFunction(fn) && isGeneratorFn(fn) diff --git a/thunks.js b/thunks.js index 3cdaad6..bec52a3 100644 --- a/thunks.js +++ b/thunks.js @@ -399,7 +399,7 @@ } thunks.NAME = 'thunks' - thunks.VERSION = '4.7.1' + thunks.VERSION = '4.7.2' thunks['default'] = thunks thunks.pruneErrorStack = true thunks.Scope = Scope diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..031c7d2 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "moduleResolution": "node" + }, + "exclude": [ + "coverage", + "debug", + "node_modules" + ], + "compileOnSave": false +} diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..bac849e --- /dev/null +++ b/typings.json @@ -0,0 +1,7 @@ +{ + "name": "thunks", + "dependencies": {}, + "globalDevDependencies": { + "node": "registry:dt/node#6.0.0+20160915134512" + } +}