Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@types/node] Add missing optional parameters to EventEmitter.on #69514

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
28 changes: 26 additions & 2 deletions types/node/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,32 @@ declare module "events" {
captureRejections?: boolean | undefined;
}
interface StaticEventEmitterOptions {
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
/**
* Can be used to cancel awaiting events.
*/
signal?: AbortSignal | undefined;
}
interface StaticEventEmitterOnOptions extends StaticEventEmitterOptions {
/**
* Names of events that will end the iteration.
* @since v20.0.0
*/
close?: readonly string[] | undefined;
/**
* The high watermark. 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
* @since v20.0.0
*/
highWatermark?: number | undefined;
/**
* The low watermark. 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
* @since v20.0.0
*/
lowWatermark?: number | undefined;
}
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 @@ -260,8 +284,8 @@ declare module "events" {
*/
static on(
emitter: NodeJS.EventEmitter,
eventName: string,
options?: StaticEventEmitterOptions,
eventName: string | symbol,
options?: StaticEventEmitterOnOptions,
): AsyncIterableIterator<any>;
/**
* A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.
Expand Down
12 changes: 12 additions & 0 deletions types/node/test/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as events from "node:events";
const emitter: events = new events.EventEmitter();
declare const listener: (...args: any[]) => void;
declare const event: string | symbol;
declare const abortSignal: AbortSignal;
declare const any: any;

{
Expand Down Expand Up @@ -39,10 +40,21 @@ declare const any: any;
let result: Promise<number[]>;

result = events.once(emitter, event);
result = events.once(emitter, event, { signal: abortSignal });

emitter.emit(event, 42);
}

{
let result: AsyncIterableIterator<any>;

result = events.on(emitter, event);
result = events.on(emitter, event, { signal: abortSignal });
result = events.on(emitter, event, { close: ["close"] });
result = events.on(emitter, event, { highWatermark: 42 });
result = events.on(emitter, event, { lowWatermark: 42 });
}

{
let result: Function[];

Expand Down