From 0a8471611f9a5fea1423f72d0ee131734b540bc1 Mon Sep 17 00:00:00 2001 From: Frieder Bluemle Date: Sun, 17 Nov 2019 17:17:01 -0800 Subject: [PATCH] Consolidate listener checks --- events.js | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/events.js b/events.js index 6dbc6eb..ef21503 100644 --- a/events.js +++ b/events.js @@ -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() { @@ -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) { @@ -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; }; @@ -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)