Skip to content

Commit

Permalink
events: add hasListener method
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 9, 2023
1 parent 5092346 commit 3594b21
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,18 @@ Returns the current max listener value for the `EventEmitter` which is either
set by [`emitter.setMaxListeners(n)`][] or defaults to
[`events.defaultMaxListeners`][].

### `emitter.hasListener(eventName, listener)`

<!-- YAML
added: REPLACEME
-->

* `eventName` {string|symbol} The name of the event being checked
* `listener` {Function} The listener to check

Returns `true` if the listener is already register for the event,
`false` otherwise.

### `emitter.listenerCount(eventName)`

<!-- YAML
Expand Down
25 changes: 25 additions & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,31 @@ function listenerCount(type) {
return 0;
}

/**
* Returns true if the function is register as listener for the event
*
* @param {string | symbol} type
* @param {Function} listener
* @returns {boolean}
*/
EventEmitter.prototype.hasListener = function hasListener(type, listener) {
const list = this._events?.[type];

if (list === undefined) {
return false;
} else if (list === listener || list.listener === listener) {
return true;
} else if (typeof list !== 'function') {
for (let i = 0, l = list.length; i < l; i++) {
if (list[i] === listener || list[i].listener === listener) {
return true;
}
}
}

return false;
};

/**
* Returns an array listing the events for which
* the emitter has registered listeners.
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-events-has-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');
const EventEmitter = require('events');
const assert = require('assert');

const EE = new EventEmitter();
const handler = common.mustCall(undefined, 2);
const anotherHandler = common.mustCall();

assert.strictEqual(EE.hasListener('event', handler), false);
assert.strictEqual(EE.hasListener('event', anotherHandler), false);

EE.on('event', handler);

assert.strictEqual(EE.hasListener('event', handler), true);
assert.strictEqual(EE.hasListener('event', anotherHandler), false);

EE.once('event', anotherHandler);

assert.strictEqual(EE.hasListener('event', handler), true);
assert.strictEqual(EE.hasListener('event', anotherHandler), true);

assert.strictEqual(EE.hasListener('another-event', handler), false);
assert.strictEqual(EE.hasListener('another-event', anotherHandler), false);

EE.emit('event');

assert.strictEqual(EE.hasListener('event', handler), true);
assert.strictEqual(EE.hasListener('event', anotherHandler), false);

EE.emit('event');

assert.strictEqual(EE.hasListener('event', handler), true);
assert.strictEqual(EE.hasListener('event', anotherHandler), false);

EE.off('event', handler);

assert.strictEqual(EE.hasListener('event', handler), false);
assert.strictEqual(EE.hasListener('event', anotherHandler), false);

0 comments on commit 3594b21

Please sign in to comment.