@types/node 24.10.2 → 25.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- node/README.md +1 -1
- node/assert/strict.d.ts +5 -11
- node/assert.d.ts +9 -169
- node/async_hooks.d.ts +8 -8
- node/buffer.buffer.d.ts +1 -7
- node/buffer.d.ts +44 -168
- node/child_process.d.ts +16 -64
- node/cluster.d.ts +240 -332
- node/console.d.ts +49 -351
- node/constants.d.ts +3 -4
- node/crypto.d.ts +279 -1631
- node/dgram.d.ts +15 -51
- node/diagnostics_channel.d.ts +4 -4
- node/dns/promises.d.ts +3 -3
- node/dns.d.ts +131 -132
- node/domain.d.ts +13 -17
- node/events.d.ts +719 -649
- node/fs/promises.d.ts +7 -8
- node/fs.d.ts +417 -455
- node/globals.d.ts +6 -26
- node/globals.typedarray.d.ts +60 -0
- node/http.d.ts +263 -254
- node/http2.d.ts +528 -804
- node/https.d.ts +59 -239
- node/index.d.ts +15 -1
- node/inspector/promises.d.ts +41 -0
- node/inspector.d.ts +6 -59
- node/inspector.generated.d.ts +3 -10
- node/module.d.ts +47 -122
- node/net.d.ts +63 -184
- node/os.d.ts +6 -6
- node/package.json +2 -2
- node/path/posix.d.ts +8 -0
- node/path/win32.d.ts +8 -0
- node/path.d.ts +120 -133
- node/perf_hooks.d.ts +282 -643
- node/process.d.ts +151 -128
- node/punycode.d.ts +5 -5
- node/querystring.d.ts +4 -4
- node/quic.d.ts +910 -0
- node/readline/promises.d.ts +3 -3
- node/readline.d.ts +67 -120
- node/repl.d.ts +75 -98
- node/sea.d.ts +1 -1
- node/sqlite.d.ts +2 -2
- node/stream/consumers.d.ts +10 -10
- node/stream/promises.d.ts +136 -15
- node/stream/web.d.ts +176 -453
- node/stream.d.ts +555 -478
- node/string_decoder.d.ts +4 -4
- node/test/reporters.d.ts +96 -0
- node/test.d.ts +80 -180
- node/timers/promises.d.ts +4 -4
- node/timers.d.ts +4 -130
- node/tls.d.ts +102 -177
- node/trace_events.d.ts +9 -9
- node/ts5.6/buffer.buffer.d.ts +1 -7
- node/ts5.6/index.d.ts +15 -1
- node/ts5.7/index.d.ts +15 -1
- node/tty.d.ts +58 -16
- node/url.d.ts +54 -592
- node/util/types.d.ts +558 -0
- node/util.d.ts +117 -792
- node/v8.d.ts +32 -5
- node/vm.d.ts +13 -13
- node/wasi.d.ts +4 -4
- node/web-globals/abortcontroller.d.ts +27 -2
- node/web-globals/blob.d.ts +23 -0
- node/web-globals/console.d.ts +9 -0
- node/web-globals/crypto.d.ts +7 -0
- node/web-globals/encoding.d.ts +11 -0
- node/web-globals/events.d.ts +9 -0
- node/web-globals/fetch.d.ts +4 -0
- node/web-globals/importmeta.d.ts +13 -0
- node/web-globals/messaging.d.ts +23 -0
- node/web-globals/performance.d.ts +45 -0
- node/web-globals/streams.d.ts +93 -0
- node/web-globals/timers.d.ts +44 -0
- node/web-globals/url.d.ts +24 -0
- node/worker_threads.d.ts +176 -358
- node/zlib.d.ts +8 -71
node/events.d.ts
CHANGED
|
@@ -32,58 +32,47 @@
|
|
|
32
32
|
* });
|
|
33
33
|
* myEmitter.emit('event');
|
|
34
34
|
* ```
|
|
35
|
-
* @see [source](https://github.com/nodejs/node/blob/
|
|
35
|
+
* @see [source](https://github.com/nodejs/node/blob/v25.x/lib/events.js)
|
|
36
36
|
*/
|
|
37
|
-
declare module "events" {
|
|
37
|
+
declare module "node:events" {
|
|
38
38
|
import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";
|
|
39
|
+
// #region Event map helpers
|
|
40
|
+
type EventMap<T> = Record<keyof T, any[]>;
|
|
41
|
+
type IfEventMap<Events extends EventMap<Events>, True, False> = {} extends Events ? False : True;
|
|
42
|
+
type Args<Events extends EventMap<Events>, EventName extends string | symbol> = IfEventMap<
|
|
43
|
+
Events,
|
|
44
|
+
EventName extends keyof Events ? Events[EventName]
|
|
45
|
+
: EventName extends keyof EventEmitterEventMap ? EventEmitterEventMap[EventName]
|
|
46
|
+
: any[],
|
|
47
|
+
any[]
|
|
48
|
+
>;
|
|
49
|
+
type EventNames<Events extends EventMap<Events>, EventName extends string | symbol> = IfEventMap<
|
|
50
|
+
Events,
|
|
51
|
+
EventName | (keyof Events & (string | symbol)) | keyof EventEmitterEventMap,
|
|
52
|
+
string | symbol
|
|
53
|
+
>;
|
|
54
|
+
type Listener<Events extends EventMap<Events>, EventName extends string | symbol> = IfEventMap<
|
|
55
|
+
Events,
|
|
56
|
+
(
|
|
57
|
+
...args: EventName extends keyof Events ? Events[EventName]
|
|
58
|
+
: EventName extends keyof EventEmitterEventMap ? EventEmitterEventMap[EventName]
|
|
59
|
+
: any[]
|
|
60
|
+
) => void,
|
|
61
|
+
(...args: any[]) => void
|
|
62
|
+
>;
|
|
63
|
+
interface EventEmitterEventMap {
|
|
64
|
+
newListener: [eventName: string | symbol, listener: (...args: any[]) => void];
|
|
65
|
+
removeListener: [eventName: string | symbol, listener: (...args: any[]) => void];
|
|
66
|
+
}
|
|
67
|
+
// #endregion
|
|
39
68
|
interface EventEmitterOptions {
|
|
40
69
|
/**
|
|
41
|
-
*
|
|
70
|
+
* It enables
|
|
71
|
+
* [automatic capturing of promise rejection](https://nodejs.org/docs/latest-v25.x/api/events.html#capture-rejections-of-promises).
|
|
72
|
+
* @default false
|
|
42
73
|
*/
|
|
43
74
|
captureRejections?: boolean | undefined;
|
|
44
75
|
}
|
|
45
|
-
interface StaticEventEmitterOptions {
|
|
46
|
-
/**
|
|
47
|
-
* Can be used to cancel awaiting events.
|
|
48
|
-
*/
|
|
49
|
-
signal?: AbortSignal | undefined;
|
|
50
|
-
}
|
|
51
|
-
interface StaticEventEmitterIteratorOptions extends StaticEventEmitterOptions {
|
|
52
|
-
/**
|
|
53
|
-
* Names of events that will end the iteration.
|
|
54
|
-
*/
|
|
55
|
-
close?: string[] | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* The high watermark. The emitter is paused every time the size of events being buffered is higher than it.
|
|
58
|
-
* Supported only on emitters implementing `pause()` and `resume()` methods.
|
|
59
|
-
* @default Number.MAX_SAFE_INTEGER
|
|
60
|
-
*/
|
|
61
|
-
highWaterMark?: number | undefined;
|
|
62
|
-
/**
|
|
63
|
-
* The low watermark. The emitter is resumed every time the size of events being buffered is lower than it.
|
|
64
|
-
* Supported only on emitters implementing `pause()` and `resume()` methods.
|
|
65
|
-
* @default 1
|
|
66
|
-
*/
|
|
67
|
-
lowWaterMark?: number | undefined;
|
|
68
|
-
}
|
|
69
|
-
interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
|
|
70
|
-
type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
|
|
71
|
-
type DefaultEventMap = [never];
|
|
72
|
-
type AnyRest = [...args: any[]];
|
|
73
|
-
type Args<K, T> = T extends DefaultEventMap ? AnyRest : (
|
|
74
|
-
K extends keyof T ? T[K] : never
|
|
75
|
-
);
|
|
76
|
-
type Key<K, T> = T extends DefaultEventMap ? string | symbol : K | keyof T;
|
|
77
|
-
type Key2<K, T> = T extends DefaultEventMap ? string | symbol : K & keyof T;
|
|
78
|
-
type Listener<K, T, F> = T extends DefaultEventMap ? F : (
|
|
79
|
-
K extends keyof T ? (
|
|
80
|
-
T[K] extends unknown[] ? (...args: T[K]) => void : never
|
|
81
|
-
)
|
|
82
|
-
: never
|
|
83
|
-
);
|
|
84
|
-
type Listener1<K, T> = Listener<K, T, (...args: any[]) => void>;
|
|
85
|
-
type Listener2<K, T> = Listener<K, T, Function>;
|
|
86
|
-
|
|
87
76
|
/**
|
|
88
77
|
* The `EventEmitter` class is defined and exposed by the `node:events` module:
|
|
89
78
|
*
|
|
@@ -97,96 +86,582 @@ declare module "events" {
|
|
|
97
86
|
* It supports the following option:
|
|
98
87
|
* @since v0.1.26
|
|
99
88
|
*/
|
|
100
|
-
class EventEmitter<T extends EventMap<T> =
|
|
89
|
+
class EventEmitter<T extends EventMap<T> = any> {
|
|
101
90
|
constructor(options?: EventEmitterOptions);
|
|
102
|
-
|
|
103
|
-
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
|
|
104
|
-
|
|
105
91
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
92
|
+
* The `Symbol.for('nodejs.rejection')` method is called in case a
|
|
93
|
+
* promise rejection happens when emitting an event and
|
|
94
|
+
* `captureRejections` is enabled on the emitter.
|
|
95
|
+
* It is possible to use `events.captureRejectionSymbol` in
|
|
96
|
+
* place of `Symbol.for('nodejs.rejection')`.
|
|
110
97
|
*
|
|
111
|
-
*
|
|
112
|
-
*
|
|
98
|
+
* ```js
|
|
99
|
+
* import { EventEmitter, captureRejectionSymbol } from 'node:events';
|
|
100
|
+
*
|
|
101
|
+
* class MyClass extends EventEmitter {
|
|
102
|
+
* constructor() {
|
|
103
|
+
* super({ captureRejections: true });
|
|
104
|
+
* }
|
|
105
|
+
*
|
|
106
|
+
* [captureRejectionSymbol](err, event, ...args) {
|
|
107
|
+
* console.log('rejection happened for', event, 'with', err, ...args);
|
|
108
|
+
* this.destroy(err);
|
|
109
|
+
* }
|
|
110
|
+
*
|
|
111
|
+
* destroy(err) {
|
|
112
|
+
* // Tear the resource down here.
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
* @since v13.4.0, v12.16.0
|
|
117
|
+
*/
|
|
118
|
+
[EventEmitter.captureRejectionSymbol]?(error: Error, event: string | symbol, ...args: any[]): void;
|
|
119
|
+
/**
|
|
120
|
+
* Alias for `emitter.on(eventName, listener)`.
|
|
121
|
+
* @since v0.1.26
|
|
122
|
+
*/
|
|
123
|
+
addListener<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
124
|
+
/**
|
|
125
|
+
* Synchronously calls each of the listeners registered for the event named
|
|
126
|
+
* `eventName`, in the order they were registered, passing the supplied arguments
|
|
127
|
+
* to each.
|
|
128
|
+
*
|
|
129
|
+
* Returns `true` if the event had listeners, `false` otherwise.
|
|
113
130
|
*
|
|
114
131
|
* ```js
|
|
115
|
-
* import {
|
|
116
|
-
*
|
|
132
|
+
* import { EventEmitter } from 'node:events';
|
|
133
|
+
* const myEmitter = new EventEmitter();
|
|
117
134
|
*
|
|
118
|
-
*
|
|
135
|
+
* // First listener
|
|
136
|
+
* myEmitter.on('event', function firstListener() {
|
|
137
|
+
* console.log('Helloooo! first listener');
|
|
138
|
+
* });
|
|
139
|
+
* // Second listener
|
|
140
|
+
* myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
141
|
+
* console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
142
|
+
* });
|
|
143
|
+
* // Third listener
|
|
144
|
+
* myEmitter.on('event', function thirdListener(...args) {
|
|
145
|
+
* const parameters = args.join(', ');
|
|
146
|
+
* console.log(`event with parameters ${parameters} in third listener`);
|
|
147
|
+
* });
|
|
119
148
|
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
149
|
+
* console.log(myEmitter.listeners('event'));
|
|
150
|
+
*
|
|
151
|
+
* myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
152
|
+
*
|
|
153
|
+
* // Prints:
|
|
154
|
+
* // [
|
|
155
|
+
* // [Function: firstListener],
|
|
156
|
+
* // [Function: secondListener],
|
|
157
|
+
* // [Function: thirdListener]
|
|
158
|
+
* // ]
|
|
159
|
+
* // Helloooo! first listener
|
|
160
|
+
* // event with parameters 1, 2 in second listener
|
|
161
|
+
* // event with parameters 1, 2, 3, 4, 5 in third listener
|
|
162
|
+
* ```
|
|
163
|
+
* @since v0.1.26
|
|
164
|
+
*/
|
|
165
|
+
emit<E extends string | symbol>(eventName: EventNames<T, E>, ...args: Args<T, E>): boolean;
|
|
166
|
+
/**
|
|
167
|
+
* Returns an array listing the events for which the emitter has registered
|
|
168
|
+
* listeners.
|
|
169
|
+
*
|
|
170
|
+
* ```js
|
|
171
|
+
* import { EventEmitter } from 'node:events';
|
|
172
|
+
*
|
|
173
|
+
* const myEE = new EventEmitter();
|
|
174
|
+
* myEE.on('foo', () => {});
|
|
175
|
+
* myEE.on('bar', () => {});
|
|
176
|
+
*
|
|
177
|
+
* const sym = Symbol('symbol');
|
|
178
|
+
* myEE.on(sym, () => {});
|
|
179
|
+
*
|
|
180
|
+
* console.log(myEE.eventNames());
|
|
181
|
+
* // Prints: [ 'foo', 'bar', Symbol(symbol) ]
|
|
182
|
+
* ```
|
|
183
|
+
* @since v6.0.0
|
|
184
|
+
*/
|
|
185
|
+
eventNames(): (string | symbol)[];
|
|
186
|
+
/**
|
|
187
|
+
* Returns the current max listener value for the `EventEmitter` which is either
|
|
188
|
+
* set by `emitter.setMaxListeners(n)` or defaults to
|
|
189
|
+
* `events.defaultMaxListeners`.
|
|
190
|
+
* @since v1.0.0
|
|
191
|
+
*/
|
|
192
|
+
getMaxListeners(): number;
|
|
193
|
+
/**
|
|
194
|
+
* Returns the number of listeners listening for the event named `eventName`.
|
|
195
|
+
* If `listener` is provided, it will return how many times the listener is found
|
|
196
|
+
* in the list of the listeners of the event.
|
|
197
|
+
* @since v3.2.0
|
|
198
|
+
* @param eventName The name of the event being listened for
|
|
199
|
+
* @param listener The event handler function
|
|
200
|
+
*/
|
|
201
|
+
listenerCount<E extends string | symbol>(eventName: EventNames<T, E>, listener?: Listener<T, E>): number;
|
|
202
|
+
/**
|
|
203
|
+
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
204
|
+
*
|
|
205
|
+
* ```js
|
|
206
|
+
* server.on('connection', (stream) => {
|
|
207
|
+
* console.log('someone connected!');
|
|
122
208
|
* });
|
|
209
|
+
* console.log(util.inspect(server.listeners('connection')));
|
|
210
|
+
* // Prints: [ [Function] ]
|
|
211
|
+
* ```
|
|
212
|
+
* @since v0.1.26
|
|
213
|
+
*/
|
|
214
|
+
listeners<E extends string | symbol>(eventName: EventNames<T, E>): Listener<T, E>[];
|
|
215
|
+
/**
|
|
216
|
+
* Alias for `emitter.removeListener()`.
|
|
217
|
+
* @since v10.0.0
|
|
218
|
+
*/
|
|
219
|
+
off<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
220
|
+
/**
|
|
221
|
+
* Adds the `listener` function to the end of the listeners array for the
|
|
222
|
+
* event named `eventName`. No checks are made to see if the `listener` has
|
|
223
|
+
* already been added. Multiple calls passing the same combination of `eventName`
|
|
224
|
+
* and `listener` will result in the `listener` being added, and called, multiple
|
|
225
|
+
* times.
|
|
123
226
|
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
227
|
+
* ```js
|
|
228
|
+
* server.on('connection', (stream) => {
|
|
229
|
+
* console.log('someone connected!');
|
|
230
|
+
* });
|
|
231
|
+
* ```
|
|
126
232
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
*
|
|
233
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
234
|
+
*
|
|
235
|
+
* By default, event listeners are invoked in the order they are added. The
|
|
236
|
+
* `emitter.prependListener()` method can be used as an alternative to add the
|
|
237
|
+
* event listener to the beginning of the listeners array.
|
|
238
|
+
*
|
|
239
|
+
* ```js
|
|
240
|
+
* import { EventEmitter } from 'node:events';
|
|
241
|
+
* const myEE = new EventEmitter();
|
|
242
|
+
* myEE.on('foo', () => console.log('a'));
|
|
243
|
+
* myEE.prependListener('foo', () => console.log('b'));
|
|
244
|
+
* myEE.emit('foo');
|
|
245
|
+
* // Prints:
|
|
246
|
+
* // b
|
|
247
|
+
* // a
|
|
248
|
+
* ```
|
|
249
|
+
* @since v0.1.101
|
|
250
|
+
* @param eventName The name of the event.
|
|
251
|
+
* @param listener The callback function
|
|
252
|
+
*/
|
|
253
|
+
on<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
254
|
+
/**
|
|
255
|
+
* Adds a **one-time** `listener` function for the event named `eventName`. The
|
|
256
|
+
* next time `eventName` is triggered, this listener is removed and then invoked.
|
|
257
|
+
*
|
|
258
|
+
* ```js
|
|
259
|
+
* server.once('connection', (stream) => {
|
|
260
|
+
* console.log('Ah, we have our first user!');
|
|
130
261
|
* });
|
|
262
|
+
* ```
|
|
131
263
|
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
264
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
265
|
+
*
|
|
266
|
+
* By default, event listeners are invoked in the order they are added. The
|
|
267
|
+
* `emitter.prependOnceListener()` method can be used as an alternative to add the
|
|
268
|
+
* event listener to the beginning of the listeners array.
|
|
269
|
+
*
|
|
270
|
+
* ```js
|
|
271
|
+
* import { EventEmitter } from 'node:events';
|
|
272
|
+
* const myEE = new EventEmitter();
|
|
273
|
+
* myEE.once('foo', () => console.log('a'));
|
|
274
|
+
* myEE.prependOnceListener('foo', () => console.log('b'));
|
|
275
|
+
* myEE.emit('foo');
|
|
276
|
+
* // Prints:
|
|
277
|
+
* // b
|
|
278
|
+
* // a
|
|
137
279
|
* ```
|
|
280
|
+
* @since v0.3.0
|
|
281
|
+
* @param eventName The name of the event.
|
|
282
|
+
* @param listener The callback function
|
|
283
|
+
*/
|
|
284
|
+
once<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
285
|
+
/**
|
|
286
|
+
* Adds the `listener` function to the _beginning_ of the listeners array for the
|
|
287
|
+
* event named `eventName`. No checks are made to see if the `listener` has
|
|
288
|
+
* already been added. Multiple calls passing the same combination of `eventName`
|
|
289
|
+
* and `listener` will result in the `listener` being added, and called, multiple
|
|
290
|
+
* times.
|
|
138
291
|
*
|
|
139
|
-
*
|
|
140
|
-
* '
|
|
141
|
-
*
|
|
292
|
+
* ```js
|
|
293
|
+
* server.prependListener('connection', (stream) => {
|
|
294
|
+
* console.log('someone connected!');
|
|
295
|
+
* });
|
|
296
|
+
* ```
|
|
297
|
+
*
|
|
298
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
299
|
+
* @since v6.0.0
|
|
300
|
+
* @param eventName The name of the event.
|
|
301
|
+
* @param listener The callback function
|
|
302
|
+
*/
|
|
303
|
+
prependListener<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
304
|
+
/**
|
|
305
|
+
* Adds a **one-time** `listener` function for the event named `eventName` to the
|
|
306
|
+
* _beginning_ of the listeners array. The next time `eventName` is triggered, this
|
|
307
|
+
* listener is removed, and then invoked.
|
|
142
308
|
*
|
|
143
309
|
* ```js
|
|
144
|
-
*
|
|
310
|
+
* server.prependOnceListener('connection', (stream) => {
|
|
311
|
+
* console.log('Ah, we have our first user!');
|
|
312
|
+
* });
|
|
313
|
+
* ```
|
|
145
314
|
*
|
|
146
|
-
*
|
|
315
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
316
|
+
* @since v6.0.0
|
|
317
|
+
* @param eventName The name of the event.
|
|
318
|
+
* @param listener The callback function
|
|
319
|
+
*/
|
|
320
|
+
prependOnceListener<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
321
|
+
/**
|
|
322
|
+
* Returns a copy of the array of listeners for the event named `eventName`,
|
|
323
|
+
* including any wrappers (such as those created by `.once()`).
|
|
147
324
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
325
|
+
* ```js
|
|
326
|
+
* import { EventEmitter } from 'node:events';
|
|
327
|
+
* const emitter = new EventEmitter();
|
|
328
|
+
* emitter.once('log', () => console.log('log once'));
|
|
151
329
|
*
|
|
152
|
-
*
|
|
330
|
+
* // Returns a new Array with a function `onceWrapper` which has a property
|
|
331
|
+
* // `listener` which contains the original listener bound above
|
|
332
|
+
* const listeners = emitter.rawListeners('log');
|
|
333
|
+
* const logFnWrapper = listeners[0];
|
|
153
334
|
*
|
|
154
|
-
* //
|
|
335
|
+
* // Logs "log once" to the console and does not unbind the `once` event
|
|
336
|
+
* logFnWrapper.listener();
|
|
337
|
+
*
|
|
338
|
+
* // Logs "log once" to the console and removes the listener
|
|
339
|
+
* logFnWrapper();
|
|
340
|
+
*
|
|
341
|
+
* emitter.on('log', () => console.log('log persistently'));
|
|
342
|
+
* // Will return a new Array with a single function bound by `.on()` above
|
|
343
|
+
* const newListeners = emitter.rawListeners('log');
|
|
344
|
+
*
|
|
345
|
+
* // Logs "log persistently" twice
|
|
346
|
+
* newListeners[0]();
|
|
347
|
+
* emitter.emit('log');
|
|
155
348
|
* ```
|
|
349
|
+
* @since v9.4.0
|
|
350
|
+
*/
|
|
351
|
+
rawListeners<E extends string | symbol>(eventName: EventNames<T, E>): Listener<T, E>[];
|
|
352
|
+
/**
|
|
353
|
+
* Removes all listeners, or those of the specified `eventName`.
|
|
156
354
|
*
|
|
157
|
-
*
|
|
355
|
+
* It is bad practice to remove listeners added elsewhere in the code,
|
|
356
|
+
* particularly when the `EventEmitter` instance was created by some other
|
|
357
|
+
* component or module (e.g. sockets or file streams).
|
|
358
|
+
*
|
|
359
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
360
|
+
* @since v0.1.26
|
|
361
|
+
*/
|
|
362
|
+
removeAllListeners<E extends string | symbol>(eventName?: EventNames<T, E>): this;
|
|
363
|
+
/**
|
|
364
|
+
* Removes the specified `listener` from the listener array for the event named
|
|
365
|
+
* `eventName`.
|
|
158
366
|
*
|
|
159
367
|
* ```js
|
|
160
|
-
*
|
|
368
|
+
* const callback = (stream) => {
|
|
369
|
+
* console.log('someone connected!');
|
|
370
|
+
* };
|
|
371
|
+
* server.on('connection', callback);
|
|
372
|
+
* // ...
|
|
373
|
+
* server.removeListener('connection', callback);
|
|
374
|
+
* ```
|
|
161
375
|
*
|
|
376
|
+
* `removeListener()` will remove, at most, one instance of a listener from the
|
|
377
|
+
* listener array. If any single listener has been added multiple times to the
|
|
378
|
+
* listener array for the specified `eventName`, then `removeListener()` must be
|
|
379
|
+
* called multiple times to remove each instance.
|
|
380
|
+
*
|
|
381
|
+
* Once an event is emitted, all listeners attached to it at the
|
|
382
|
+
* time of emitting are called in order. This implies that any
|
|
383
|
+
* `removeListener()` or `removeAllListeners()` calls _after_ emitting and
|
|
384
|
+
* _before_ the last listener finishes execution will not remove them from
|
|
385
|
+
* `emit()` in progress. Subsequent events behave as expected.
|
|
386
|
+
*
|
|
387
|
+
* ```js
|
|
388
|
+
* import { EventEmitter } from 'node:events';
|
|
389
|
+
* class MyEmitter extends EventEmitter {}
|
|
390
|
+
* const myEmitter = new MyEmitter();
|
|
391
|
+
*
|
|
392
|
+
* const callbackA = () => {
|
|
393
|
+
* console.log('A');
|
|
394
|
+
* myEmitter.removeListener('event', callbackB);
|
|
395
|
+
* };
|
|
396
|
+
*
|
|
397
|
+
* const callbackB = () => {
|
|
398
|
+
* console.log('B');
|
|
399
|
+
* };
|
|
400
|
+
*
|
|
401
|
+
* myEmitter.on('event', callbackA);
|
|
402
|
+
*
|
|
403
|
+
* myEmitter.on('event', callbackB);
|
|
404
|
+
*
|
|
405
|
+
* // callbackA removes listener callbackB but it will still be called.
|
|
406
|
+
* // Internal listener array at time of emit [callbackA, callbackB]
|
|
407
|
+
* myEmitter.emit('event');
|
|
408
|
+
* // Prints:
|
|
409
|
+
* // A
|
|
410
|
+
* // B
|
|
411
|
+
*
|
|
412
|
+
* // callbackB is now removed.
|
|
413
|
+
* // Internal listener array [callbackA]
|
|
414
|
+
* myEmitter.emit('event');
|
|
415
|
+
* // Prints:
|
|
416
|
+
* // A
|
|
417
|
+
* ```
|
|
418
|
+
*
|
|
419
|
+
* Because listeners are managed using an internal array, calling this will
|
|
420
|
+
* change the position indexes of any listener registered _after_ the listener
|
|
421
|
+
* being removed. This will not impact the order in which listeners are called,
|
|
422
|
+
* but it means that any copies of the listener array as returned by
|
|
423
|
+
* the `emitter.listeners()` method will need to be recreated.
|
|
424
|
+
*
|
|
425
|
+
* When a single function has been added as a handler multiple times for a single
|
|
426
|
+
* event (as in the example below), `removeListener()` will remove the most
|
|
427
|
+
* recently added instance. In the example the `once('ping')`
|
|
428
|
+
* listener is removed:
|
|
429
|
+
*
|
|
430
|
+
* ```js
|
|
431
|
+
* import { EventEmitter } from 'node:events';
|
|
162
432
|
* const ee = new EventEmitter();
|
|
163
|
-
* const ac = new AbortController();
|
|
164
433
|
*
|
|
165
|
-
*
|
|
434
|
+
* function pong() {
|
|
435
|
+
* console.log('pong');
|
|
436
|
+
* }
|
|
437
|
+
*
|
|
438
|
+
* ee.on('ping', pong);
|
|
439
|
+
* ee.once('ping', pong);
|
|
440
|
+
* ee.removeListener('ping', pong);
|
|
441
|
+
*
|
|
442
|
+
* ee.emit('ping');
|
|
443
|
+
* ee.emit('ping');
|
|
444
|
+
* ```
|
|
445
|
+
*
|
|
446
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
447
|
+
* @since v0.1.26
|
|
448
|
+
*/
|
|
449
|
+
removeListener<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this;
|
|
450
|
+
/**
|
|
451
|
+
* By default `EventEmitter`s will print a warning if more than `10` listeners are
|
|
452
|
+
* added for a particular event. This is a useful default that helps finding
|
|
453
|
+
* memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
|
|
454
|
+
* modified for this specific `EventEmitter` instance. The value can be set to
|
|
455
|
+
* `Infinity` (or `0`) to indicate an unlimited number of listeners.
|
|
456
|
+
*
|
|
457
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
458
|
+
* @since v0.3.5
|
|
459
|
+
*/
|
|
460
|
+
setMaxListeners(n: number): this;
|
|
461
|
+
}
|
|
462
|
+
namespace EventEmitter {
|
|
463
|
+
export { EventEmitter, EventEmitterEventMap, EventEmitterOptions };
|
|
464
|
+
}
|
|
465
|
+
namespace EventEmitter {
|
|
466
|
+
interface Abortable {
|
|
467
|
+
signal?: AbortSignal | undefined;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* See how to write a custom [rejection handler](https://nodejs.org/docs/latest-v25.x/api/events.html#emittersymbolfornodejsrejectionerr-eventname-args).
|
|
471
|
+
* @since v13.4.0, v12.16.0
|
|
472
|
+
*/
|
|
473
|
+
const captureRejectionSymbol: unique symbol;
|
|
474
|
+
/**
|
|
475
|
+
* Change the default `captureRejections` option on all new `EventEmitter` objects.
|
|
476
|
+
* @since v13.4.0, v12.16.0
|
|
477
|
+
*/
|
|
478
|
+
let captureRejections: boolean;
|
|
479
|
+
/**
|
|
480
|
+
* By default, a maximum of `10` listeners can be registered for any single
|
|
481
|
+
* event. This limit can be changed for individual `EventEmitter` instances
|
|
482
|
+
* using the `emitter.setMaxListeners(n)` method. To change the default
|
|
483
|
+
* for _all_ `EventEmitter` instances, the `events.defaultMaxListeners`
|
|
484
|
+
* property can be used. If this value is not a positive number, a `RangeError`
|
|
485
|
+
* is thrown.
|
|
486
|
+
*
|
|
487
|
+
* Take caution when setting the `events.defaultMaxListeners` because the
|
|
488
|
+
* change affects _all_ `EventEmitter` instances, including those created before
|
|
489
|
+
* the change is made. However, calling `emitter.setMaxListeners(n)` still has
|
|
490
|
+
* precedence over `events.defaultMaxListeners`.
|
|
491
|
+
*
|
|
492
|
+
* This is not a hard limit. The `EventEmitter` instance will allow
|
|
493
|
+
* more listeners to be added but will output a trace warning to stderr indicating
|
|
494
|
+
* that a "possible EventEmitter memory leak" has been detected. For any single
|
|
495
|
+
* `EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`
|
|
496
|
+
* methods can be used to temporarily avoid this warning:
|
|
497
|
+
*
|
|
498
|
+
* `defaultMaxListeners` has no effect on `AbortSignal` instances. While it is
|
|
499
|
+
* still possible to use `emitter.setMaxListeners(n)` to set a warning limit
|
|
500
|
+
* for individual `AbortSignal` instances, per default `AbortSignal` instances will not warn.
|
|
501
|
+
*
|
|
502
|
+
* ```js
|
|
503
|
+
* import { EventEmitter } from 'node:events';
|
|
504
|
+
* const emitter = new EventEmitter();
|
|
505
|
+
* emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
506
|
+
* emitter.once('event', () => {
|
|
507
|
+
* // do stuff
|
|
508
|
+
* emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
509
|
+
* });
|
|
510
|
+
* ```
|
|
511
|
+
*
|
|
512
|
+
* The `--trace-warnings` command-line flag can be used to display the
|
|
513
|
+
* stack trace for such warnings.
|
|
514
|
+
*
|
|
515
|
+
* The emitted warning can be inspected with `process.on('warning')` and will
|
|
516
|
+
* have the additional `emitter`, `type`, and `count` properties, referring to
|
|
517
|
+
* the event emitter instance, the event's name and the number of attached
|
|
518
|
+
* listeners, respectively.
|
|
519
|
+
* Its `name` property is set to `'MaxListenersExceededWarning'`.
|
|
520
|
+
* @since v0.11.2
|
|
521
|
+
*/
|
|
522
|
+
let defaultMaxListeners: number;
|
|
523
|
+
/**
|
|
524
|
+
* This symbol shall be used to install a listener for only monitoring `'error'`
|
|
525
|
+
* events. Listeners installed using this symbol are called before the regular
|
|
526
|
+
* `'error'` listeners are called.
|
|
527
|
+
*
|
|
528
|
+
* Installing a listener using this symbol does not change the behavior once an
|
|
529
|
+
* `'error'` event is emitted. Therefore, the process will still crash if no
|
|
530
|
+
* regular `'error'` listener is installed.
|
|
531
|
+
* @since v13.6.0, v12.17.0
|
|
532
|
+
*/
|
|
533
|
+
const errorMonitor: unique symbol;
|
|
534
|
+
/**
|
|
535
|
+
* Listens once to the `abort` event on the provided `signal`.
|
|
536
|
+
*
|
|
537
|
+
* Listening to the `abort` event on abort signals is unsafe and may
|
|
538
|
+
* lead to resource leaks since another third party with the signal can
|
|
539
|
+
* call `e.stopImmediatePropagation()`. Unfortunately Node.js cannot change
|
|
540
|
+
* this since it would violate the web standard. Additionally, the original
|
|
541
|
+
* API makes it easy to forget to remove listeners.
|
|
542
|
+
*
|
|
543
|
+
* This API allows safely using `AbortSignal`s in Node.js APIs by solving these
|
|
544
|
+
* two issues by listening to the event such that `stopImmediatePropagation` does
|
|
545
|
+
* not prevent the listener from running.
|
|
546
|
+
*
|
|
547
|
+
* Returns a disposable so that it may be unsubscribed from more easily.
|
|
548
|
+
*
|
|
549
|
+
* ```js
|
|
550
|
+
* import { addAbortListener } from 'node:events';
|
|
551
|
+
*
|
|
552
|
+
* function example(signal) {
|
|
553
|
+
* let disposable;
|
|
166
554
|
* try {
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
* console.error('There was an error', error.message);
|
|
174
|
-
* }
|
|
555
|
+
* signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
|
|
556
|
+
* disposable = addAbortListener(signal, (e) => {
|
|
557
|
+
* // Do something when signal is aborted.
|
|
558
|
+
* });
|
|
559
|
+
* } finally {
|
|
560
|
+
* disposable?.[Symbol.dispose]();
|
|
175
561
|
* }
|
|
176
562
|
* }
|
|
563
|
+
* ```
|
|
564
|
+
* @since v20.5.0
|
|
565
|
+
* @return Disposable that removes the `abort` listener.
|
|
566
|
+
*/
|
|
567
|
+
function addAbortListener(signal: AbortSignal, resource: (event: Event) => void): Disposable;
|
|
568
|
+
/**
|
|
569
|
+
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
570
|
+
*
|
|
571
|
+
* For `EventEmitter`s this behaves exactly the same as calling `.listeners` on
|
|
572
|
+
* the emitter.
|
|
573
|
+
*
|
|
574
|
+
* For `EventTarget`s this is the only way to get the event listeners for the
|
|
575
|
+
* event target. This is useful for debugging and diagnostic purposes.
|
|
576
|
+
*
|
|
577
|
+
* ```js
|
|
578
|
+
* import { getEventListeners, EventEmitter } from 'node:events';
|
|
579
|
+
*
|
|
580
|
+
* {
|
|
581
|
+
* const ee = new EventEmitter();
|
|
582
|
+
* const listener = () => console.log('Events are fun');
|
|
583
|
+
* ee.on('foo', listener);
|
|
584
|
+
* console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
|
|
585
|
+
* }
|
|
586
|
+
* {
|
|
587
|
+
* const et = new EventTarget();
|
|
588
|
+
* const listener = () => console.log('Events are fun');
|
|
589
|
+
* et.addEventListener('foo', listener);
|
|
590
|
+
* console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
|
|
591
|
+
* }
|
|
592
|
+
* ```
|
|
593
|
+
* @since v15.2.0, v14.17.0
|
|
594
|
+
*/
|
|
595
|
+
function getEventListeners(emitter: NodeJS.EventEmitter, name: string | symbol): ((...args: any[]) => void)[];
|
|
596
|
+
function getEventListeners(emitter: EventTarget, name: string): ((...args: any[]) => void)[];
|
|
597
|
+
/**
|
|
598
|
+
* Returns the currently set max amount of listeners.
|
|
599
|
+
*
|
|
600
|
+
* For `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on
|
|
601
|
+
* the emitter.
|
|
602
|
+
*
|
|
603
|
+
* For `EventTarget`s this is the only way to get the max event listeners for the
|
|
604
|
+
* event target. If the number of event handlers on a single EventTarget exceeds
|
|
605
|
+
* the max set, the EventTarget will print a warning.
|
|
606
|
+
*
|
|
607
|
+
* ```js
|
|
608
|
+
* import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';
|
|
609
|
+
*
|
|
610
|
+
* {
|
|
611
|
+
* const ee = new EventEmitter();
|
|
612
|
+
* console.log(getMaxListeners(ee)); // 10
|
|
613
|
+
* setMaxListeners(11, ee);
|
|
614
|
+
* console.log(getMaxListeners(ee)); // 11
|
|
615
|
+
* }
|
|
616
|
+
* {
|
|
617
|
+
* const et = new EventTarget();
|
|
618
|
+
* console.log(getMaxListeners(et)); // 10
|
|
619
|
+
* setMaxListeners(11, et);
|
|
620
|
+
* console.log(getMaxListeners(et)); // 11
|
|
621
|
+
* }
|
|
622
|
+
* ```
|
|
623
|
+
* @since v19.9.0
|
|
624
|
+
*/
|
|
625
|
+
function getMaxListeners(emitter: NodeJS.EventEmitter | EventTarget): number;
|
|
626
|
+
/**
|
|
627
|
+
* A class method that returns the number of listeners for the given `eventName`
|
|
628
|
+
* registered on the given `emitter`.
|
|
177
629
|
*
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
630
|
+
* ```js
|
|
631
|
+
* import { EventEmitter, listenerCount } from 'node:events';
|
|
632
|
+
*
|
|
633
|
+
* const myEmitter = new EventEmitter();
|
|
634
|
+
* myEmitter.on('event', () => {});
|
|
635
|
+
* myEmitter.on('event', () => {});
|
|
636
|
+
* console.log(listenerCount(myEmitter, 'event'));
|
|
637
|
+
* // Prints: 2
|
|
181
638
|
* ```
|
|
182
|
-
* @since
|
|
639
|
+
* @since v0.9.12
|
|
640
|
+
* @deprecated Use `emitter.listenerCount()` instead.
|
|
641
|
+
* @param emitter The emitter to query
|
|
642
|
+
* @param eventName The event name
|
|
183
643
|
*/
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
644
|
+
function listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
|
|
645
|
+
interface OnOptions extends Abortable {
|
|
646
|
+
/**
|
|
647
|
+
* Names of events that will end the iteration.
|
|
648
|
+
*/
|
|
649
|
+
close?: readonly string[] | undefined;
|
|
650
|
+
/**
|
|
651
|
+
* The high watermark. The emitter is paused every time the size of events
|
|
652
|
+
* being buffered is higher than it. Supported only on emitters implementing
|
|
653
|
+
* `pause()` and `resume()` methods.
|
|
654
|
+
* @default Number.MAX_SAFE_INTEGER
|
|
655
|
+
*/
|
|
656
|
+
highWaterMark?: number | undefined;
|
|
657
|
+
/**
|
|
658
|
+
* The low watermark. The emitter is resumed every time the size of events
|
|
659
|
+
* being buffered is lower than it. Supported only on emitters implementing
|
|
660
|
+
* `pause()` and `resume()` methods.
|
|
661
|
+
* @default 1
|
|
662
|
+
*/
|
|
663
|
+
lowWaterMark?: number | undefined;
|
|
664
|
+
}
|
|
190
665
|
/**
|
|
191
666
|
* ```js
|
|
192
667
|
* import { on, EventEmitter } from 'node:events';
|
|
@@ -242,116 +717,106 @@ declare module "events" {
|
|
|
242
717
|
*
|
|
243
718
|
* process.nextTick(() => ac.abort());
|
|
244
719
|
* ```
|
|
245
|
-
*
|
|
246
|
-
* Use the `close` option to specify an array of event names that will end the iteration:
|
|
247
|
-
*
|
|
248
|
-
* ```js
|
|
249
|
-
* import { on, EventEmitter } from 'node:events';
|
|
250
|
-
* import process from 'node:process';
|
|
251
|
-
*
|
|
252
|
-
* const ee = new EventEmitter();
|
|
253
|
-
*
|
|
254
|
-
* // Emit later on
|
|
255
|
-
* process.nextTick(() => {
|
|
256
|
-
* ee.emit('foo', 'bar');
|
|
257
|
-
* ee.emit('foo', 42);
|
|
258
|
-
* ee.emit('close');
|
|
259
|
-
* });
|
|
260
|
-
*
|
|
261
|
-
* for await (const event of on(ee, 'foo', { close: ['close'] })) {
|
|
262
|
-
* console.log(event); // prints ['bar'] [42]
|
|
263
|
-
* }
|
|
264
|
-
* // the loop will exit after 'close' is emitted
|
|
265
|
-
* console.log('done'); // prints 'done'
|
|
266
|
-
* ```
|
|
267
720
|
* @since v13.6.0, v12.16.0
|
|
268
|
-
* @
|
|
721
|
+
* @returns `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
|
|
269
722
|
*/
|
|
270
|
-
|
|
723
|
+
function on(
|
|
271
724
|
emitter: NodeJS.EventEmitter,
|
|
272
725
|
eventName: string | symbol,
|
|
273
|
-
options?:
|
|
726
|
+
options?: OnOptions,
|
|
274
727
|
): NodeJS.AsyncIterator<any[]>;
|
|
275
|
-
|
|
728
|
+
function on(
|
|
276
729
|
emitter: EventTarget,
|
|
277
730
|
eventName: string,
|
|
278
|
-
options?:
|
|
731
|
+
options?: OnOptions,
|
|
279
732
|
): NodeJS.AsyncIterator<any[]>;
|
|
733
|
+
interface OnceOptions extends Abortable {}
|
|
280
734
|
/**
|
|
281
|
-
*
|
|
735
|
+
* Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
|
|
736
|
+
* event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
|
|
737
|
+
* The `Promise` will resolve with an array of all the arguments emitted to the
|
|
738
|
+
* given event.
|
|
739
|
+
*
|
|
740
|
+
* This method is intentionally generic and works with the web platform
|
|
741
|
+
* [EventTarget][WHATWG-EventTarget] interface, which has no special
|
|
742
|
+
* `'error'` event semantics and does not listen to the `'error'` event.
|
|
282
743
|
*
|
|
283
744
|
* ```js
|
|
284
|
-
* import {
|
|
745
|
+
* import { once, EventEmitter } from 'node:events';
|
|
746
|
+
* import process from 'node:process';
|
|
285
747
|
*
|
|
286
|
-
* const
|
|
287
|
-
* myEmitter.on('event', () => {});
|
|
288
|
-
* myEmitter.on('event', () => {});
|
|
289
|
-
* console.log(listenerCount(myEmitter, 'event'));
|
|
290
|
-
* // Prints: 2
|
|
291
|
-
* ```
|
|
292
|
-
* @since v0.9.12
|
|
293
|
-
* @deprecated Since v3.2.0 - Use `listenerCount` instead.
|
|
294
|
-
* @param emitter The emitter to query
|
|
295
|
-
* @param eventName The event name
|
|
296
|
-
*/
|
|
297
|
-
static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
|
|
298
|
-
/**
|
|
299
|
-
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
748
|
+
* const ee = new EventEmitter();
|
|
300
749
|
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
750
|
+
* process.nextTick(() => {
|
|
751
|
+
* ee.emit('myevent', 42);
|
|
752
|
+
* });
|
|
303
753
|
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
754
|
+
* const [value] = await once(ee, 'myevent');
|
|
755
|
+
* console.log(value);
|
|
306
756
|
*
|
|
307
|
-
*
|
|
308
|
-
*
|
|
757
|
+
* const err = new Error('kaboom');
|
|
758
|
+
* process.nextTick(() => {
|
|
759
|
+
* ee.emit('error', err);
|
|
760
|
+
* });
|
|
309
761
|
*
|
|
310
|
-
* {
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
314
|
-
* console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
|
|
315
|
-
* }
|
|
316
|
-
* {
|
|
317
|
-
* const et = new EventTarget();
|
|
318
|
-
* const listener = () => console.log('Events are fun');
|
|
319
|
-
* et.addEventListener('foo', listener);
|
|
320
|
-
* console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
|
|
762
|
+
* try {
|
|
763
|
+
* await once(ee, 'myevent');
|
|
764
|
+
* } catch (err) {
|
|
765
|
+
* console.error('error happened', err);
|
|
321
766
|
* }
|
|
322
767
|
* ```
|
|
323
|
-
* @since v15.2.0, v14.17.0
|
|
324
|
-
*/
|
|
325
|
-
static getEventListeners(emitter: EventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
|
|
326
|
-
/**
|
|
327
|
-
* Returns the currently set max amount of listeners.
|
|
328
768
|
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
769
|
+
* The special handling of the `'error'` event is only used when `events.once()`
|
|
770
|
+
* is used to wait for another event. If `events.once()` is used to wait for the
|
|
771
|
+
* '`error'` event itself, then it is treated as any other kind of event without
|
|
772
|
+
* special handling:
|
|
331
773
|
*
|
|
332
|
-
*
|
|
333
|
-
*
|
|
334
|
-
*
|
|
774
|
+
* ```js
|
|
775
|
+
* import { EventEmitter, once } from 'node:events';
|
|
776
|
+
*
|
|
777
|
+
* const ee = new EventEmitter();
|
|
778
|
+
*
|
|
779
|
+
* once(ee, 'error')
|
|
780
|
+
* .then(([err]) => console.log('ok', err.message))
|
|
781
|
+
* .catch((err) => console.error('error', err.message));
|
|
782
|
+
*
|
|
783
|
+
* ee.emit('error', new Error('boom'));
|
|
784
|
+
*
|
|
785
|
+
* // Prints: ok boom
|
|
786
|
+
* ```
|
|
787
|
+
*
|
|
788
|
+
* An `AbortSignal` can be used to cancel waiting for the event:
|
|
335
789
|
*
|
|
336
790
|
* ```js
|
|
337
|
-
* import {
|
|
791
|
+
* import { EventEmitter, once } from 'node:events';
|
|
338
792
|
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
* }
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
793
|
+
* const ee = new EventEmitter();
|
|
794
|
+
* const ac = new AbortController();
|
|
795
|
+
*
|
|
796
|
+
* async function foo(emitter, event, signal) {
|
|
797
|
+
* try {
|
|
798
|
+
* await once(emitter, event, { signal });
|
|
799
|
+
* console.log('event emitted!');
|
|
800
|
+
* } catch (error) {
|
|
801
|
+
* if (error.name === 'AbortError') {
|
|
802
|
+
* console.error('Waiting for the event was canceled!');
|
|
803
|
+
* } else {
|
|
804
|
+
* console.error('There was an error', error.message);
|
|
805
|
+
* }
|
|
806
|
+
* }
|
|
350
807
|
* }
|
|
808
|
+
*
|
|
809
|
+
* foo(ee, 'foo', ac.signal);
|
|
810
|
+
* ac.abort(); // Prints: Waiting for the event was canceled!
|
|
351
811
|
* ```
|
|
352
|
-
* @since
|
|
812
|
+
* @since v11.13.0, v10.16.0
|
|
353
813
|
*/
|
|
354
|
-
|
|
814
|
+
function once(
|
|
815
|
+
emitter: NodeJS.EventEmitter,
|
|
816
|
+
eventName: string | symbol,
|
|
817
|
+
options?: OnceOptions,
|
|
818
|
+
): Promise<any[]>;
|
|
819
|
+
function once(emitter: EventTarget, eventName: string, options?: OnceOptions): Promise<any[]>;
|
|
355
820
|
/**
|
|
356
821
|
* ```js
|
|
357
822
|
* import { setMaxListeners, EventEmitter } from 'node:events';
|
|
@@ -363,134 +828,68 @@ declare module "events" {
|
|
|
363
828
|
* ```
|
|
364
829
|
* @since v15.4.0
|
|
365
830
|
* @param n A non-negative number. The maximum number of listeners per `EventTarget` event.
|
|
366
|
-
* @param eventTargets Zero or more
|
|
831
|
+
* @param eventTargets Zero or more `EventTarget`
|
|
832
|
+
* or `EventEmitter` instances. If none are specified, `n` is set as the default
|
|
833
|
+
* max for all newly created `EventTarget` and `EventEmitter` objects.
|
|
367
834
|
* objects.
|
|
368
835
|
*/
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Listens once to the `abort` event on the provided `signal`.
|
|
372
|
-
*
|
|
373
|
-
* Listening to the `abort` event on abort signals is unsafe and may
|
|
374
|
-
* lead to resource leaks since another third party with the signal can
|
|
375
|
-
* call `e.stopImmediatePropagation()`. Unfortunately Node.js cannot change
|
|
376
|
-
* this since it would violate the web standard. Additionally, the original
|
|
377
|
-
* API makes it easy to forget to remove listeners.
|
|
378
|
-
*
|
|
379
|
-
* This API allows safely using `AbortSignal`s in Node.js APIs by solving these
|
|
380
|
-
* two issues by listening to the event such that `stopImmediatePropagation` does
|
|
381
|
-
* not prevent the listener from running.
|
|
382
|
-
*
|
|
383
|
-
* Returns a disposable so that it may be unsubscribed from more easily.
|
|
384
|
-
*
|
|
385
|
-
* ```js
|
|
386
|
-
* import { addAbortListener } from 'node:events';
|
|
387
|
-
*
|
|
388
|
-
* function example(signal) {
|
|
389
|
-
* let disposable;
|
|
390
|
-
* try {
|
|
391
|
-
* signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
|
|
392
|
-
* disposable = addAbortListener(signal, (e) => {
|
|
393
|
-
* // Do something when signal is aborted.
|
|
394
|
-
* });
|
|
395
|
-
* } finally {
|
|
396
|
-
* disposable?.[Symbol.dispose]();
|
|
397
|
-
* }
|
|
398
|
-
* }
|
|
399
|
-
* ```
|
|
400
|
-
* @since v20.5.0
|
|
401
|
-
* @return Disposable that removes the `abort` listener.
|
|
402
|
-
*/
|
|
403
|
-
static addAbortListener(signal: AbortSignal, resource: (event: Event) => void): Disposable;
|
|
404
|
-
/**
|
|
405
|
-
* This symbol shall be used to install a listener for only monitoring `'error'` events. Listeners installed using this symbol are called before the regular `'error'` listeners are called.
|
|
406
|
-
*
|
|
407
|
-
* Installing a listener using this symbol does not change the behavior once an `'error'` event is emitted. Therefore, the process will still crash if no
|
|
408
|
-
* regular `'error'` listener is installed.
|
|
409
|
-
* @since v13.6.0, v12.17.0
|
|
410
|
-
*/
|
|
411
|
-
static readonly errorMonitor: unique symbol;
|
|
412
|
-
/**
|
|
413
|
-
* Value: `Symbol.for('nodejs.rejection')`
|
|
414
|
-
*
|
|
415
|
-
* See how to write a custom `rejection handler`.
|
|
416
|
-
* @since v13.4.0, v12.16.0
|
|
417
|
-
*/
|
|
418
|
-
static readonly captureRejectionSymbol: unique symbol;
|
|
419
|
-
/**
|
|
420
|
-
* Value: [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
|
|
421
|
-
*
|
|
422
|
-
* Change the default `captureRejections` option on all new `EventEmitter` objects.
|
|
423
|
-
* @since v13.4.0, v12.16.0
|
|
424
|
-
*/
|
|
425
|
-
static captureRejections: boolean;
|
|
836
|
+
function setMaxListeners(n: number, ...eventTargets: ReadonlyArray<NodeJS.EventEmitter | EventTarget>): void;
|
|
426
837
|
/**
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
* the
|
|
436
|
-
*
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
* that a "possible EventEmitter memory leak" has been detected. For any single
|
|
441
|
-
* `EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()` methods can be used to
|
|
442
|
-
* temporarily avoid this warning:
|
|
443
|
-
*
|
|
444
|
-
* ```js
|
|
445
|
-
* import { EventEmitter } from 'node:events';
|
|
446
|
-
* const emitter = new EventEmitter();
|
|
447
|
-
* emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
448
|
-
* emitter.once('event', () => {
|
|
449
|
-
* // do stuff
|
|
450
|
-
* emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
451
|
-
* });
|
|
452
|
-
* ```
|
|
453
|
-
*
|
|
454
|
-
* The `--trace-warnings` command-line flag can be used to display the
|
|
455
|
-
* stack trace for such warnings.
|
|
456
|
-
*
|
|
457
|
-
* The emitted warning can be inspected with `process.on('warning')` and will
|
|
458
|
-
* have the additional `emitter`, `type`, and `count` properties, referring to
|
|
459
|
-
* the event emitter instance, the event's name and the number of attached
|
|
460
|
-
* listeners, respectively.
|
|
461
|
-
* Its `name` property is set to `'MaxListenersExceededWarning'`.
|
|
462
|
-
* @since v0.11.2
|
|
838
|
+
* This is the interface from which event-emitting Node.js APIs inherit in the types package.
|
|
839
|
+
* **It is not intended for consumer use.**
|
|
840
|
+
*
|
|
841
|
+
* It provides event-mapped definitions similar to EventEmitter, except that its signatures
|
|
842
|
+
* are deliberately permissive: they provide type _hinting_, but not rigid type-checking,
|
|
843
|
+
* for compatibility reasons.
|
|
844
|
+
*
|
|
845
|
+
* Classes that inherit directly from EventEmitter in JavaScript can inherit directly from
|
|
846
|
+
* this interface in the type definitions. Classes that are more than one inheritance level
|
|
847
|
+
* away from EventEmitter (eg. `net.Socket` > `stream.Duplex` > `EventEmitter`) must instead
|
|
848
|
+
* copy these method definitions into the derived class. Search "#region InternalEventEmitter"
|
|
849
|
+
* for examples.
|
|
850
|
+
* @internal
|
|
463
851
|
*/
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
852
|
+
interface InternalEventEmitter<T extends EventMap<T>> extends EventEmitter {
|
|
853
|
+
addListener<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
854
|
+
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
855
|
+
emit<E extends keyof T>(eventName: E, ...args: T[E]): boolean;
|
|
856
|
+
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
857
|
+
listenerCount<E extends keyof T>(eventName: E, listener?: (...args: T[E]) => void): number;
|
|
858
|
+
listenerCount(eventName: string | symbol, listener?: (...args: any[]) => void): number;
|
|
859
|
+
listeners<E extends keyof T>(eventName: E): ((...args: T[E]) => void)[];
|
|
860
|
+
listeners(eventName: string | symbol): ((...args: any[]) => void)[];
|
|
861
|
+
off<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
862
|
+
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
863
|
+
on<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
864
|
+
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
865
|
+
once<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
866
|
+
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
867
|
+
prependListener<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
868
|
+
prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
869
|
+
prependOnceListener<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
870
|
+
prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
871
|
+
rawListeners<E extends keyof T>(eventName: E): ((...args: T[E]) => void)[];
|
|
872
|
+
rawListeners(eventName: string | symbol): ((...args: any[]) => void)[];
|
|
873
|
+
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
|
874
|
+
removeAllListeners<E extends keyof T>(eventName?: E): this;
|
|
875
|
+
removeAllListeners(eventName?: string | symbol): this;
|
|
876
|
+
removeListener<E extends keyof T>(eventName: E, listener: (...args: T[E]) => void): this;
|
|
877
|
+
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
475
878
|
}
|
|
476
|
-
|
|
477
|
-
export interface EventEmitterReferencingAsyncResource extends AsyncResource {
|
|
879
|
+
interface EventEmitterReferencingAsyncResource extends AsyncResource {
|
|
478
880
|
readonly eventEmitter: EventEmitterAsyncResource;
|
|
479
881
|
}
|
|
480
|
-
|
|
481
|
-
export interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {
|
|
882
|
+
interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {
|
|
482
883
|
/**
|
|
483
|
-
* The type of async event
|
|
484
|
-
*
|
|
485
|
-
* @default new.target.name if instantiated as a child class.
|
|
884
|
+
* The type of async event.
|
|
885
|
+
* @default new.target.name
|
|
486
886
|
*/
|
|
487
887
|
name?: string | undefined;
|
|
488
888
|
}
|
|
489
|
-
|
|
490
889
|
/**
|
|
491
890
|
* Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that
|
|
492
891
|
* require manual async tracking. Specifically, all events emitted by instances
|
|
493
|
-
* of `events.EventEmitterAsyncResource` will run within its
|
|
892
|
+
* of `events.EventEmitterAsyncResource` will run within its [async context](https://nodejs.org/docs/latest-v25.x/api/async_context.html).
|
|
494
893
|
*
|
|
495
894
|
* ```js
|
|
496
895
|
* import { EventEmitterAsyncResource, EventEmitter } from 'node:events';
|
|
@@ -525,11 +924,17 @@ declare module "events" {
|
|
|
525
924
|
* same options as `EventEmitter` and `AsyncResource` themselves.
|
|
526
925
|
* @since v17.4.0, v16.14.0
|
|
527
926
|
*/
|
|
528
|
-
|
|
927
|
+
class EventEmitterAsyncResource extends EventEmitter {
|
|
928
|
+
constructor(options?: EventEmitterAsyncResourceOptions);
|
|
529
929
|
/**
|
|
530
|
-
*
|
|
930
|
+
* The unique `asyncId` assigned to the resource.
|
|
531
931
|
*/
|
|
532
|
-
|
|
932
|
+
readonly asyncId: number;
|
|
933
|
+
/**
|
|
934
|
+
* The returned `AsyncResource` object has an additional `eventEmitter` property
|
|
935
|
+
* that provides a reference to this `EventEmitterAsyncResource`.
|
|
936
|
+
*/
|
|
937
|
+
readonly asyncResource: EventEmitterReferencingAsyncResource;
|
|
533
938
|
/**
|
|
534
939
|
* Call all `destroy` hooks. This should only ever be called once. An error will
|
|
535
940
|
* be thrown if it is called more than once. This **must** be manually called. If
|
|
@@ -538,25 +943,17 @@ declare module "events" {
|
|
|
538
943
|
*/
|
|
539
944
|
emitDestroy(): void;
|
|
540
945
|
/**
|
|
541
|
-
* The
|
|
542
|
-
|
|
543
|
-
readonly asyncId: number;
|
|
544
|
-
/**
|
|
545
|
-
* The same triggerAsyncId that is passed to the AsyncResource constructor.
|
|
946
|
+
* The same `triggerAsyncId` that is passed to the
|
|
947
|
+
* `AsyncResource` constructor.
|
|
546
948
|
*/
|
|
547
949
|
readonly triggerAsyncId: number;
|
|
548
|
-
/**
|
|
549
|
-
* The returned `AsyncResource` object has an additional `eventEmitter` property
|
|
550
|
-
* that provides a reference to this `EventEmitterAsyncResource`.
|
|
551
|
-
*/
|
|
552
|
-
readonly asyncResource: EventEmitterReferencingAsyncResource;
|
|
553
950
|
}
|
|
554
951
|
/**
|
|
555
952
|
* The `NodeEventTarget` is a Node.js-specific extension to `EventTarget`
|
|
556
953
|
* that emulates a subset of the `EventEmitter` API.
|
|
557
954
|
* @since v14.5.0
|
|
558
955
|
*/
|
|
559
|
-
|
|
956
|
+
interface NodeEventTarget extends EventTarget {
|
|
560
957
|
/**
|
|
561
958
|
* Node.js-specific extension to the `EventTarget` class that emulates the
|
|
562
959
|
* equivalent `EventEmitter` API. The only difference between `addListener()` and
|
|
@@ -630,347 +1027,20 @@ declare module "events" {
|
|
|
630
1027
|
*/
|
|
631
1028
|
removeListener(type: string, listener: (arg: any) => void, options?: EventListenerOptions): this;
|
|
632
1029
|
}
|
|
1030
|
+
/** @internal */
|
|
1031
|
+
type InternalEventTargetEventProperties<T> = {
|
|
1032
|
+
[K in keyof T & string as `on${K}`]: ((ev: T[K]) => void) | null;
|
|
1033
|
+
};
|
|
633
1034
|
}
|
|
634
1035
|
global {
|
|
1036
|
+
import _ = EventEmitter;
|
|
635
1037
|
namespace NodeJS {
|
|
636
|
-
interface EventEmitter<T extends EventMap<T> =
|
|
637
|
-
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
|
|
638
|
-
/**
|
|
639
|
-
* Alias for `emitter.on(eventName, listener)`.
|
|
640
|
-
* @since v0.1.26
|
|
641
|
-
*/
|
|
642
|
-
addListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
643
|
-
/**
|
|
644
|
-
* Adds the `listener` function to the end of the listeners array for the event
|
|
645
|
-
* named `eventName`. No checks are made to see if the `listener` has already
|
|
646
|
-
* been added. Multiple calls passing the same combination of `eventName` and
|
|
647
|
-
* `listener` will result in the `listener` being added, and called, multiple times.
|
|
648
|
-
*
|
|
649
|
-
* ```js
|
|
650
|
-
* server.on('connection', (stream) => {
|
|
651
|
-
* console.log('someone connected!');
|
|
652
|
-
* });
|
|
653
|
-
* ```
|
|
654
|
-
*
|
|
655
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
656
|
-
*
|
|
657
|
-
* By default, event listeners are invoked in the order they are added. The `emitter.prependListener()` method can be used as an alternative to add the
|
|
658
|
-
* event listener to the beginning of the listeners array.
|
|
659
|
-
*
|
|
660
|
-
* ```js
|
|
661
|
-
* import { EventEmitter } from 'node:events';
|
|
662
|
-
* const myEE = new EventEmitter();
|
|
663
|
-
* myEE.on('foo', () => console.log('a'));
|
|
664
|
-
* myEE.prependListener('foo', () => console.log('b'));
|
|
665
|
-
* myEE.emit('foo');
|
|
666
|
-
* // Prints:
|
|
667
|
-
* // b
|
|
668
|
-
* // a
|
|
669
|
-
* ```
|
|
670
|
-
* @since v0.1.101
|
|
671
|
-
* @param eventName The name of the event.
|
|
672
|
-
* @param listener The callback function
|
|
673
|
-
*/
|
|
674
|
-
on<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
675
|
-
/**
|
|
676
|
-
* Adds a **one-time** `listener` function for the event named `eventName`. The
|
|
677
|
-
* next time `eventName` is triggered, this listener is removed and then invoked.
|
|
678
|
-
*
|
|
679
|
-
* ```js
|
|
680
|
-
* server.once('connection', (stream) => {
|
|
681
|
-
* console.log('Ah, we have our first user!');
|
|
682
|
-
* });
|
|
683
|
-
* ```
|
|
684
|
-
*
|
|
685
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
686
|
-
*
|
|
687
|
-
* By default, event listeners are invoked in the order they are added. The `emitter.prependOnceListener()` method can be used as an alternative to add the
|
|
688
|
-
* event listener to the beginning of the listeners array.
|
|
689
|
-
*
|
|
690
|
-
* ```js
|
|
691
|
-
* import { EventEmitter } from 'node:events';
|
|
692
|
-
* const myEE = new EventEmitter();
|
|
693
|
-
* myEE.once('foo', () => console.log('a'));
|
|
694
|
-
* myEE.prependOnceListener('foo', () => console.log('b'));
|
|
695
|
-
* myEE.emit('foo');
|
|
696
|
-
* // Prints:
|
|
697
|
-
* // b
|
|
698
|
-
* // a
|
|
699
|
-
* ```
|
|
700
|
-
* @since v0.3.0
|
|
701
|
-
* @param eventName The name of the event.
|
|
702
|
-
* @param listener The callback function
|
|
703
|
-
*/
|
|
704
|
-
once<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
705
|
-
/**
|
|
706
|
-
* Removes the specified `listener` from the listener array for the event named `eventName`.
|
|
707
|
-
*
|
|
708
|
-
* ```js
|
|
709
|
-
* const callback = (stream) => {
|
|
710
|
-
* console.log('someone connected!');
|
|
711
|
-
* };
|
|
712
|
-
* server.on('connection', callback);
|
|
713
|
-
* // ...
|
|
714
|
-
* server.removeListener('connection', callback);
|
|
715
|
-
* ```
|
|
716
|
-
*
|
|
717
|
-
* `removeListener()` will remove, at most, one instance of a listener from the
|
|
718
|
-
* listener array. If any single listener has been added multiple times to the
|
|
719
|
-
* listener array for the specified `eventName`, then `removeListener()` must be
|
|
720
|
-
* called multiple times to remove each instance.
|
|
721
|
-
*
|
|
722
|
-
* Once an event is emitted, all listeners attached to it at the
|
|
723
|
-
* time of emitting are called in order. This implies that any `removeListener()` or `removeAllListeners()` calls _after_ emitting and _before_ the last listener finishes execution
|
|
724
|
-
* will not remove them from`emit()` in progress. Subsequent events behave as expected.
|
|
725
|
-
*
|
|
726
|
-
* ```js
|
|
727
|
-
* import { EventEmitter } from 'node:events';
|
|
728
|
-
* class MyEmitter extends EventEmitter {}
|
|
729
|
-
* const myEmitter = new MyEmitter();
|
|
730
|
-
*
|
|
731
|
-
* const callbackA = () => {
|
|
732
|
-
* console.log('A');
|
|
733
|
-
* myEmitter.removeListener('event', callbackB);
|
|
734
|
-
* };
|
|
735
|
-
*
|
|
736
|
-
* const callbackB = () => {
|
|
737
|
-
* console.log('B');
|
|
738
|
-
* };
|
|
739
|
-
*
|
|
740
|
-
* myEmitter.on('event', callbackA);
|
|
741
|
-
*
|
|
742
|
-
* myEmitter.on('event', callbackB);
|
|
743
|
-
*
|
|
744
|
-
* // callbackA removes listener callbackB but it will still be called.
|
|
745
|
-
* // Internal listener array at time of emit [callbackA, callbackB]
|
|
746
|
-
* myEmitter.emit('event');
|
|
747
|
-
* // Prints:
|
|
748
|
-
* // A
|
|
749
|
-
* // B
|
|
750
|
-
*
|
|
751
|
-
* // callbackB is now removed.
|
|
752
|
-
* // Internal listener array [callbackA]
|
|
753
|
-
* myEmitter.emit('event');
|
|
754
|
-
* // Prints:
|
|
755
|
-
* // A
|
|
756
|
-
* ```
|
|
757
|
-
*
|
|
758
|
-
* Because listeners are managed using an internal array, calling this will
|
|
759
|
-
* change the position indices of any listener registered _after_ the listener
|
|
760
|
-
* being removed. This will not impact the order in which listeners are called,
|
|
761
|
-
* but it means that any copies of the listener array as returned by
|
|
762
|
-
* the `emitter.listeners()` method will need to be recreated.
|
|
763
|
-
*
|
|
764
|
-
* When a single function has been added as a handler multiple times for a single
|
|
765
|
-
* event (as in the example below), `removeListener()` will remove the most
|
|
766
|
-
* recently added instance. In the example the `once('ping')` listener is removed:
|
|
767
|
-
*
|
|
768
|
-
* ```js
|
|
769
|
-
* import { EventEmitter } from 'node:events';
|
|
770
|
-
* const ee = new EventEmitter();
|
|
771
|
-
*
|
|
772
|
-
* function pong() {
|
|
773
|
-
* console.log('pong');
|
|
774
|
-
* }
|
|
775
|
-
*
|
|
776
|
-
* ee.on('ping', pong);
|
|
777
|
-
* ee.once('ping', pong);
|
|
778
|
-
* ee.removeListener('ping', pong);
|
|
779
|
-
*
|
|
780
|
-
* ee.emit('ping');
|
|
781
|
-
* ee.emit('ping');
|
|
782
|
-
* ```
|
|
783
|
-
*
|
|
784
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
785
|
-
* @since v0.1.26
|
|
786
|
-
*/
|
|
787
|
-
removeListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
788
|
-
/**
|
|
789
|
-
* Alias for `emitter.removeListener()`.
|
|
790
|
-
* @since v10.0.0
|
|
791
|
-
*/
|
|
792
|
-
off<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
793
|
-
/**
|
|
794
|
-
* Removes all listeners, or those of the specified `eventName`.
|
|
795
|
-
*
|
|
796
|
-
* It is bad practice to remove listeners added elsewhere in the code,
|
|
797
|
-
* particularly when the `EventEmitter` instance was created by some other
|
|
798
|
-
* component or module (e.g. sockets or file streams).
|
|
799
|
-
*
|
|
800
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
801
|
-
* @since v0.1.26
|
|
802
|
-
*/
|
|
803
|
-
removeAllListeners(eventName?: Key<unknown, T>): this;
|
|
804
|
-
/**
|
|
805
|
-
* By default `EventEmitter`s will print a warning if more than `10` listeners are
|
|
806
|
-
* added for a particular event. This is a useful default that helps finding
|
|
807
|
-
* memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
|
|
808
|
-
* modified for this specific `EventEmitter` instance. The value can be set to `Infinity` (or `0`) to indicate an unlimited number of listeners.
|
|
809
|
-
*
|
|
810
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
811
|
-
* @since v0.3.5
|
|
812
|
-
*/
|
|
813
|
-
setMaxListeners(n: number): this;
|
|
814
|
-
/**
|
|
815
|
-
* Returns the current max listener value for the `EventEmitter` which is either
|
|
816
|
-
* set by `emitter.setMaxListeners(n)` or defaults to {@link EventEmitter.defaultMaxListeners}.
|
|
817
|
-
* @since v1.0.0
|
|
818
|
-
*/
|
|
819
|
-
getMaxListeners(): number;
|
|
820
|
-
/**
|
|
821
|
-
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
822
|
-
*
|
|
823
|
-
* ```js
|
|
824
|
-
* server.on('connection', (stream) => {
|
|
825
|
-
* console.log('someone connected!');
|
|
826
|
-
* });
|
|
827
|
-
* console.log(util.inspect(server.listeners('connection')));
|
|
828
|
-
* // Prints: [ [Function] ]
|
|
829
|
-
* ```
|
|
830
|
-
* @since v0.1.26
|
|
831
|
-
*/
|
|
832
|
-
listeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
|
|
833
|
-
/**
|
|
834
|
-
* Returns a copy of the array of listeners for the event named `eventName`,
|
|
835
|
-
* including any wrappers (such as those created by `.once()`).
|
|
836
|
-
*
|
|
837
|
-
* ```js
|
|
838
|
-
* import { EventEmitter } from 'node:events';
|
|
839
|
-
* const emitter = new EventEmitter();
|
|
840
|
-
* emitter.once('log', () => console.log('log once'));
|
|
841
|
-
*
|
|
842
|
-
* // Returns a new Array with a function `onceWrapper` which has a property
|
|
843
|
-
* // `listener` which contains the original listener bound above
|
|
844
|
-
* const listeners = emitter.rawListeners('log');
|
|
845
|
-
* const logFnWrapper = listeners[0];
|
|
846
|
-
*
|
|
847
|
-
* // Logs "log once" to the console and does not unbind the `once` event
|
|
848
|
-
* logFnWrapper.listener();
|
|
849
|
-
*
|
|
850
|
-
* // Logs "log once" to the console and removes the listener
|
|
851
|
-
* logFnWrapper();
|
|
852
|
-
*
|
|
853
|
-
* emitter.on('log', () => console.log('log persistently'));
|
|
854
|
-
* // Will return a new Array with a single function bound by `.on()` above
|
|
855
|
-
* const newListeners = emitter.rawListeners('log');
|
|
856
|
-
*
|
|
857
|
-
* // Logs "log persistently" twice
|
|
858
|
-
* newListeners[0]();
|
|
859
|
-
* emitter.emit('log');
|
|
860
|
-
* ```
|
|
861
|
-
* @since v9.4.0
|
|
862
|
-
*/
|
|
863
|
-
rawListeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
|
|
864
|
-
/**
|
|
865
|
-
* Synchronously calls each of the listeners registered for the event named `eventName`, in the order they were registered, passing the supplied arguments
|
|
866
|
-
* to each.
|
|
867
|
-
*
|
|
868
|
-
* Returns `true` if the event had listeners, `false` otherwise.
|
|
869
|
-
*
|
|
870
|
-
* ```js
|
|
871
|
-
* import { EventEmitter } from 'node:events';
|
|
872
|
-
* const myEmitter = new EventEmitter();
|
|
873
|
-
*
|
|
874
|
-
* // First listener
|
|
875
|
-
* myEmitter.on('event', function firstListener() {
|
|
876
|
-
* console.log('Helloooo! first listener');
|
|
877
|
-
* });
|
|
878
|
-
* // Second listener
|
|
879
|
-
* myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
880
|
-
* console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
881
|
-
* });
|
|
882
|
-
* // Third listener
|
|
883
|
-
* myEmitter.on('event', function thirdListener(...args) {
|
|
884
|
-
* const parameters = args.join(', ');
|
|
885
|
-
* console.log(`event with parameters ${parameters} in third listener`);
|
|
886
|
-
* });
|
|
887
|
-
*
|
|
888
|
-
* console.log(myEmitter.listeners('event'));
|
|
889
|
-
*
|
|
890
|
-
* myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
891
|
-
*
|
|
892
|
-
* // Prints:
|
|
893
|
-
* // [
|
|
894
|
-
* // [Function: firstListener],
|
|
895
|
-
* // [Function: secondListener],
|
|
896
|
-
* // [Function: thirdListener]
|
|
897
|
-
* // ]
|
|
898
|
-
* // Helloooo! first listener
|
|
899
|
-
* // event with parameters 1, 2 in second listener
|
|
900
|
-
* // event with parameters 1, 2, 3, 4, 5 in third listener
|
|
901
|
-
* ```
|
|
902
|
-
* @since v0.1.26
|
|
903
|
-
*/
|
|
904
|
-
emit<K>(eventName: Key<K, T>, ...args: Args<K, T>): boolean;
|
|
905
|
-
/**
|
|
906
|
-
* Returns the number of listeners listening for the event named `eventName`.
|
|
907
|
-
* If `listener` is provided, it will return how many times the listener is found
|
|
908
|
-
* in the list of the listeners of the event.
|
|
909
|
-
* @since v3.2.0
|
|
910
|
-
* @param eventName The name of the event being listened for
|
|
911
|
-
* @param listener The event handler function
|
|
912
|
-
*/
|
|
913
|
-
listenerCount<K>(eventName: Key<K, T>, listener?: Listener2<K, T>): number;
|
|
914
|
-
/**
|
|
915
|
-
* Adds the `listener` function to the _beginning_ of the listeners array for the
|
|
916
|
-
* event named `eventName`. No checks are made to see if the `listener` has
|
|
917
|
-
* already been added. Multiple calls passing the same combination of `eventName`
|
|
918
|
-
* and `listener` will result in the `listener` being added, and called, multiple times.
|
|
919
|
-
*
|
|
920
|
-
* ```js
|
|
921
|
-
* server.prependListener('connection', (stream) => {
|
|
922
|
-
* console.log('someone connected!');
|
|
923
|
-
* });
|
|
924
|
-
* ```
|
|
925
|
-
*
|
|
926
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
927
|
-
* @since v6.0.0
|
|
928
|
-
* @param eventName The name of the event.
|
|
929
|
-
* @param listener The callback function
|
|
930
|
-
*/
|
|
931
|
-
prependListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
932
|
-
/**
|
|
933
|
-
* Adds a **one-time**`listener` function for the event named `eventName` to the _beginning_ of the listeners array. The next time `eventName` is triggered, this
|
|
934
|
-
* listener is removed, and then invoked.
|
|
935
|
-
*
|
|
936
|
-
* ```js
|
|
937
|
-
* server.prependOnceListener('connection', (stream) => {
|
|
938
|
-
* console.log('Ah, we have our first user!');
|
|
939
|
-
* });
|
|
940
|
-
* ```
|
|
941
|
-
*
|
|
942
|
-
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
943
|
-
* @since v6.0.0
|
|
944
|
-
* @param eventName The name of the event.
|
|
945
|
-
* @param listener The callback function
|
|
946
|
-
*/
|
|
947
|
-
prependOnceListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
|
|
948
|
-
/**
|
|
949
|
-
* Returns an array listing the events for which the emitter has registered
|
|
950
|
-
* listeners. The values in the array are strings or `Symbol`s.
|
|
951
|
-
*
|
|
952
|
-
* ```js
|
|
953
|
-
* import { EventEmitter } from 'node:events';
|
|
954
|
-
*
|
|
955
|
-
* const myEE = new EventEmitter();
|
|
956
|
-
* myEE.on('foo', () => {});
|
|
957
|
-
* myEE.on('bar', () => {});
|
|
958
|
-
*
|
|
959
|
-
* const sym = Symbol('symbol');
|
|
960
|
-
* myEE.on(sym, () => {});
|
|
961
|
-
*
|
|
962
|
-
* console.log(myEE.eventNames());
|
|
963
|
-
* // Prints: [ 'foo', 'bar', Symbol(symbol) ]
|
|
964
|
-
* ```
|
|
965
|
-
* @since v6.0.0
|
|
966
|
-
*/
|
|
967
|
-
eventNames(): Array<(string | symbol) & Key2<unknown, T>>;
|
|
968
|
-
}
|
|
1038
|
+
interface EventEmitter<T extends EventMap<T> = any> extends _<T> {}
|
|
969
1039
|
}
|
|
970
1040
|
}
|
|
971
1041
|
export = EventEmitter;
|
|
972
1042
|
}
|
|
973
|
-
declare module "
|
|
974
|
-
import events = require("events");
|
|
1043
|
+
declare module "events" {
|
|
1044
|
+
import events = require("node:events");
|
|
975
1045
|
export = events;
|
|
976
1046
|
}
|