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