githrun

A CLI tool to run Python scrip...
Log | Files | Refs | README | LICENSE

events.d.ts (31985B)


      1 /**
      2  * Much of the Node.js core API is built around an idiomatic asynchronous
      3  * 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 peer
      7  * 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. These
     11  * objects expose an `eventEmitter.on()` function that allows one or more
     12  * functions to be attached to named events emitted by the object. Typically,
     13  * event names are camel-cased strings but any valid JavaScript property key
     14  * can be used.
     15  *
     16  * When the `EventEmitter` object emits an event, all of the functions attached
     17  * to that specific event are called _synchronously_. Any values returned by the
     18  * called listeners are _ignored_ and discarded.
     19  *
     20  * The following example shows a simple `EventEmitter` instance with a single
     21  * listener. The `eventEmitter.on()` method is used to register listeners, while
     22  * the `eventEmitter.emit()` method is used to trigger the event.
     23  *
     24  * ```js
     25  * 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/v16.9.0/lib/events.js)
     36  */
     37 declare module "events" {
     38     import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";
     39 
     40     interface EventEmitterOptions {
     41         /**
     42          * Enables automatic capturing of promise rejection.
     43          */
     44         captureRejections?: boolean | undefined;
     45     }
     46     interface NodeEventTarget {
     47         once(eventName: string | symbol, listener: (...args: any[]) => void): this;
     48     }
     49     interface DOMEventTarget {
     50         addEventListener(
     51             eventName: string,
     52             listener: (...args: any[]) => void,
     53             opts?: {
     54                 once: boolean;
     55             },
     56         ): any;
     57     }
     58     interface StaticEventEmitterOptions {
     59         signal?: AbortSignal | undefined;
     60     }
     61     interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
     62     type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
     63     type DefaultEventMap = [never];
     64     type AnyRest = [...args: any[]];
     65     type Args<K, T> = T extends DefaultEventMap ? AnyRest : (
     66         K extends keyof T ? T[K] : never
     67     );
     68     type Key<K, T> = T extends DefaultEventMap ? string | symbol : K | keyof T;
     69     type Key2<K, T> = T extends DefaultEventMap ? string | symbol : K & keyof T;
     70     type Listener<K, T, F> = T extends DefaultEventMap ? F : (
     71         K extends keyof T ? (
     72                 T[K] extends unknown[] ? (...args: T[K]) => void : never
     73             )
     74             : never
     75     );
     76     type Listener1<K, T> = Listener<K, T, (...args: any[]) => void>;
     77     type Listener2<K, T> = Listener<K, T, Function>;
     78     /**
     79      * The `EventEmitter` class is defined and exposed by the `events` module:
     80      *
     81      * ```js
     82      * import EventEmitter from 'node:events';
     83      * ```
     84      *
     85      * All `EventEmitter`s emit the event `'newListener'` when new listeners are
     86      * added and `'removeListener'` when existing listeners are removed.
     87      *
     88      * It supports the following option:
     89      * @since v0.1.26
     90      */
     91     class EventEmitter<T extends EventMap<T> = DefaultEventMap> {
     92         constructor(options?: EventEmitterOptions);
     93 
     94         [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
     95 
     96         /**
     97          * Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
     98          * event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
     99          * The `Promise` will resolve with an array of all the arguments emitted to the
    100          * given event.
    101          *
    102          * 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
    103          * semantics and does not listen to the `'error'` event.
    104          *
    105          * ```js
    106          * import { once, EventEmitter } from 'node:events';
    107          *
    108          * async function run() {
    109          *   const ee = new EventEmitter();
    110          *
    111          *   process.nextTick(() => {
    112          *     ee.emit('myevent', 42);
    113          *   });
    114          *
    115          *   const [value] = await once(ee, 'myevent');
    116          *   console.log(value);
    117          *
    118          *   const err = new Error('kaboom');
    119          *   process.nextTick(() => {
    120          *     ee.emit('error', err);
    121          *   });
    122          *
    123          *   try {
    124          *     await once(ee, 'myevent');
    125          *   } catch (err) {
    126          *     console.log('error happened', err);
    127          *   }
    128          * }
    129          *
    130          * run();
    131          * ```
    132          *
    133          * 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
    134          * '`error'` event itself, then it is treated as any other kind of event without
    135          * special handling:
    136          *
    137          * ```js
    138          * import { EventEmitter, once } from 'node:events';
    139          *
    140          * const ee = new EventEmitter();
    141          *
    142          * once(ee, 'error')
    143          *   .then(([err]) => console.log('ok', err.message))
    144          *   .catch((err) => console.log('error', err.message));
    145          *
    146          * ee.emit('error', new Error('boom'));
    147          *
    148          * // Prints: ok boom
    149          * ```
    150          *
    151          * An `AbortSignal` can be used to cancel waiting for the event:
    152          *
    153          * ```js
    154          * import { EventEmitter, once } from 'node:events';
    155          *
    156          * const ee = new EventEmitter();
    157          * const ac = new AbortController();
    158          *
    159          * async function foo(emitter, event, signal) {
    160          *   try {
    161          *     await once(emitter, event, { signal });
    162          *     console.log('event emitted!');
    163          *   } catch (error) {
    164          *     if (error.name === 'AbortError') {
    165          *       console.error('Waiting for the event was canceled!');
    166          *     } else {
    167          *       console.error('There was an error', error.message);
    168          *     }
    169          *   }
    170          * }
    171          *
    172          * foo(ee, 'foo', ac.signal);
    173          * ac.abort(); // Abort waiting for the event
    174          * ee.emit('foo'); // Prints: Waiting for the event was canceled!
    175          * ```
    176          * @since v11.13.0, v10.16.0
    177          */
    178         static once(
    179             emitter: NodeEventTarget,
    180             eventName: string | symbol,
    181             options?: StaticEventEmitterOptions,
    182         ): Promise<any[]>;
    183         static once(emitter: DOMEventTarget, eventName: string, options?: StaticEventEmitterOptions): Promise<any[]>;
    184         /**
    185          * ```js
    186          * import { on, EventEmitter } from 'node:events';
    187          *
    188          * (async () => {
    189          *   const ee = new EventEmitter();
    190          *
    191          *   // Emit later on
    192          *   process.nextTick(() => {
    193          *     ee.emit('foo', 'bar');
    194          *     ee.emit('foo', 42);
    195          *   });
    196          *
    197          *   for await (const event of on(ee, 'foo')) {
    198          *     // The execution of this inner block is synchronous and it
    199          *     // processes one event at a time (even with await). Do not use
    200          *     // if concurrent execution is required.
    201          *     console.log(event); // prints ['bar'] [42]
    202          *   }
    203          *   // Unreachable here
    204          * })();
    205          * ```
    206          *
    207          * Returns an `AsyncIterator` that iterates `eventName` events. It will throw
    208          * if the `EventEmitter` emits `'error'`. It removes all listeners when
    209          * exiting the loop. The `value` returned by each iteration is an array
    210          * composed of the emitted event arguments.
    211          *
    212          * An `AbortSignal` can be used to cancel waiting on events:
    213          *
    214          * ```js
    215          * import { on, EventEmitter } from 'node:events';
    216          * const ac = new AbortController();
    217          *
    218          * (async () => {
    219          *   const ee = new EventEmitter();
    220          *
    221          *   // Emit later on
    222          *   process.nextTick(() => {
    223          *     ee.emit('foo', 'bar');
    224          *     ee.emit('foo', 42);
    225          *   });
    226          *
    227          *   for await (const event of on(ee, 'foo', { signal: ac.signal })) {
    228          *     // The execution of this inner block is synchronous and it
    229          *     // processes one event at a time (even with await). Do not use
    230          *     // if concurrent execution is required.
    231          *     console.log(event); // prints ['bar'] [42]
    232          *   }
    233          *   // Unreachable here
    234          * })();
    235          *
    236          * process.nextTick(() => ac.abort());
    237          * ```
    238          * @since v13.6.0, v12.16.0
    239          * @param eventName The name of the event being listened for
    240          * @return that iterates `eventName` events emitted by the `emitter`
    241          */
    242         static on(
    243             emitter: NodeJS.EventEmitter,
    244             eventName: string,
    245             options?: StaticEventEmitterOptions,
    246         ): NodeJS.AsyncIterator<any>;
    247         /**
    248          * A class method that returns the number of listeners for the given `eventName`registered on the given `emitter`.
    249          *
    250          * ```js
    251          * import { EventEmitter, listenerCount } from 'node:events';
    252          * const myEmitter = new EventEmitter();
    253          * myEmitter.on('event', () => {});
    254          * myEmitter.on('event', () => {});
    255          * console.log(listenerCount(myEmitter, 'event'));
    256          * // Prints: 2
    257          * ```
    258          * @since v0.9.12
    259          * @deprecated Since v3.2.0 - Use `listenerCount` instead.
    260          * @param emitter The emitter to query
    261          * @param eventName The event name
    262          */
    263         static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
    264         /**
    265          * Returns a copy of the array of listeners for the event named `eventName`.
    266          *
    267          * For `EventEmitter`s this behaves exactly the same as calling `.listeners` on
    268          * the emitter.
    269          *
    270          * For `EventTarget`s this is the only way to get the event listeners for the
    271          * event target. This is useful for debugging and diagnostic purposes.
    272          *
    273          * ```js
    274          * import { getEventListeners, EventEmitter } from 'node:events';
    275          *
    276          * {
    277          *   const ee = new EventEmitter();
    278          *   const listener = () => console.log('Events are fun');
    279          *   ee.on('foo', listener);
    280          *   getEventListeners(ee, 'foo'); // [listener]
    281          * }
    282          * {
    283          *   const et = new EventTarget();
    284          *   const listener = () => console.log('Events are fun');
    285          *   et.addEventListener('foo', listener);
    286          *   getEventListeners(et, 'foo'); // [listener]
    287          * }
    288          * ```
    289          * @since v15.2.0
    290          */
    291         static getEventListeners(emitter: DOMEventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
    292         /**
    293          * ```js
    294          * import {
    295          *   setMaxListeners,
    296          *   EventEmitter
    297          * } from 'node:events';
    298          *
    299          * const target = new EventTarget();
    300          * const emitter = new EventEmitter();
    301          *
    302          * setMaxListeners(5, target, emitter);
    303          * ```
    304          * @since v15.4.0
    305          * @param n A non-negative number. The maximum number of listeners per `EventTarget` event.
    306          * @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}
    307          * objects.
    308          */
    309         static setMaxListeners(n?: number, ...eventTargets: Array<DOMEventTarget | NodeJS.EventEmitter>): void;
    310         /**
    311          * This symbol shall be used to install a listener for only monitoring `'error'`
    312          * events. Listeners installed using this symbol are called before the regular
    313          * `'error'` listeners are called.
    314          *
    315          * Installing a listener using this symbol does not change the behavior once an
    316          * `'error'` event is emitted, therefore the process will still crash if no
    317          * regular `'error'` listener is installed.
    318          */
    319         static readonly errorMonitor: unique symbol;
    320         static readonly captureRejectionSymbol: unique symbol;
    321         /**
    322          * Sets or gets the default captureRejection value for all emitters.
    323          */
    324         // TODO: These should be described using static getter/setter pairs:
    325         static captureRejections: boolean;
    326         static defaultMaxListeners: number;
    327     }
    328     import internal = require("node:events");
    329     namespace EventEmitter {
    330         // Should just be `export { EventEmitter }`, but that doesn't work in TypeScript 3.4
    331         export { internal as EventEmitter };
    332         export interface Abortable {
    333             /**
    334              * When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
    335              */
    336             signal?: AbortSignal | undefined;
    337         }
    338 
    339         export interface EventEmitterReferencingAsyncResource extends AsyncResource {
    340             readonly eventEmitter: EventEmitterAsyncResource;
    341         }
    342 
    343         export interface EventEmitterAsyncResourceOptions extends AsyncResourceOptions, EventEmitterOptions {
    344             /**
    345              * The type of async event, this is required when instantiating `EventEmitterAsyncResource`
    346              * directly rather than as a child class.
    347              * @default new.target.name if instantiated as a child class.
    348              */
    349             name?: string;
    350         }
    351 
    352         /**
    353          * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that require
    354          * manual async tracking. Specifically, all events emitted by instances of
    355          * `EventEmitterAsyncResource` will run within its async context.
    356          *
    357          * The EventEmitterAsyncResource class has the same methods and takes the
    358          * same options as EventEmitter and AsyncResource themselves.
    359          * @throws if `options.name` is not provided when instantiated directly.
    360          * @since v17.4.0, v16.14.0
    361          */
    362         export class EventEmitterAsyncResource extends EventEmitter {
    363             /**
    364              * @param options Only optional in child class.
    365              */
    366             constructor(options?: EventEmitterAsyncResourceOptions);
    367             /**
    368              * Call all destroy hooks. This should only ever be called once. An
    369              * error will be thrown if it is called more than once. This must be
    370              * manually called. If the resource is left to be collected by the GC then
    371              * the destroy hooks will never be called.
    372              */
    373             emitDestroy(): void;
    374             /** The unique asyncId assigned to the resource. */
    375             readonly asyncId: number;
    376             /** The same triggerAsyncId that is passed to the AsyncResource constructor. */
    377             readonly triggerAsyncId: number;
    378             /** The underlying AsyncResource */
    379             readonly asyncResource: EventEmitterReferencingAsyncResource;
    380         }
    381     }
    382     global {
    383         namespace NodeJS {
    384             interface EventEmitter<T extends EventMap<T> = DefaultEventMap> {
    385                 [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
    386                 /**
    387                  * Alias for `emitter.on(eventName, listener)`.
    388                  * @since v0.1.26
    389                  */
    390                 addListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    391                 /**
    392                  * Adds the `listener` function to the end of the listeners array for the
    393                  * event named `eventName`. No checks are made to see if the `listener` has
    394                  * already been added. Multiple calls passing the same combination of `eventName` and `listener` will result in the `listener` being added, and called, multiple
    395                  * times.
    396                  *
    397                  * ```js
    398                  * server.on('connection', (stream) => {
    399                  *   console.log('someone connected!');
    400                  * });
    401                  * ```
    402                  *
    403                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    404                  *
    405                  * By default, event listeners are invoked in the order they are added. The`emitter.prependListener()` method can be used as an alternative to add the
    406                  * event listener to the beginning of the listeners array.
    407                  *
    408                  * ```js
    409                  * const myEE = new EventEmitter();
    410                  * myEE.on('foo', () => console.log('a'));
    411                  * myEE.prependListener('foo', () => console.log('b'));
    412                  * myEE.emit('foo');
    413                  * // Prints:
    414                  * //   b
    415                  * //   a
    416                  * ```
    417                  * @since v0.1.101
    418                  * @param eventName The name of the event.
    419                  * @param listener The callback function
    420                  */
    421                 on<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    422                 /**
    423                  * Adds a **one-time**`listener` function for the event named `eventName`. The
    424                  * next time `eventName` is triggered, this listener is removed and then invoked.
    425                  *
    426                  * ```js
    427                  * server.once('connection', (stream) => {
    428                  *   console.log('Ah, we have our first user!');
    429                  * });
    430                  * ```
    431                  *
    432                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    433                  *
    434                  * By default, event listeners are invoked in the order they are added. The`emitter.prependOnceListener()` method can be used as an alternative to add the
    435                  * event listener to the beginning of the listeners array.
    436                  *
    437                  * ```js
    438                  * const myEE = new EventEmitter();
    439                  * myEE.once('foo', () => console.log('a'));
    440                  * myEE.prependOnceListener('foo', () => console.log('b'));
    441                  * myEE.emit('foo');
    442                  * // Prints:
    443                  * //   b
    444                  * //   a
    445                  * ```
    446                  * @since v0.3.0
    447                  * @param eventName The name of the event.
    448                  * @param listener The callback function
    449                  */
    450                 once<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    451                 /**
    452                  * Removes the specified `listener` from the listener array for the event named`eventName`.
    453                  *
    454                  * ```js
    455                  * const callback = (stream) => {
    456                  *   console.log('someone connected!');
    457                  * };
    458                  * server.on('connection', callback);
    459                  * // ...
    460                  * server.removeListener('connection', callback);
    461                  * ```
    462                  *
    463                  * `removeListener()` will remove, at most, one instance of a listener from the
    464                  * listener array. If any single listener has been added multiple times to the
    465                  * listener array for the specified `eventName`, then `removeListener()` must be
    466                  * called multiple times to remove each instance.
    467                  *
    468                  * Once an event is emitted, all listeners attached to it at the
    469                  * time of emitting are called in order. This implies that any`removeListener()` or `removeAllListeners()` calls _after_ emitting and_before_ the last listener finishes execution will
    470                  * not remove them from`emit()` in progress. Subsequent events behave as expected.
    471                  *
    472                  * ```js
    473                  * const myEmitter = new MyEmitter();
    474                  *
    475                  * const callbackA = () => {
    476                  *   console.log('A');
    477                  *   myEmitter.removeListener('event', callbackB);
    478                  * };
    479                  *
    480                  * const callbackB = () => {
    481                  *   console.log('B');
    482                  * };
    483                  *
    484                  * myEmitter.on('event', callbackA);
    485                  *
    486                  * myEmitter.on('event', callbackB);
    487                  *
    488                  * // callbackA removes listener callbackB but it will still be called.
    489                  * // Internal listener array at time of emit [callbackA, callbackB]
    490                  * myEmitter.emit('event');
    491                  * // Prints:
    492                  * //   A
    493                  * //   B
    494                  *
    495                  * // callbackB is now removed.
    496                  * // Internal listener array [callbackA]
    497                  * myEmitter.emit('event');
    498                  * // Prints:
    499                  * //   A
    500                  * ```
    501                  *
    502                  * Because listeners are managed using an internal array, calling this will
    503                  * change the position indices of any listener registered _after_ the listener
    504                  * being removed. This will not impact the order in which listeners are called,
    505                  * but it means that any copies of the listener array as returned by
    506                  * the `emitter.listeners()` method will need to be recreated.
    507                  *
    508                  * When a single function has been added as a handler multiple times for a single
    509                  * event (as in the example below), `removeListener()` will remove the most
    510                  * recently added instance. In the example the `once('ping')`listener is removed:
    511                  *
    512                  * ```js
    513                  * const ee = new EventEmitter();
    514                  *
    515                  * function pong() {
    516                  *   console.log('pong');
    517                  * }
    518                  *
    519                  * ee.on('ping', pong);
    520                  * ee.once('ping', pong);
    521                  * ee.removeListener('ping', pong);
    522                  *
    523                  * ee.emit('ping');
    524                  * ee.emit('ping');
    525                  * ```
    526                  *
    527                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    528                  * @since v0.1.26
    529                  */
    530                 removeListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    531                 /**
    532                  * Alias for `emitter.removeListener()`.
    533                  * @since v10.0.0
    534                  */
    535                 off<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    536                 /**
    537                  * Removes all listeners, or those of the specified `eventName`.
    538                  *
    539                  * It is bad practice to remove listeners added elsewhere in the code,
    540                  * particularly when the `EventEmitter` instance was created by some other
    541                  * component or module (e.g. sockets or file streams).
    542                  *
    543                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    544                  * @since v0.1.26
    545                  */
    546                 removeAllListeners(event?: Key<unknown, T>): this;
    547                 /**
    548                  * By default `EventEmitter`s will print a warning if more than `10` listeners are
    549                  * added for a particular event. This is a useful default that helps finding
    550                  * memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
    551                  * modified for this specific `EventEmitter` instance. The value can be set to`Infinity` (or `0`) to indicate an unlimited number of listeners.
    552                  *
    553                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    554                  * @since v0.3.5
    555                  */
    556                 setMaxListeners(n: number): this;
    557                 /**
    558                  * Returns the current max listener value for the `EventEmitter` which is either
    559                  * set by `emitter.setMaxListeners(n)` or defaults to {@link EventEmitter.defaultMaxListeners}.
    560                  * @since v1.0.0
    561                  */
    562                 getMaxListeners(): number;
    563                 /**
    564                  * Returns a copy of the array of listeners for the event named `eventName`.
    565                  *
    566                  * ```js
    567                  * server.on('connection', (stream) => {
    568                  *   console.log('someone connected!');
    569                  * });
    570                  * console.log(util.inspect(server.listeners('connection')));
    571                  * // Prints: [ [Function] ]
    572                  * ```
    573                  * @since v0.1.26
    574                  */
    575                 listeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
    576                 /**
    577                  * Returns a copy of the array of listeners for the event named `eventName`,
    578                  * including any wrappers (such as those created by `.once()`).
    579                  *
    580                  * ```js
    581                  * const emitter = new EventEmitter();
    582                  * emitter.once('log', () => console.log('log once'));
    583                  *
    584                  * // Returns a new Array with a function `onceWrapper` which has a property
    585                  * // `listener` which contains the original listener bound above
    586                  * const listeners = emitter.rawListeners('log');
    587                  * const logFnWrapper = listeners[0];
    588                  *
    589                  * // Logs "log once" to the console and does not unbind the `once` event
    590                  * logFnWrapper.listener();
    591                  *
    592                  * // Logs "log once" to the console and removes the listener
    593                  * logFnWrapper();
    594                  *
    595                  * emitter.on('log', () => console.log('log persistently'));
    596                  * // Will return a new Array with a single function bound by `.on()` above
    597                  * const newListeners = emitter.rawListeners('log');
    598                  *
    599                  * // Logs "log persistently" twice
    600                  * newListeners[0]();
    601                  * emitter.emit('log');
    602                  * ```
    603                  * @since v9.4.0
    604                  */
    605                 rawListeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
    606                 /**
    607                  * Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
    608                  * to each.
    609                  *
    610                  * Returns `true` if the event had listeners, `false` otherwise.
    611                  *
    612                  * ```js
    613                  * import EventEmitter from 'node:events';
    614                  * const myEmitter = new EventEmitter();
    615                  *
    616                  * // First listener
    617                  * myEmitter.on('event', function firstListener() {
    618                  *   console.log('Helloooo! first listener');
    619                  * });
    620                  * // Second listener
    621                  * myEmitter.on('event', function secondListener(arg1, arg2) {
    622                  *   console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
    623                  * });
    624                  * // Third listener
    625                  * myEmitter.on('event', function thirdListener(...args) {
    626                  *   const parameters = args.join(', ');
    627                  *   console.log(`event with parameters ${parameters} in third listener`);
    628                  * });
    629                  *
    630                  * console.log(myEmitter.listeners('event'));
    631                  *
    632                  * myEmitter.emit('event', 1, 2, 3, 4, 5);
    633                  *
    634                  * // Prints:
    635                  * // [
    636                  * //   [Function: firstListener],
    637                  * //   [Function: secondListener],
    638                  * //   [Function: thirdListener]
    639                  * // ]
    640                  * // Helloooo! first listener
    641                  * // event with parameters 1, 2 in second listener
    642                  * // event with parameters 1, 2, 3, 4, 5 in third listener
    643                  * ```
    644                  * @since v0.1.26
    645                  */
    646                 emit<K>(eventName: Key<K, T>, ...args: Args<K, T>): boolean;
    647                 /**
    648                  * Returns the number of listeners listening to the event named `eventName`.
    649                  * @since v3.2.0
    650                  * @param eventName The name of the event being listened for
    651                  */
    652                 listenerCount<K>(eventName: Key<K, T>, listener?: Listener2<K, T>): number;
    653                 /**
    654                  * Adds the `listener` function to the _beginning_ of the listeners array for the
    655                  * event named `eventName`. No checks are made to see if the `listener` has
    656                  * already been added. Multiple calls passing the same combination of `eventName` and `listener` will result in the `listener` being added, and called, multiple
    657                  * times.
    658                  *
    659                  * ```js
    660                  * server.prependListener('connection', (stream) => {
    661                  *   console.log('someone connected!');
    662                  * });
    663                  * ```
    664                  *
    665                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    666                  * @since v6.0.0
    667                  * @param eventName The name of the event.
    668                  * @param listener The callback function
    669                  */
    670                 prependListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    671                 /**
    672                  * 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
    673                  * listener is removed, and then invoked.
    674                  *
    675                  * ```js
    676                  * server.prependOnceListener('connection', (stream) => {
    677                  *   console.log('Ah, we have our first user!');
    678                  * });
    679                  * ```
    680                  *
    681                  * Returns a reference to the `EventEmitter`, so that calls can be chained.
    682                  * @since v6.0.0
    683                  * @param eventName The name of the event.
    684                  * @param listener The callback function
    685                  */
    686                 prependOnceListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
    687                 /**
    688                  * Returns an array listing the events for which the emitter has registered
    689                  * listeners. The values in the array are strings or `Symbol`s.
    690                  *
    691                  * ```js
    692                  * import EventEmitter from 'node:events';
    693                  * const myEE = new EventEmitter();
    694                  * myEE.on('foo', () => {});
    695                  * myEE.on('bar', () => {});
    696                  *
    697                  * const sym = Symbol('symbol');
    698                  * myEE.on(sym, () => {});
    699                  *
    700                  * console.log(myEE.eventNames());
    701                  * // Prints: [ 'foo', 'bar', Symbol(symbol) ]
    702                  * ```
    703                  * @since v6.0.0
    704                  */
    705                 eventNames(): Array<(string | symbol) & Key2<unknown, T>>;
    706             }
    707         }
    708     }
    709     export = EventEmitter;
    710 }
    711 declare module "node:events" {
    712     import events = require("events");
    713     export = events;
    714 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror