Skip to content

Commit

Permalink
Consolidate listener checks
Browse files Browse the repository at this point in the history
  • Loading branch information
friederbluemle committed Nov 18, 2019
1 parent 7b91104 commit 0a84716
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions events.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ EventEmitter.prototype._maxListeners = undefined;
// added to it. This is a useful default which helps finding memory leaks.
var defaultMaxListeners = 10;

function checkListener(listener) {
if (typeof listener !== 'function') {
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
}
}

Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
enumerable: true,
get: function() {
Expand Down Expand Up @@ -159,9 +165,7 @@ function _addListener(target, type, listener, prepend) {
var events;
var existing;

if (typeof listener !== 'function') {
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
}
checkListener(listener);

events = target._events;
if (events === undefined) {
Expand Down Expand Up @@ -248,18 +252,14 @@ function _onceWrap(target, type, listener) {
}

EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') {
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
}
checkListener(listener);
this.on(type, _onceWrap(this, type, listener));
return this;
};

EventEmitter.prototype.prependOnceListener =
function prependOnceListener(type, listener) {
if (typeof listener !== 'function') {
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
}
checkListener(listener);
this.prependListener(type, _onceWrap(this, type, listener));
return this;
};
Expand All @@ -269,9 +269,7 @@ EventEmitter.prototype.removeListener =
function removeListener(type, listener) {
var list, events, position, i, originalListener;

if (typeof listener !== 'function') {
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
}
checkListener(listener);

events = this._events;
if (events === undefined)
Expand Down

0 comments on commit 0a84716

Please sign in to comment.