| | |
| | | /** |
| | | * Much of the Node.js core API is built around an idiomatic asynchronous |
| | | * event-driven architecture in which certain kinds of objects (called "emitters") |
| | | * emit named events that cause `Function` objects ("listeners") to be called. |
| | | * |
| | | * For instance: a `net.Server` object emits an event each time a peer |
| | | * connects to it; a `fs.ReadStream` emits an event when the file is opened; |
| | | * a `stream` emits an event whenever data is available to be read. |
| | | * |
| | | * All objects that emit events are instances of the `EventEmitter` class. These |
| | | * objects expose an `eventEmitter.on()` function that allows one or more |
| | | * functions to be attached to named events emitted by the object. Typically, |
| | | * event names are camel-cased strings but any valid JavaScript property key |
| | | * can be used. |
| | | * |
| | | * When the `EventEmitter` object emits an event, all of the functions attached |
| | | * to that specific event are called _synchronously_. Any values returned by the |
| | | * called listeners are _ignored_ and discarded. |
| | | * |
| | | * The following example shows a simple `EventEmitter` instance with a single |
| | | * listener. The `eventEmitter.on()` method is used to register listeners, while |
| | | * the `eventEmitter.emit()` method is used to trigger the event. |
| | | * |
| | | * ```js |
| | | * import { EventEmitter } from 'node:events'; |
| | | * |
| | | * class MyEmitter extends EventEmitter {} |
| | | * |
| | | * const myEmitter = new MyEmitter(); |
| | | * myEmitter.on('event', () => { |
| | | * console.log('an event occurred!'); |
| | | * }); |
| | | * myEmitter.emit('event'); |
| | | * ``` |
| | | * @see [source](https://github.com/nodejs/node/blob/v25.x/lib/events.js) |
| | | */ |
| | | declare module "node:events" { |
| | | import { AsyncResource, AsyncResourceOptions } from "node:async_hooks"; |
| | | // #region Event map helpers |
| | |
| | | */ |
| | | class EventEmitter<T extends EventMap<T> = any> { |
| | | constructor(options?: EventEmitterOptions); |
| | | } |
| | | interface EventEmitter<T extends EventMap<T> = any> extends NodeJS.EventEmitter<T> {} |
| | | global { |
| | | namespace NodeJS { |
| | | interface EventEmitter<T extends EventMap<T> = any> { |
| | | /** |
| | | * The `Symbol.for('nodejs.rejection')` method is called in case a |
| | | * promise rejection happens when emitting an event and |
| | |
| | | * @param eventName The name of the event being listened for |
| | | * @param listener The event handler function |
| | | */ |
| | | listenerCount<E extends string | symbol>(eventName: EventNames<T, E>, listener?: Listener<T, E>): number; |
| | | listenerCount<E extends string | symbol>( |
| | | eventName: EventNames<T, E>, |
| | | listener?: Listener<T, E>, |
| | | ): number; |
| | | /** |
| | | * Returns a copy of the array of listeners for the event named `eventName`. |
| | | * |
| | |
| | | * @param eventName The name of the event. |
| | | * @param listener The callback function |
| | | */ |
| | | prependOnceListener<E extends string | symbol>(eventName: EventNames<T, E>, listener: Listener<T, E>): this; |
| | | prependOnceListener<E extends string | symbol>( |
| | | eventName: EventNames<T, E>, |
| | | listener: Listener<T, E>, |
| | | ): this; |
| | | /** |
| | | * Returns a copy of the array of listeners for the event named `eventName`, |
| | | * including any wrappers (such as those created by `.once()`). |
| | |
| | | * Returns a reference to the `EventEmitter`, so that calls can be chained. |
| | | * @since v0.1.26 |
| | | */ |
| | | // eslint-disable-next-line @definitelytyped/no-unnecessary-generics |
| | | removeAllListeners<E extends string | symbol>(eventName?: EventNames<T, E>): this; |
| | | /** |
| | | * Removes the specified `listener` from the listener array for the event named |
| | |
| | | * @since v0.3.5 |
| | | */ |
| | | setMaxListeners(n: number): this; |
| | | } |
| | | } |
| | | } |
| | | namespace EventEmitter { |
| | | export { EventEmitter, EventEmitterEventMap, EventEmitterOptions }; |
| | |
| | | * ``` |
| | | * @since v15.2.0, v14.17.0 |
| | | */ |
| | | function getEventListeners(emitter: NodeJS.EventEmitter, name: string | symbol): ((...args: any[]) => void)[]; |
| | | function getEventListeners(emitter: EventEmitter, name: string | symbol): ((...args: any[]) => void)[]; |
| | | function getEventListeners(emitter: EventTarget, name: string): ((...args: any[]) => void)[]; |
| | | /** |
| | | * Returns the currently set max amount of listeners. |
| | |
| | | * ``` |
| | | * @since v19.9.0 |
| | | */ |
| | | function getMaxListeners(emitter: NodeJS.EventEmitter | EventTarget): number; |
| | | function getMaxListeners(emitter: EventEmitter | EventTarget): number; |
| | | /** |
| | | * A class method that returns the number of listeners for the given `eventName` |
| | | * registered on the given `emitter`. |
| | | * Returns the number of registered listeners for the event named `eventName`. |
| | | * |
| | | * ```js |
| | | * import { EventEmitter, listenerCount } from 'node:events'; |
| | | * For `EventEmitter`s this behaves exactly the same as calling `.listenerCount` |
| | | * on the emitter. |
| | | * |
| | | * const myEmitter = new EventEmitter(); |
| | | * myEmitter.on('event', () => {}); |
| | | * myEmitter.on('event', () => {}); |
| | | * console.log(listenerCount(myEmitter, 'event')); |
| | | * // Prints: 2 |
| | | * ``` |
| | | * For `EventTarget`s this is the only way to obtain the listener count. This can |
| | | * be useful for debugging and diagnostic purposes. |
| | | * @since v0.9.12 |
| | | * @deprecated Use `emitter.listenerCount()` instead. |
| | | * @param emitter The emitter to query |
| | | * @param eventName The event name |
| | | */ |
| | | function listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number; |
| | | function listenerCount(emitter: EventEmitter, eventName: string | symbol): number; |
| | | function listenerCount(emitter: EventTarget, eventName: string): number; |
| | | interface OnOptions extends Abortable { |
| | | /** |
| | | * Names of events that will end the iteration. |
| | |
| | | * @returns `AsyncIterator` that iterates `eventName` events emitted by the `emitter` |
| | | */ |
| | | function on( |
| | | emitter: NodeJS.EventEmitter, |
| | | emitter: EventEmitter, |
| | | eventName: string | symbol, |
| | | options?: OnOptions, |
| | | ): NodeJS.AsyncIterator<any[]>; |
| | |
| | | * @since v11.13.0, v10.16.0 |
| | | */ |
| | | function once( |
| | | emitter: NodeJS.EventEmitter, |
| | | emitter: EventEmitter, |
| | | eventName: string | symbol, |
| | | options?: OnceOptions, |
| | | ): Promise<any[]>; |
| | |
| | | * max for all newly created `EventTarget` and `EventEmitter` objects. |
| | | * objects. |
| | | */ |
| | | function setMaxListeners(n: number, ...eventTargets: ReadonlyArray<NodeJS.EventEmitter | EventTarget>): void; |
| | | function setMaxListeners(n: number, ...eventTargets: ReadonlyArray<EventEmitter | EventTarget>): void; |
| | | /** |
| | | * This is the interface from which event-emitting Node.js APIs inherit in the types package. |
| | | * **It is not intended for consumer use.** |
| | |
| | | type InternalEventTargetEventProperties<T> = { |
| | | [K in keyof T & string as `on${K}`]: ((ev: T[K]) => void) | null; |
| | | }; |
| | | } |
| | | global { |
| | | import _ = EventEmitter; |
| | | namespace NodeJS { |
| | | interface EventEmitter<T extends EventMap<T> = any> extends _<T> {} |
| | | } |
| | | } |
| | | export = EventEmitter; |
| | | } |