From dc79f3f37caf6f25b8efee4623bec31e2c20f595 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 6 Nov 2020 08:09:42 -0800 Subject: [PATCH] events: add max listener warning for EventTarget Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/36001 Fixes: https://github.com/nodejs/node/issues/35990 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott --- doc/api/events.md | 23 ++++++ lib/events.js | 47 +++++++++++ lib/internal/event_target.js | 59 +++++++------- .../test-eventtarget-memoryleakwarning.js | 79 +++++++++++++++++++ 4 files changed, 178 insertions(+), 30 deletions(-) create mode 100644 test/parallel/test-eventtarget-memoryleakwarning.js diff --git a/doc/api/events.md b/doc/api/events.md index 5c6b058123c..1ce815ce92c 100644 --- a/doc/api/events.md +++ b/doc/api/events.md @@ -383,6 +383,29 @@ Installing a listener using this symbol does not change the behavior once an `'error'` event is emitted, therefore the process will still crash if no regular `'error'` listener is installed. +### `EventEmitter.setMaxListeners(n[, ...eventTargets])` + + +* `n` {number} A non-negative number. The maximum number of listeners per + `EventTarget` event. +* `...eventsTargets` {EventTarget[]|EventEmitter[]} 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} objects. + +```js +const { + setMaxListeners, + EventEmitter +} = require('events'); + +const target = new EventTarget(); +const emitter = new EventEmitter(); + +setMaxListeners(5, target, emitter); +``` + ### `emitter.addListener(eventName, listener)`