diff --git a/lib/events.js b/lib/events.js index 1d253fdd33cea7..8a676773b6507a 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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); } @@ -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); } } @@ -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; } @@ -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; @@ -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 }