Skip to content

Commit

Permalink
events: load internal/errors eagerly
Browse files Browse the repository at this point in the history
Since `internal/errors` is loaded by many builtin modules and is
currently the first module loaded during bootstrap, it is
fine to load it eagerly. We just need to make sure
that `internal/errors` itself load other modules lazily.

PR-URL: nodejs#26771
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
joyeecheung authored and targos committed Mar 27, 2019
1 parent 6e16980 commit 8c30c09
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

var spliceOne;

const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE,
ERR_UNHANDLED_ERROR
} = require('internal/errors').codes;

function EventEmitter() {
EventEmitter.init.call(this);
}
Expand All @@ -42,17 +48,9 @@ EventEmitter.prototype._maxListeners = undefined;
// added to it. This is a useful default which helps finding memory leaks.
var defaultMaxListeners = 10;

var errors;
function lazyErrors() {
if (errors === undefined)
errors = require('internal/errors').codes;
return errors;
}

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

Expand All @@ -63,10 +61,9 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
},
set: function(arg) {
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
const errors = lazyErrors();
throw new errors.ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
'a non-negative number',
arg);
}
defaultMaxListeners = arg;
}
Expand All @@ -87,8 +84,7 @@ EventEmitter.init = function() {
// that to be increased. Set to zero for unlimited.
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
const errors = lazyErrors();
throw new errors.ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
}
this._maxListeners = n;
return this;
Expand Down Expand Up @@ -183,8 +179,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
}

// At least give some kind of context to the user
const errors = lazyErrors();
const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr);
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
err.context = er;
throw err; // Unhandled 'error' event
}
Expand Down

0 comments on commit 8c30c09

Please sign in to comment.