Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion types/node/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ declare module "events" {
interface StaticEventEmitterOptions {
signal?: AbortSignal | undefined;
}
interface StaticEventEmitterIteratorOptions extends StaticEventEmitterOptions {
/**
* Names of events that will end the iteration.
*/
close?: string[];
/**
* The emitter is paused every time the size of events being buffered is higher than it. Supported only on emitters implementing pause() and resume() methods.
* @default Number.MAX_SAFE_INTEGER
*/
highWaterMark?: number;
/**
* The emitter is resumed every time the size of events being buffered is lower than it. Supported only on emitters implementing pause() and resume() methods.
* @default 1
*/
lowWaterMark?: number
}
interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
type DefaultEventMap = [never];
Expand Down Expand Up @@ -254,14 +270,36 @@ declare module "events" {
*
* process.nextTick(() => ac.abort());
* ```
*
* Use the `close` option to specify an array of event names that will end the iteration:
*
* ```js
* import { on, EventEmitter } from 'node:events';
* import process from 'node:process';
*
* const ee = new EventEmitter();
*
* // Emit later on
* process.nextTick(() => {
* ee.emit('foo', 'bar');
* ee.emit('foo', 42);
* ee.emit('close');
* });
*
* for await (const event of on(ee, 'foo', { close: ['close'] })) {
* console.log(event); // prints ['bar'] [42]
* }
* // the loop will exit after 'close' is emitted
* console.log('done'); // prints 'done'
* ```
* @since v13.6.0, v12.16.0
* @param eventName The name of the event being listened for
* @return An `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
*/
static on(
emitter: NodeJS.EventEmitter,
eventName: string,
options?: StaticEventEmitterOptions,
options?: StaticEventEmitterIteratorOptions,
): AsyncIterableIterator<any>;
/**
* A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.
Expand Down
3 changes: 3 additions & 0 deletions types/node/test/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ async function test() {
console.log(e);
}
events.on(new events.EventEmitter(), "test", { signal: new AbortController().signal });
events.on(new events.EventEmitter(), "test", { close: ["close"] });
events.on(new events.EventEmitter(), "test", { highWaterMark: 42 });
events.on(new events.EventEmitter(), "test", { lowWaterMark: 42 });
}

{
Expand Down