diff --git a/types/ee-first/ee-first-tests.ts b/types/ee-first/ee-first-tests.ts new file mode 100644 index 0000000000..9ffc03281d --- /dev/null +++ b/types/ee-first/ee-first-tests.ts @@ -0,0 +1,32 @@ +import first = require('ee-first'); +import { EventEmitter } from 'events'; + +class Foo extends EventEmitter { + foo: 'foo'; +} + +class Bar extends EventEmitter { + bar: 'bar'; +} + +const ee1 = new Foo(); +const ee2 = new Bar(); + +const thunk = first( + [[ee1, 'close', 'end', 'error'], [ee2, 'error']], + (err, ee, event, args) => { + err; // $ExpectType any + ee; // $ExpectType Foo | Bar + event; // $ExpectType string[] + args; // $ExpectType any[] + } +); + +thunk((err, ee, event, args) => { + err; // $ExpectType any + ee; // $ExpectType Foo | Bar + event; // $ExpectType string[] + args; // $ExpectType any[] +}); + +thunk.cancel(); diff --git a/types/ee-first/index.d.ts b/types/ee-first/index.d.ts new file mode 100644 index 0000000000..6898d6b664 --- /dev/null +++ b/types/ee-first/index.d.ts @@ -0,0 +1,48 @@ +// Type definitions for ee-first 1.1 +// Project: https://github.com/jonathanong/ee-first +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +/// + +import { EventEmitter } from 'events'; + +export = first; + +/** + * Get the first event in a set of event emitters and event pairs, then clean up after itself. + * Invoke `listener` on the first event from the list specified in `eventSpec`. + * + * @param eventSpec Array of arrays, with each array in the format `[ee, ...event]`. + * @param listener Will be called only once, the first time any of the given events are emitted. + * If `error` is one of the listened events, then if that fires first, the `listener` will be given the `err` argument. + * `listener`'s arguments: + * - `err`: the first argument emitted from an error event, if applicable + * - `ee`: the event emitter that fired + * - `event`: the string event name that fired + * - `args`: an array of the arguments that were emitted on the event + */ +declare function first( + eventSpec: Array<[TEmitter, ...string[]]>, + listener: first.Listener +): first.Thunk; + +declare namespace first { + type Listener = ( + err: any, + ee: TEmitter, + event: string[], + args: any[] + ) => void; + + interface Thunk { + (listener: Listener): void; + + /** + * The group of listeners can be cancelled before being invoked and have all the event listeners removed + * from the underlying event emitters. + */ + cancel(): void; + } +} diff --git a/types/ee-first/tsconfig.json b/types/ee-first/tsconfig.json new file mode 100644 index 0000000000..2de6e9baf0 --- /dev/null +++ b/types/ee-first/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ee-first-tests.ts" + ] +} diff --git a/types/ee-first/tslint.json b/types/ee-first/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/ee-first/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }