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

events: add getMaxListeners method #47039

Merged
merged 4 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,57 @@ 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
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
event target.

```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 @@ -220,6 +220,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 @@ -930,6 +931,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);
}