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: extract listener check as a function #24303

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
29 changes: 13 additions & 16 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function lazyErrors() {
return errors;
}

function checkListener(listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
}

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

if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
checkListener(listener);

events = target._events;
if (events === undefined) {
Expand Down Expand Up @@ -283,20 +287,16 @@ function _onceWrap(target, type, listener) {
}

EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
checkListener(listener);

this.on(type, _onceWrap(this, type, listener));
return this;
};

EventEmitter.prototype.prependOnceListener =
function prependOnceListener(type, listener) {
if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
checkListener(listener);

this.prependListener(type, _onceWrap(this, type, listener));
return this;
};
Expand All @@ -306,10 +306,7 @@ EventEmitter.prototype.removeListener =
function removeListener(type, listener) {
var list, events, position, i, originalListener;

if (typeof listener !== 'function') {
const errors = lazyErrors();
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
}
checkListener(listener);

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