Skip to content

Commit

Permalink
events: add getMaxListeners method
Browse files Browse the repository at this point in the history
PR-URL: #47039
Reviewed-By: Debadree Chatterjee <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
KhafraDev authored and RafaelGSS committed Apr 7, 2023
1 parent 10569de commit c5cd6b7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
52 changes: 52 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,58 @@ const { getEventListeners, EventEmitter } = require('node:events');
}
```

## `events.getMaxListeners(emitterOrTarget)`

<!-- YAML
added: REPLACEME
-->

* `emitterOrTarget` {EventEmitter|EventTarget}
* Returns: {number}

Returns the currently set max amount of listeners.

For `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on
the emitter.

For `EventTarget`s this is the only way to get the max event listeners for the
event target. If the number of event handlers on a single EventTarget exceeds
the max set, the EventTarget will print a warning.

```mjs
import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';

{
const ee = new EventEmitter();
console.log(getMaxListeners(ee)); // 10
setMaxListeners(11, ee);
console.log(getMaxListeners(ee)); // 11
}
{
const et = new EventTarget();
console.log(getMaxListeners(et)); // 10
setMaxListeners(11, et);
console.log(getMaxListeners(et)); // 11
}
```

```cjs
const { getMaxListeners, setMaxListeners, EventEmitter } = require('node:events');

{
const ee = new EventEmitter();
console.log(getMaxListeners(ee)); // 10
setMaxListeners(11, ee);
console.log(getMaxListeners(ee)); // 11
}
{
const et = new EventTarget();
console.log(getMaxListeners(et)); // 10
setMaxListeners(11, et);
console.log(getMaxListeners(et)); // 11
}
```

## `events.once(emitter, name[, options])`

<!-- YAML
Expand Down
18 changes: 18 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ module.exports = EventEmitter;
module.exports.once = once;
module.exports.on = on;
module.exports.getEventListeners = getEventListeners;
module.exports.getMaxListeners = getMaxListeners;
// Backwards-compat with node 0.10.x
EventEmitter.EventEmitter = EventEmitter;

Expand Down Expand Up @@ -932,6 +933,23 @@ function getEventListeners(emitterOrTarget, type) {
emitterOrTarget);
}

/**
* Returns the max listeners set.
* @param {EventEmitter | EventTarget} emitterOrTarget
* @returns {number}
*/
function getMaxListeners(emitterOrTarget) {
if (typeof emitterOrTarget?.getMaxListeners === 'function') {
return _getMaxListeners(emitterOrTarget);
} else if (emitterOrTarget?.[kMaxEventTargetListeners]) {
return emitterOrTarget[kMaxEventTargetListeners];
}

throw new ERR_INVALID_ARG_TYPE('emitter',
['EventEmitter', 'EventTarget'],
emitterOrTarget);
}

/**
* Creates a `Promise` that is fulfilled when the emitter
* emits the given event.
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-events-getmaxlisteners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

require('../common');
const assert = require('node:assert');
const { getMaxListeners, EventEmitter, defaultMaxListeners, setMaxListeners } = require('node:events');

{
const ee = new EventEmitter();
assert.strictEqual(getMaxListeners(ee), defaultMaxListeners);
setMaxListeners(101, ee);
assert.strictEqual(getMaxListeners(ee), 101);
}

{
const et = new EventTarget();
assert.strictEqual(getMaxListeners(et), defaultMaxListeners);
setMaxListeners(101, et);
assert.strictEqual(getMaxListeners(et), 101);
}

0 comments on commit c5cd6b7

Please sign in to comment.