Pinsave/counterstrike
1
1/**2 * Much of the Node.js core API is built around an idiomatic asynchronous3 * event-driven architecture in which certain kinds of objects (called "emitters")4 * emit named events that cause `Function` objects ("listeners") to be called.5 *6 * For instance: a `net.Server` object emits an event each time a peer7 * connects to it; a `fs.ReadStream` emits an event when the file is opened;8 * a `stream` emits an event whenever data is available to be read.9 *10 * All objects that emit events are instances of the `EventEmitter` class. These11 * objects expose an `eventEmitter.on()` function that allows one or more12 * functions to be attached to named events emitted by the object. Typically,13 * event names are camel-cased strings but any valid JavaScript property key14 * can be used.15 *16 * When the `EventEmitter` object emits an event, all of the functions attached17 * to that specific event are called _synchronously_. Any values returned by the18 * called listeners are _ignored_ and discarded.19 *20 * The following example shows a simple `EventEmitter` instance with a single21 * listener. The `eventEmitter.on()` method is used to register listeners, while22 * the `eventEmitter.emit()` method is used to trigger the event.23 *24 * ```js25 * import { EventEmitter } from 'node:events';26 *27 * class MyEmitter extends EventEmitter {}28 *29 * const myEmitter = new MyEmitter();30 * myEmitter.on('event', () => {31 * console.log('an event occurred!');32 * });33 * myEmitter.emit('event');34 * ```35 * @see [source](https://github.com/nodejs/node/blob/v24.x/lib/events.js)36 */37declare module "events" {38 import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";39 // NOTE: This class is in the docs but is **not actually exported** by Node.40 // If https://github.com/nodejs/node/issues/39903 gets resolved and Node41 // actually starts exporting the class, uncomment below.42 // import { EventListener, EventListenerObject } from '__dom-events';43 // /** The NodeEventTarget is a Node.js-specific extension to EventTarget that emulates a subset of the EventEmitter API. */44 // interface NodeEventTarget extends EventTarget {45 // /**46 // * Node.js-specific extension to the `EventTarget` class that emulates the equivalent `EventEmitter` API.47 // * The only difference between `addListener()` and `addEventListener()` is that addListener() will return a reference to the EventTarget.48 // */49 // addListener(type: string, listener: EventListener | EventListenerObject, options?: { once: boolean }): this;50 // /** Node.js-specific extension to the `EventTarget` class that returns an array of event `type` names for which event listeners are registered. */51 // eventNames(): string[];52 // /** Node.js-specific extension to the `EventTarget` class that returns the number of event listeners registered for the `type`. */53 // listenerCount(type: string): number;54 // /** Node.js-specific alias for `eventTarget.removeListener()`. */55 // off(type: string, listener: EventListener | EventListenerObject): this;56 // /** Node.js-specific alias for `eventTarget.addListener()`. */57 // on(type: string, listener: EventListener | EventListenerObject, options?: { once: boolean }): this;58 // /** Node.js-specific extension to the `EventTarget` class that adds a `once` listener for the given event `type`. This is equivalent to calling `on` with the `once` option set to `true`. */59 // once(type: string, listener: EventListener | EventListenerObject): this;60 // /**61 // * Node.js-specific extension to the `EventTarget` class.62 // * If `type` is specified, removes all registered listeners for `type`,63 // * otherwise removes all registered listeners.64 // */65 // removeAllListeners(type: string): this;66 // /**67 // * Node.js-specific extension to the `EventTarget` class that removes the listener for the given `type`.68 // * The only difference between `removeListener()` and `removeEventListener()` is that `removeListener()` will return a reference to the `EventTarget`.69 // */70 // removeListener(type: string, listener: EventListener | EventListenerObject): this;71 // }72 interface EventEmitterOptions {73 /**74 * Enables automatic capturing of promise rejection.75 */76 captureRejections?: boolean | undefined;77 }78 interface StaticEventEmitterOptions {79 /**80 * Can be used to cancel awaiting events.81 */82 signal?: AbortSignal | undefined;83 }84 interface StaticEventEmitterIteratorOptions extends StaticEventEmitterOptions {85 /**86 * Names of events that will end the iteration.87 */88 close?: string[] | undefined;89 /**90 * The high watermark. The emitter is paused every time the size of events being buffered is higher than it.91 * Supported only on emitters implementing `pause()` and `resume()` methods.92 * @default Number.MAX_SAFE_INTEGER93 */94 highWaterMark?: number | undefined;95 /**96 * The low watermark. The emitter is resumed every time the size of events being buffered is lower than it.97 * Supported only on emitters implementing `pause()` and `resume()` methods.98 * @default 199 */100 lowWaterMark?: number | undefined;101 }102 interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}103 type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;104 type DefaultEventMap = [never];105 type AnyRest = [...args: any[]];106 type Args<K, T> = T extends DefaultEventMap ? AnyRest : (107 K extends keyof T ? T[K] : never108 );109 type Key<K, T> = T extends DefaultEventMap ? string | symbol : K | keyof T;110 type Key2<K, T> = T extends DefaultEventMap ? string | symbol : K & keyof T;111 type Listener<K, T, F> = T extends DefaultEventMap ? F : (112 K extends keyof T ? (113 T[K] extends unknown[] ? (...args: T[K]) => void : never114 )115 : never116 );117 type Listener1<K, T> = Listener<K, T, (...args: any[]) => void>;118 type Listener2<K, T> = Listener<K, T, Function>;119 120 /**121 * The `EventEmitter` class is defined and exposed by the `node:events` module:122 *123 * ```js124 * import { EventEmitter } from 'node:events';125 * ```126 *127 * All `EventEmitter`s emit the event `'newListener'` when new listeners are128 * added and `'removeListener'` when existing listeners are removed.129 *130 * It supports the following option:131 * @since v0.1.26132 */133 class EventEmitter<T extends EventMap<T> = DefaultEventMap> {134 constructor(options?: EventEmitterOptions);135 136 [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;137 138 /**139 * Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given140 * event or that is rejected if the `EventEmitter` emits `'error'` while waiting.141 * The `Promise` will resolve with an array of all the arguments emitted to the142 * given event.143 *144 * 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'` event145 * semantics and does not listen to the `'error'` event.146 *147 * ```js148 * import { once, EventEmitter } from 'node:events';149 * import process from 'node:process';150 *151 * const ee = new EventEmitter();152 *153 * process.nextTick(() => {154 * ee.emit('myevent', 42);155 * });156 *157 * const [value] = await once(ee, 'myevent');158 * console.log(value);159 *160 * const err = new Error('kaboom');161 * process.nextTick(() => {162 * ee.emit('error', err);163 * });164 *165 * try {166 * await once(ee, 'myevent');167 * } catch (err) {168 * console.error('error happened', err);169 * }170 * ```171 *172 * 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 the173 * '`error'` event itself, then it is treated as any other kind of event without174 * special handling:175 *176 * ```js177 * import { EventEmitter, once } from 'node:events';178 *179 * const ee = new EventEmitter();180 *181 * once(ee, 'error')182 * .then(([err]) => console.log('ok', err.message))183 * .catch((err) => console.error('error', err.message));184 *185 * ee.emit('error', new Error('boom'));186 *187 * // Prints: ok boom188 * ```189 *190 * An `AbortSignal` can be used to cancel waiting for the event:191 *192 * ```js193 * import { EventEmitter, once } from 'node:events';194 *195 * const ee = new EventEmitter();196 * const ac = new AbortController();197 *198 * async function foo(emitter, event, signal) {199 * try {200 * await once(emitter, event, { signal });201 * console.log('event emitted!');202 * } catch (error) {203 * if (error.name === 'AbortError') {204 * console.error('Waiting for the event was canceled!');205 * } else {206 * console.error('There was an error', error.message);207 * }208 * }209 * }210 *211 * foo(ee, 'foo', ac.signal);212 * ac.abort(); // Abort waiting for the event213 * ee.emit('foo'); // Prints: Waiting for the event was canceled!214 * ```215 * @since v11.13.0, v10.16.0216 */217 static once(218 emitter: NodeJS.EventEmitter,219 eventName: string | symbol,220 options?: StaticEventEmitterOptions,221 ): Promise<any[]>;222 static once(emitter: EventTarget, eventName: string, options?: StaticEventEmitterOptions): Promise<any[]>;223 /**224 * ```js225 * import { on, EventEmitter } from 'node:events';226 * import process from 'node:process';227 *228 * const ee = new EventEmitter();229 *230 * // Emit later on231 * process.nextTick(() => {232 * ee.emit('foo', 'bar');233 * ee.emit('foo', 42);234 * });235 *236 * for await (const event of on(ee, 'foo')) {237 * // The execution of this inner block is synchronous and it238 * // processes one event at a time (even with await). Do not use239 * // if concurrent execution is required.240 * console.log(event); // prints ['bar'] [42]241 * }242 * // Unreachable here243 * ```244 *245 * Returns an `AsyncIterator` that iterates `eventName` events. It will throw246 * if the `EventEmitter` emits `'error'`. It removes all listeners when247 * exiting the loop. The `value` returned by each iteration is an array248 * composed of the emitted event arguments.249 *250 * An `AbortSignal` can be used to cancel waiting on events:251 *252 * ```js253 * import { on, EventEmitter } from 'node:events';254 * import process from 'node:process';255 *256 * const ac = new AbortController();257 *258 * (async () => {259 * const ee = new EventEmitter();260 *261 * // Emit later on262 * process.nextTick(() => {263 * ee.emit('foo', 'bar');264 * ee.emit('foo', 42);265 * });266 *267 * for await (const event of on(ee, 'foo', { signal: ac.signal })) {268 * // The execution of this inner block is synchronous and it269 * // processes one event at a time (even with await). Do not use270 * // if concurrent execution is required.271 * console.log(event); // prints ['bar'] [42]272 * }273 * // Unreachable here274 * })();275 *276 * process.nextTick(() => ac.abort());277 * ```278 *279 * Use the `close` option to specify an array of event names that will end the iteration:280 *281 * ```js282 * import { on, EventEmitter } from 'node:events';283 * import process from 'node:process';284 *285 * const ee = new EventEmitter();286 *287 * // Emit later on288 * process.nextTick(() => {289 * ee.emit('foo', 'bar');290 * ee.emit('foo', 42);291 * ee.emit('close');292 * });293 *294 * for await (const event of on(ee, 'foo', { close: ['close'] })) {295 * console.log(event); // prints ['bar'] [42]296 * }297 * // the loop will exit after 'close' is emitted298 * console.log('done'); // prints 'done'299 * ```300 * @since v13.6.0, v12.16.0301 * @return An `AsyncIterator` that iterates `eventName` events emitted by the `emitter`302 */303 static on(304 emitter: NodeJS.EventEmitter,305 eventName: string | symbol,306 options?: StaticEventEmitterIteratorOptions,307 ): NodeJS.AsyncIterator<any[]>;308 static on(309 emitter: EventTarget,310 eventName: string,311 options?: StaticEventEmitterIteratorOptions,312 ): NodeJS.AsyncIterator<any[]>;313 /**314 * A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.315 *316 * ```js317 * import { EventEmitter, listenerCount } from 'node:events';318 *319 * const myEmitter = new EventEmitter();320 * myEmitter.on('event', () => {});321 * myEmitter.on('event', () => {});322 * console.log(listenerCount(myEmitter, 'event'));323 * // Prints: 2324 * ```325 * @since v0.9.12326 * @deprecated Since v3.2.0 - Use `listenerCount` instead.327 * @param emitter The emitter to query328 * @param eventName The event name329 */330 static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;331 /**332 * Returns a copy of the array of listeners for the event named `eventName`.333 *334 * For `EventEmitter`s this behaves exactly the same as calling `.listeners` on335 * the emitter.336 *337 * For `EventTarget`s this is the only way to get the event listeners for the338 * event target. This is useful for debugging and diagnostic purposes.339 *340 * ```js341 * import { getEventListeners, EventEmitter } from 'node:events';342 *343 * {344 * const ee = new EventEmitter();345 * const listener = () => console.log('Events are fun');346 * ee.on('foo', listener);347 * console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]348 * }349 * {350 * const et = new EventTarget();351 * const listener = () => console.log('Events are fun');352 * et.addEventListener('foo', listener);353 * console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]354 * }355 * ```356 * @since v15.2.0, v14.17.0357 */358 static getEventListeners(emitter: EventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];359 /**360 * Returns the currently set max amount of listeners.361 *362 * For `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on363 * the emitter.364 *365 * For `EventTarget`s this is the only way to get the max event listeners for the366 * event target. If the number of event handlers on a single EventTarget exceeds367 * the max set, the EventTarget will print a warning.368 *369 * ```js370 * import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';371 *372 * {373 * const ee = new EventEmitter();374 * console.log(getMaxListeners(ee)); // 10375 * setMaxListeners(11, ee);376 * console.log(getMaxListeners(ee)); // 11377 * }378 * {379 * const et = new EventTarget();380 * console.log(getMaxListeners(et)); // 10381 * setMaxListeners(11, et);382 * console.log(getMaxListeners(et)); // 11383 * }384 * ```385 * @since v19.9.0386 */387 static getMaxListeners(emitter: EventTarget | NodeJS.EventEmitter): number;388 /**389 * ```js390 * import { setMaxListeners, EventEmitter } from 'node:events';391 *392 * const target = new EventTarget();393 * const emitter = new EventEmitter();394 *395 * setMaxListeners(5, target, emitter);396 * ```397 * @since v15.4.0398 * @param n A non-negative number. The maximum number of listeners per `EventTarget` event.399 * @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}400 * objects.401 */402 static setMaxListeners(n?: number, ...eventTargets: Array<EventTarget | NodeJS.EventEmitter>): void;403 /**404 * Listens once to the `abort` event on the provided `signal`.405 *406 * Listening to the `abort` event on abort signals is unsafe and may407 * lead to resource leaks since another third party with the signal can408 * call `e.stopImmediatePropagation()`. Unfortunately Node.js cannot change409 * this since it would violate the web standard. Additionally, the original410 * API makes it easy to forget to remove listeners.411 *412 * This API allows safely using `AbortSignal`s in Node.js APIs by solving these413 * two issues by listening to the event such that `stopImmediatePropagation` does414 * not prevent the listener from running.415 *416 * Returns a disposable so that it may be unsubscribed from more easily.417 *418 * ```js419 * import { addAbortListener } from 'node:events';420 *421 * function example(signal) {422 * let disposable;423 * try {424 * signal.addEventListener('abort', (e) => e.stopImmediatePropagation());425 * disposable = addAbortListener(signal, (e) => {426 * // Do something when signal is aborted.427 * });428 * } finally {429 * disposable?.[Symbol.dispose]();430 * }431 * }432 * ```433 * @since v20.5.0434 * @return Disposable that removes the `abort` listener.435 */436 static addAbortListener(signal: AbortSignal, resource: (event: Event) => void): Disposable;437 /**438 * 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.439 *440 * Installing a listener using this symbol does not change the behavior once an `'error'` event is emitted. Therefore, the process will still crash if no441 * regular `'error'` listener is installed.442 * @since v13.6.0, v12.17.0443 */444 static readonly errorMonitor: unique symbol;445 /**446 * Value: `Symbol.for('nodejs.rejection')`447 *448 * See how to write a custom `rejection handler`.449 * @since v13.4.0, v12.16.0450 */451 static readonly captureRejectionSymbol: unique symbol;452 /**453 * Value: [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)454 *455 * Change the default `captureRejections` option on all new `EventEmitter` objects.456 * @since v13.4.0, v12.16.0457 */458 static captureRejections: boolean;459 /**460 * By default, a maximum of `10` listeners can be registered for any single461 * event. This limit can be changed for individual `EventEmitter` instances462 * using the `emitter.setMaxListeners(n)` method. To change the default463 * for _all_`EventEmitter` instances, the `events.defaultMaxListeners` property464 * can be used. If this value is not a positive number, a `RangeError` is thrown.465 *466 * Take caution when setting the `events.defaultMaxListeners` because the467 * change affects _all_ `EventEmitter` instances, including those created before468 * the change is made. However, calling `emitter.setMaxListeners(n)` still has469 * precedence over `events.defaultMaxListeners`.470 *471 * This is not a hard limit. The `EventEmitter` instance will allow472 * more listeners to be added but will output a trace warning to stderr indicating473 * that a "possible EventEmitter memory leak" has been detected. For any single474 * `EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()` methods can be used to475 * temporarily avoid this warning:476 *477 * ```js478 * import { EventEmitter } from 'node:events';479 * const emitter = new EventEmitter();480 * emitter.setMaxListeners(emitter.getMaxListeners() + 1);481 * emitter.once('event', () => {482 * // do stuff483 * emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));484 * });485 * ```486 *487 * The `--trace-warnings` command-line flag can be used to display the488 * stack trace for such warnings.489 *490 * The emitted warning can be inspected with `process.on('warning')` and will491 * have the additional `emitter`, `type`, and `count` properties, referring to492 * the event emitter instance, the event's name and the number of attached493 * listeners, respectively.494 * Its `name` property is set to `'MaxListenersExceededWarning'`.495 * @since v0.11.2496 */497 static defaultMaxListeners: number;498 }499 import internal = require("node:events");500 namespace EventEmitter {501 // Should just be `export { EventEmitter }`, but that doesn't work in TypeScript 3.4502 export { internal as EventEmitter };503 export interface Abortable {504 /**505 * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.506 */507 signal?: AbortSignal | undefined;508 }509 510 export interface EventEmitterReferencingAsyncResource extends AsyncResource {511 readonly eventEmitter: EventEmitterAsyncResource;512 }513 514 export interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {515 /**516 * The type of async event, this is required when instantiating `EventEmitterAsyncResource`517 * directly rather than as a child class.518 * @default new.target.name if instantiated as a child class.519 */520 name?: string;521 }522 523 /**524 * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that525 * require manual async tracking. Specifically, all events emitted by instances526 * of `events.EventEmitterAsyncResource` will run within its `async context`.527 *528 * ```js529 * import { EventEmitterAsyncResource, EventEmitter } from 'node:events';530 * import { notStrictEqual, strictEqual } from 'node:assert';531 * import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';532 *533 * // Async tracking tooling will identify this as 'Q'.534 * const ee1 = new EventEmitterAsyncResource({ name: 'Q' });535 *536 * // 'foo' listeners will run in the EventEmitters async context.537 * ee1.on('foo', () => {538 * strictEqual(executionAsyncId(), ee1.asyncId);539 * strictEqual(triggerAsyncId(), ee1.triggerAsyncId);540 * });541 *542 * const ee2 = new EventEmitter();543 *544 * // 'foo' listeners on ordinary EventEmitters that do not track async545 * // context, however, run in the same async context as the emit().546 * ee2.on('foo', () => {547 * notStrictEqual(executionAsyncId(), ee2.asyncId);548 * notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);549 * });550 *551 * Promise.resolve().then(() => {552 * ee1.emit('foo');553 * ee2.emit('foo');554 * });555 * ```556 *557 * The `EventEmitterAsyncResource` class has the same methods and takes the558 * same options as `EventEmitter` and `AsyncResource` themselves.559 * @since v17.4.0, v16.14.0560 */561 export class EventEmitterAsyncResource extends EventEmitter {562 /**563 * @param options Only optional in child class.564 */565 constructor(options?: EventEmitterAsyncResourceOptions);566 /**567 * Call all `destroy` hooks. This should only ever be called once. An error will568 * be thrown if it is called more than once. This **must** be manually called. If569 * the resource is left to be collected by the GC then the `destroy` hooks will570 * never be called.571 */572 emitDestroy(): void;573 /**574 * The unique `asyncId` assigned to the resource.575 */576 readonly asyncId: number;577 /**578 * The same triggerAsyncId that is passed to the AsyncResource constructor.579 */580 readonly triggerAsyncId: number;581 /**582 * The returned `AsyncResource` object has an additional `eventEmitter` property583 * that provides a reference to this `EventEmitterAsyncResource`.584 */585 readonly asyncResource: EventEmitterReferencingAsyncResource;586 }587 }588 global {589 namespace NodeJS {590 interface EventEmitter<T extends EventMap<T> = DefaultEventMap> {591 [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;592 /**593 * Alias for `emitter.on(eventName, listener)`.594 * @since v0.1.26595 */596 addListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;597 /**598 * Adds the `listener` function to the end of the listeners array for the event599 * named `eventName`. No checks are made to see if the `listener` has already600 * been added. Multiple calls passing the same combination of `eventName` and601 * `listener` will result in the `listener` being added, and called, multiple times.602 *603 * ```js604 * server.on('connection', (stream) => {605 * console.log('someone connected!');606 * });607 * ```608 *609 * Returns a reference to the `EventEmitter`, so that calls can be chained.610 *611 * By default, event listeners are invoked in the order they are added. The `emitter.prependListener()` method can be used as an alternative to add the612 * event listener to the beginning of the listeners array.613 *614 * ```js615 * import { EventEmitter } from 'node:events';616 * const myEE = new EventEmitter();617 * myEE.on('foo', () => console.log('a'));618 * myEE.prependListener('foo', () => console.log('b'));619 * myEE.emit('foo');620 * // Prints:621 * // b622 * // a623 * ```624 * @since v0.1.101625 * @param eventName The name of the event.626 * @param listener The callback function627 */628 on<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;629 /**630 * Adds a **one-time** `listener` function for the event named `eventName`. The631 * next time `eventName` is triggered, this listener is removed and then invoked.632 *633 * ```js634 * server.once('connection', (stream) => {635 * console.log('Ah, we have our first user!');636 * });637 * ```638 *639 * Returns a reference to the `EventEmitter`, so that calls can be chained.640 *641 * By default, event listeners are invoked in the order they are added. The `emitter.prependOnceListener()` method can be used as an alternative to add the642 * event listener to the beginning of the listeners array.643 *644 * ```js645 * import { EventEmitter } from 'node:events';646 * const myEE = new EventEmitter();647 * myEE.once('foo', () => console.log('a'));648 * myEE.prependOnceListener('foo', () => console.log('b'));649 * myEE.emit('foo');650 * // Prints:651 * // b652 * // a653 * ```654 * @since v0.3.0655 * @param eventName The name of the event.656 * @param listener The callback function657 */658 once<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;659 /**660 * Removes the specified `listener` from the listener array for the event named `eventName`.661 *662 * ```js663 * const callback = (stream) => {664 * console.log('someone connected!');665 * };666 * server.on('connection', callback);667 * // ...668 * server.removeListener('connection', callback);669 * ```670 *671 * `removeListener()` will remove, at most, one instance of a listener from the672 * listener array. If any single listener has been added multiple times to the673 * listener array for the specified `eventName`, then `removeListener()` must be674 * called multiple times to remove each instance.675 *676 * Once an event is emitted, all listeners attached to it at the677 * time of emitting are called in order. This implies that any `removeListener()` or `removeAllListeners()` calls _after_ emitting and _before_ the last listener finishes execution678 * will not remove them from`emit()` in progress. Subsequent events behave as expected.679 *680 * ```js681 * import { EventEmitter } from 'node:events';682 * class MyEmitter extends EventEmitter {}683 * const myEmitter = new MyEmitter();684 *685 * const callbackA = () => {686 * console.log('A');687 * myEmitter.removeListener('event', callbackB);688 * };689 *690 * const callbackB = () => {691 * console.log('B');692 * };693 *694 * myEmitter.on('event', callbackA);695 *696 * myEmitter.on('event', callbackB);697 *698 * // callbackA removes listener callbackB but it will still be called.699 * // Internal listener array at time of emit [callbackA, callbackB]700 * myEmitter.emit('event');701 * // Prints:702 * // A703 * // B704 *705 * // callbackB is now removed.706 * // Internal listener array [callbackA]707 * myEmitter.emit('event');708 * // Prints:709 * // A710 * ```711 *712 * Because listeners are managed using an internal array, calling this will713 * change the position indices of any listener registered _after_ the listener714 * being removed. This will not impact the order in which listeners are called,715 * but it means that any copies of the listener array as returned by716 * the `emitter.listeners()` method will need to be recreated.717 *718 * When a single function has been added as a handler multiple times for a single719 * event (as in the example below), `removeListener()` will remove the most720 * recently added instance. In the example the `once('ping')` listener is removed:721 *722 * ```js723 * import { EventEmitter } from 'node:events';724 * const ee = new EventEmitter();725 *726 * function pong() {727 * console.log('pong');728 * }729 *730 * ee.on('ping', pong);731 * ee.once('ping', pong);732 * ee.removeListener('ping', pong);733 *734 * ee.emit('ping');735 * ee.emit('ping');736 * ```737 *738 * Returns a reference to the `EventEmitter`, so that calls can be chained.739 * @since v0.1.26740 */741 removeListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;742 /**743 * Alias for `emitter.removeListener()`.744 * @since v10.0.0745 */746 off<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;747 /**748 * Removes all listeners, or those of the specified `eventName`.749 *750 * It is bad practice to remove listeners added elsewhere in the code,751 * particularly when the `EventEmitter` instance was created by some other752 * component or module (e.g. sockets or file streams).753 *754 * Returns a reference to the `EventEmitter`, so that calls can be chained.755 * @since v0.1.26756 */757 removeAllListeners(eventName?: Key<unknown, T>): this;758 /**759 * By default `EventEmitter`s will print a warning if more than `10` listeners are760 * added for a particular event. This is a useful default that helps finding761 * memory leaks. The `emitter.setMaxListeners()` method allows the limit to be762 * modified for this specific `EventEmitter` instance. The value can be set to `Infinity` (or `0`) to indicate an unlimited number of listeners.763 *764 * Returns a reference to the `EventEmitter`, so that calls can be chained.765 * @since v0.3.5766 */767 setMaxListeners(n: number): this;768 /**769 * Returns the current max listener value for the `EventEmitter` which is either770 * set by `emitter.setMaxListeners(n)` or defaults to {@link EventEmitter.defaultMaxListeners}.771 * @since v1.0.0772 */773 getMaxListeners(): number;774 /**775 * Returns a copy of the array of listeners for the event named `eventName`.776 *777 * ```js778 * server.on('connection', (stream) => {779 * console.log('someone connected!');780 * });781 * console.log(util.inspect(server.listeners('connection')));782 * // Prints: [ [Function] ]783 * ```784 * @since v0.1.26785 */786 listeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;787 /**788 * Returns a copy of the array of listeners for the event named `eventName`,789 * including any wrappers (such as those created by `.once()`).790 *791 * ```js792 * import { EventEmitter } from 'node:events';793 * const emitter = new EventEmitter();794 * emitter.once('log', () => console.log('log once'));795 *796 * // Returns a new Array with a function `onceWrapper` which has a property797 * // `listener` which contains the original listener bound above798 * const listeners = emitter.rawListeners('log');799 * const logFnWrapper = listeners[0];800 *801 * // Logs "log once" to the console and does not unbind the `once` event802 * logFnWrapper.listener();803 *804 * // Logs "log once" to the console and removes the listener805 * logFnWrapper();806 *807 * emitter.on('log', () => console.log('log persistently'));808 * // Will return a new Array with a single function bound by `.on()` above809 * const newListeners = emitter.rawListeners('log');810 *811 * // Logs "log persistently" twice812 * newListeners[0]();813 * emitter.emit('log');814 * ```815 * @since v9.4.0816 */817 rawListeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;818 /**819 * Synchronously calls each of the listeners registered for the event named `eventName`, in the order they were registered, passing the supplied arguments820 * to each.821 *822 * Returns `true` if the event had listeners, `false` otherwise.823 *824 * ```js825 * import { EventEmitter } from 'node:events';826 * const myEmitter = new EventEmitter();827 *828 * // First listener829 * myEmitter.on('event', function firstListener() {830 * console.log('Helloooo! first listener');831 * });832 * // Second listener833 * myEmitter.on('event', function secondListener(arg1, arg2) {834 * console.log(`event with parameters ${arg1}, ${arg2} in second listener`);835 * });836 * // Third listener837 * myEmitter.on('event', function thirdListener(...args) {838 * const parameters = args.join(', ');839 * console.log(`event with parameters ${parameters} in third listener`);840 * });841 *842 * console.log(myEmitter.listeners('event'));843 *844 * myEmitter.emit('event', 1, 2, 3, 4, 5);845 *846 * // Prints:847 * // [848 * // [Function: firstListener],849 * // [Function: secondListener],850 * // [Function: thirdListener]851 * // ]852 * // Helloooo! first listener853 * // event with parameters 1, 2 in second listener854 * // event with parameters 1, 2, 3, 4, 5 in third listener855 * ```856 * @since v0.1.26857 */858 emit<K>(eventName: Key<K, T>, ...args: Args<K, T>): boolean;859 /**860 * Returns the number of listeners listening for the event named `eventName`.861 * If `listener` is provided, it will return how many times the listener is found862 * in the list of the listeners of the event.863 * @since v3.2.0864 * @param eventName The name of the event being listened for865 * @param listener The event handler function866 */867 listenerCount<K>(eventName: Key<K, T>, listener?: Listener2<K, T>): number;868 /**869 * Adds the `listener` function to the _beginning_ of the listeners array for the870 * event named `eventName`. No checks are made to see if the `listener` has871 * already been added. Multiple calls passing the same combination of `eventName`872 * and `listener` will result in the `listener` being added, and called, multiple times.873 *874 * ```js875 * server.prependListener('connection', (stream) => {876 * console.log('someone connected!');877 * });878 * ```879 *880 * Returns a reference to the `EventEmitter`, so that calls can be chained.881 * @since v6.0.0882 * @param eventName The name of the event.883 * @param listener The callback function884 */885 prependListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;886 /**887 * Adds a **one-time**`listener` function for the event named `eventName` to the _beginning_ of the listeners array. The next time `eventName` is triggered, this888 * listener is removed, and then invoked.889 *890 * ```js891 * server.prependOnceListener('connection', (stream) => {892 * console.log('Ah, we have our first user!');893 * });894 * ```895 *896 * Returns a reference to the `EventEmitter`, so that calls can be chained.897 * @since v6.0.0898 * @param eventName The name of the event.899 * @param listener The callback function900 */901 prependOnceListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;902 /**903 * Returns an array listing the events for which the emitter has registered904 * listeners. The values in the array are strings or `Symbol`s.905 *906 * ```js907 * import { EventEmitter } from 'node:events';908 *909 * const myEE = new EventEmitter();910 * myEE.on('foo', () => {});911 * myEE.on('bar', () => {});912 *913 * const sym = Symbol('symbol');914 * myEE.on(sym, () => {});915 *916 * console.log(myEE.eventNames());917 * // Prints: [ 'foo', 'bar', Symbol(symbol) ]918 * ```919 * @since v6.0.0920 */921 eventNames(): Array<(string | symbol) & Key2<unknown, T>>;922 }923 }924 }925 export = EventEmitter;926}927declare module "node:events" {928 import events = require("events");929 export = events;930}931 