1 | // Type definitions for ee-first 1.1
|
2 | // Project: https://github.com/jonathanong/ee-first
|
3 | // Definitions by: BendingBender <https://github.com/BendingBender>
|
4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
5 | // TypeScript Version: 3.0
|
6 |
|
7 | /// <reference types="node" />
|
8 |
|
9 | import { EventEmitter } from 'events';
|
10 |
|
11 | export = first;
|
12 |
|
13 | /**
|
14 | * Get the first event in a set of event emitters and event pairs, then clean up after itself.
|
15 | * Invoke `listener` on the first event from the list specified in `eventSpec`.
|
16 | *
|
17 | * @param eventSpec Array of arrays, with each array in the format `[ee, ...event]`.
|
18 | * @param listener Will be called only once, the first time any of the given events are emitted.
|
19 | * If `error` is one of the listened events, then if that fires first, the `listener` will be given the `err` argument.
|
20 | * `listener`'s arguments:
|
21 | * - `err`: the first argument emitted from an error event, if applicable
|
22 | * - `ee`: the event emitter that fired
|
23 | * - `event`: the string event name that fired
|
24 | * - `args`: an array of the arguments that were emitted on the event
|
25 | */
|
26 | declare function first<TEmitter extends EventEmitter>(
|
27 | eventSpec: Array<[TEmitter, ...string[]]>,
|
28 | listener: first.Listener<TEmitter>
|
29 | ): first.Thunk<TEmitter>;
|
30 |
|
31 | declare namespace first {
|
32 | type Listener<TEmitter extends EventEmitter> = (
|
33 | err: any,
|
34 | ee: TEmitter,
|
35 | event: string[],
|
36 | args: any[]
|
37 | ) => void;
|
38 |
|
39 | interface Thunk<TEmitter extends EventEmitter> {
|
40 | (listener: Listener<TEmitter>): void;
|
41 |
|
42 | /**
|
43 | * The group of listeners can be cancelled before being invoked and have all the event listeners removed
|
44 | * from the underlying event emitters.
|
45 | */
|
46 | cancel(): void;
|
47 | }
|
48 | }
|