Skip to content

Commit 7022609

Browse files
joyeecheungtargos
authored andcommitted
events: load internal/errors eagerly
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: #26771 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent e49cd40 commit 7022609

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

lib/events.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323

2424
var spliceOne;
2525

26+
const {
27+
ERR_INVALID_ARG_TYPE,
28+
ERR_OUT_OF_RANGE,
29+
ERR_UNHANDLED_ERROR
30+
} = require('internal/errors').codes;
31+
2632
function EventEmitter() {
2733
EventEmitter.init.call(this);
2834
}
@@ -42,17 +48,9 @@ EventEmitter.prototype._maxListeners = undefined;
4248
// added to it. This is a useful default which helps finding memory leaks.
4349
var defaultMaxListeners = 10;
4450

45-
var errors;
46-
function lazyErrors() {
47-
if (errors === undefined)
48-
errors = require('internal/errors').codes;
49-
return errors;
50-
}
51-
5251
function checkListener(listener) {
5352
if (typeof listener !== 'function') {
54-
const errors = lazyErrors();
55-
throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
53+
throw new ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
5654
}
5755
}
5856

@@ -63,10 +61,9 @@ Object.defineProperty(EventEmitter, 'defaultMaxListeners', {
6361
},
6462
set: function(arg) {
6563
if (typeof arg !== 'number' || arg < 0 || Number.isNaN(arg)) {
66-
const errors = lazyErrors();
67-
throw new errors.ERR_OUT_OF_RANGE('defaultMaxListeners',
68-
'a non-negative number',
69-
arg);
64+
throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
65+
'a non-negative number',
66+
arg);
7067
}
7168
defaultMaxListeners = arg;
7269
}
@@ -87,8 +84,7 @@ EventEmitter.init = function() {
8784
// that to be increased. Set to zero for unlimited.
8885
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
8986
if (typeof n !== 'number' || n < 0 || Number.isNaN(n)) {
90-
const errors = lazyErrors();
91-
throw new errors.ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
87+
throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
9288
}
9389
this._maxListeners = n;
9490
return this;
@@ -183,8 +179,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
183179
}
184180

185181
// At least give some kind of context to the user
186-
const errors = lazyErrors();
187-
const err = new errors.ERR_UNHANDLED_ERROR(stringifiedEr);
182+
const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
188183
err.context = er;
189184
throw err; // Unhandled 'error' event
190185
}

0 commit comments

Comments
 (0)