Skip to content

Commit

Permalink
fixup! events: refactor to use more primordials
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 18, 2020
1 parent e3d0bb1 commit 940bd12
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ const {
Boolean,
Error,
ErrorCaptureStackTrace,
Function,
FunctionPrototypeBind,
FunctionPrototypeCall,
FunctionPrototypeToString,
MathMin,
NumberIsNaN,
ObjectCreate,
Expand All @@ -49,6 +51,7 @@ const {
ReflectOwnKeys,
String,
StringPrototypeSplit,
StringPrototypeReplace,
Symbol,
SymbolFor,
SymbolAsyncIterator
Expand Down Expand Up @@ -90,9 +93,6 @@ const lazyDOMException = hideStackFrames((message, name) => {
});


function EventEmitter(opts) {
FunctionPrototypeCall(EventEmitter.init, this, opts);
}
module.exports = EventEmitter;
module.exports.once = once;
module.exports.on = on;
Expand Down Expand Up @@ -199,7 +199,9 @@ EventEmitter.setMaxListeners =
}
};

EventEmitter.init = function(opts) {
let customInit;
function EventEmitter(opts) {
if (customInit) return FunctionPrototypeCall(customInit, this, opts);

if (this._events === undefined ||
this._events === ObjectGetPrototypeOf(this)._events) {
Expand All @@ -221,7 +223,16 @@ EventEmitter.init = function(opts) {
// prototype lookup in a very sensitive hot path.
this[kCapture] = EventEmitter.prototype[kCapture];
}
};
}
ObjectDefineProperty(EventEmitter, 'init', {
enumerable: true,
get: () =>
new Function(StringPrototypeReplace(FunctionPrototypeToString(EventEmitter),
/^\s*if\s*\(customInit\)/, '')),
set: (init) => {
customInit = init;
}
});

function addCatch(that, promise, type, args) {
if (!that[kCapture]) {
Expand Down

0 comments on commit 940bd12

Please sign in to comment.