UNPKG

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