UNPKG

4 kBTypeScriptView Raw
1declare module 'events' {
2 interface EventEmitterOptions {
3 /**
4 * Enables automatic capturing of promise rejection.
5 */
6 captureRejections?: boolean;
7 }
8
9 interface NodeEventTarget {
10 once(event: string | symbol, listener: (...args: any[]) => void): this;
11 }
12
13 interface DOMEventTarget {
14 addEventListener(event: string, listener: (...args: any[]) => void, opts?: { once: boolean }): any;
15 }
16
17 interface StaticEventEmitterOptions {
18 signal?: AbortSignal;
19 }
20
21 interface EventEmitter extends NodeJS.EventEmitter {}
22 class EventEmitter {
23 constructor(options?: EventEmitterOptions);
24
25 static once(emitter: NodeEventTarget, event: string | symbol, options?: StaticEventEmitterOptions): Promise<any[]>;
26 static once(emitter: DOMEventTarget, event: string, options?: StaticEventEmitterOptions): Promise<any[]>;
27 static on(emitter: NodeJS.EventEmitter, event: string, options?: StaticEventEmitterOptions): AsyncIterableIterator<any>;
28
29 /** @deprecated since v4.0.0 */
30 static listenerCount(emitter: NodeJS.EventEmitter, event: string | symbol): number;
31 /**
32 * Returns a list listener for a specific emitter event name.
33 */
34 static getEventListener(emitter: DOMEventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
35
36 /**
37 * This symbol shall be used to install a listener for only monitoring `'error'`
38 * events. Listeners installed using this symbol are called before the regular
39 * `'error'` listeners are called.
40 *
41 * Installing a listener using this symbol does not change the behavior once an
42 * `'error'` event is emitted, therefore the process will still crash if no
43 * regular `'error'` listener is installed.
44 */
45 static readonly errorMonitor: unique symbol;
46 static readonly captureRejectionSymbol: unique symbol;
47
48 /**
49 * Sets or gets the default captureRejection value for all emitters.
50 */
51 // TODO: These should be described using static getter/setter pairs:
52 static captureRejections: boolean;
53 static defaultMaxListeners: number;
54 }
55
56 import internal = require('events');
57 namespace EventEmitter {
58 // Should just be `export { EventEmitter }`, but that doesn't work in TypeScript 3.4
59 export { internal as EventEmitter };
60
61 export interface Abortable {
62 /**
63 * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
64 */
65 signal?: AbortSignal;
66 }
67 }
68
69 global {
70 namespace NodeJS {
71 interface EventEmitter {
72 addListener(event: string | symbol, listener: (...args: any[]) => void): this;
73 on(event: string | symbol, listener: (...args: any[]) => void): this;
74 once(event: string | symbol, listener: (...args: any[]) => void): this;
75 removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
76 off(event: string | symbol, listener: (...args: any[]) => void): this;
77 removeAllListeners(event?: string | symbol): this;
78 setMaxListeners(n: number): this;
79 getMaxListeners(): number;
80 listeners(event: string | symbol): Function[];
81 rawListeners(event: string | symbol): Function[];
82 emit(event: string | symbol, ...args: any[]): boolean;
83 listenerCount(event: string | symbol): number;
84 // Added in Node 6...
85 prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
86 prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
87 eventNames(): Array<string | symbol>;
88 }
89 }
90 }
91
92 export = EventEmitter;
93}