Skip to content

Commit 940bd12

Browse files
committed
fixup! events: refactor to use more primordials
1 parent e3d0bb1 commit 940bd12

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/events.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ const {
3333
Boolean,
3434
Error,
3535
ErrorCaptureStackTrace,
36+
Function,
3637
FunctionPrototypeBind,
3738
FunctionPrototypeCall,
39+
FunctionPrototypeToString,
3840
MathMin,
3941
NumberIsNaN,
4042
ObjectCreate,
@@ -49,6 +51,7 @@ const {
4951
ReflectOwnKeys,
5052
String,
5153
StringPrototypeSplit,
54+
StringPrototypeReplace,
5255
Symbol,
5356
SymbolFor,
5457
SymbolAsyncIterator
@@ -90,9 +93,6 @@ const lazyDOMException = hideStackFrames((message, name) => {
9093
});
9194

9295

93-
function EventEmitter(opts) {
94-
FunctionPrototypeCall(EventEmitter.init, this, opts);
95-
}
9696
module.exports = EventEmitter;
9797
module.exports.once = once;
9898
module.exports.on = on;
@@ -199,7 +199,9 @@ EventEmitter.setMaxListeners =
199199
}
200200
};
201201

202-
EventEmitter.init = function(opts) {
202+
let customInit;
203+
function EventEmitter(opts) {
204+
if (customInit) return FunctionPrototypeCall(customInit, this, opts);
203205

204206
if (this._events === undefined ||
205207
this._events === ObjectGetPrototypeOf(this)._events) {
@@ -221,7 +223,16 @@ EventEmitter.init = function(opts) {
221223
// prototype lookup in a very sensitive hot path.
222224
this[kCapture] = EventEmitter.prototype[kCapture];
223225
}
224-
};
226+
}
227+
ObjectDefineProperty(EventEmitter, 'init', {
228+
enumerable: true,
229+
get: () =>
230+
new Function(StringPrototypeReplace(FunctionPrototypeToString(EventEmitter),
231+
/^\s*if\s*\(customInit\)/, '')),
232+
set: (init) => {
233+
customInit = init;
234+
}
235+
});
225236

226237
function addCatch(that, promise, type, args) {
227238
if (!that[kCapture]) {

0 commit comments

Comments
 (0)