Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: refactor to use more primordials #36304

Closed
wants to merge 13 commits into from
Closed
15 changes: 9 additions & 6 deletions lib/events.js
Expand Up @@ -22,10 +22,13 @@
'use strict';

const {
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeSlice,
Boolean,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeCall,
MathMin,
NumberIsNaN,
ObjectCreate,
Expand Down Expand Up @@ -81,7 +84,7 @@ const lazyDOMException = hideStackFrames((message, name) => {


function EventEmitter(opts) {
EventEmitter.init.call(this, opts);
FunctionPrototypeCall(EventEmitter.init, this, opts);
}
module.exports = EventEmitter;
module.exports.once = once;
Expand Down Expand Up @@ -173,7 +176,7 @@ EventEmitter.setMaxListeners =
isEventTarget = require('internal/event_target').isEventTarget;

// Performance for forEach is now comparable with regular for-loop
eventTargets.forEach((target) => {
ArrayPrototypeForEach(eventTargets, (target) => {
if (isEventTarget(target)) {
target[kMaxEventTargetListeners] = n;
target[kMaxEventTargetListenersWarned] = false;
Expand Down Expand Up @@ -224,7 +227,7 @@ function addCatch(that, promise, type, args) {
const then = promise.then;

if (typeof then === 'function') {
then.call(promise, undefined, function(err) {
FunctionPrototypeCall(then, promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
Expand Down Expand Up @@ -670,7 +673,7 @@ function arrayClone(arr) {
case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]];
case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
}
return arr.slice();
return ArrayPrototypeSlice(arr);
}

function unwrapListeners(arr) {
Expand Down Expand Up @@ -813,7 +816,7 @@ function on(emitter, event, options) {

// Wait until an event happens
return new Promise(function(resolve, reject) {
unconsumedPromises.push({ resolve, reject });
ArrayPrototypePush(unconsumedPromises, { resolve, reject });
});
},

Expand Down Expand Up @@ -877,7 +880,7 @@ function on(emitter, event, options) {
if (promise) {
promise.resolve(createIterResult(args, false));
} else {
unconsumedEvents.push(args);
ArrayPrototypePush(unconsumedEvents, args);
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/internal/event_target.js
Expand Up @@ -4,6 +4,7 @@ const {
ArrayFrom,
Boolean,
Error,
FunctionPrototypeBind,
FunctionPrototypeCall,
NumberIsInteger,
ObjectAssign,
Expand Down Expand Up @@ -212,7 +213,7 @@ class Listener {
this.callback =
typeof listener === 'function' ?
listener :
listener.handleEvent.bind(listener);
FunctionPrototypeBind(listener.handleEvent, listener);
}

same(listener, capture) {
Expand Down Expand Up @@ -419,7 +420,7 @@ class EventTarget {
} else {
arg = createEvent();
}
const result = handler.callback.call(this, arg);
const result = FunctionPrototypeCall(handler.callback, this, arg);
if (result !== undefined && result !== null)
addCatch(this, result, createEvent());
} catch (err) {
Expand Down Expand Up @@ -590,7 +591,7 @@ function isEventTarget(obj) {
function addCatch(that, promise, event) {
const then = promise.then;
if (typeof then === 'function') {
then.call(promise, undefined, function(err) {
FunctionPrototypeCall(then, promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, event);
Expand Down
7 changes: 4 additions & 3 deletions lib/trace_events.js
Expand Up @@ -2,7 +2,8 @@

const {
ArrayIsArray,
Set,
ArrayPrototypeJoin,
SafeSet,
Symbol,
} = primordials;

Expand All @@ -27,7 +28,7 @@ const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');

const enabledTracingObjects = new Set();
const enabledTracingObjects = new SafeSet();

class Tracing {
constructor(categories) {
Expand Down Expand Up @@ -63,7 +64,7 @@ class Tracing {
}

get categories() {
return this[kCategories].join(',');
return ArrayPrototypeJoin(this[kCategories], ',');
}

[customInspectSymbol](depth, opts) {
Expand Down