Skip to content

Commit 8f1dcac

Browse files
author
putzisan
committed
Add missing options close, highWaterMark and lowWaterMark to options interface for events.on
1 parent e881416 commit 8f1dcac

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

types/node/events.d.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ declare module "events" {
7878
interface StaticEventEmitterOptions {
7979
signal?: AbortSignal | undefined;
8080
}
81+
interface StaticEventEmitterIteratorOptions extends StaticEventEmitterOptions {
82+
/**
83+
* Names of events that will end the iteration.
84+
*/
85+
close?: string[];
86+
/**
87+
* 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.
88+
* @default Number.MAX_SAFE_INTEGER
89+
*/
90+
highWaterMark?: number;
91+
/**
92+
* 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.
93+
* @default 1
94+
*/
95+
lowWaterMark?: number
96+
}
8197
interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
8298
type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
8399
type DefaultEventMap = [never];
@@ -254,14 +270,36 @@ declare module "events" {
254270
*
255271
* process.nextTick(() => ac.abort());
256272
* ```
273+
*
274+
* Use the `close` option to specify an array of event names that will end the iteration:
275+
*
276+
* ```js
277+
* import { on, EventEmitter } from 'node:events';
278+
* import process from 'node:process';
279+
*
280+
* const ee = new EventEmitter();
281+
*
282+
* // Emit later on
283+
* process.nextTick(() => {
284+
* ee.emit('foo', 'bar');
285+
* ee.emit('foo', 42);
286+
* ee.emit('close');
287+
* });
288+
*
289+
* for await (const event of on(ee, 'foo', { close: ['close'] })) {
290+
* console.log(event); // prints ['bar'] [42]
291+
* }
292+
* // the loop will exit after 'close' is emitted
293+
* console.log('done'); // prints 'done'
294+
* ```
257295
* @since v13.6.0, v12.16.0
258296
* @param eventName The name of the event being listened for
259297
* @return An `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
260298
*/
261299
static on(
262300
emitter: NodeJS.EventEmitter,
263301
eventName: string,
264-
options?: StaticEventEmitterOptions,
302+
options?: StaticEventEmitterIteratorOptions,
265303
): AsyncIterableIterator<any>;
266304
/**
267305
* A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.

types/node/test/events.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ async function test() {
100100
console.log(e);
101101
}
102102
events.on(new events.EventEmitter(), "test", { signal: new AbortController().signal });
103+
events.on(new events.EventEmitter(), "test", { close: ["close"] });
104+
events.on(new events.EventEmitter(), "test", { highWaterMark: 42 });
105+
events.on(new events.EventEmitter(), "test", { lowWaterMark: 42 });
103106
}
104107

105108
{

0 commit comments

Comments
 (0)