Skip to content
Closed
Changes from 1 commit
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
Next Next commit
lib: load internal/errors eagerly in events.js
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.
  • Loading branch information
joyeecheung committed Mar 19, 2019
commit 5ead4a08175878c698f42157c2ff26effc24ce4d
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