Skip to content

Commit

Permalink
events: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36304
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent a467125 commit 4fdcbae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lib/events.js
Original file line number Diff line number Diff line change
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 @@ -839,7 +842,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 @@ -903,7 +906,7 @@ function on(emitter, event, options) {
if (promise) {
promise.resolve(createIterResult(args, false));
} else {
unconsumedEvents.push(args);
ArrayPrototypePush(unconsumedEvents, args);
}
}

Expand Down
8 changes: 5 additions & 3 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {
ArrayFrom,
Boolean,
Error,
FunctionPrototypeBind,
FunctionPrototypeCall,
NumberIsInteger,
ObjectAssign,
ObjectDefineProperties,
Expand Down Expand Up @@ -208,7 +210,7 @@ class Listener {
this.callback =
typeof listener === 'function' ?
listener :
listener.handleEvent.bind(listener);
FunctionPrototypeBind(listener.handleEvent, listener);
}

same(listener, capture) {
Expand Down Expand Up @@ -396,7 +398,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 @@ -566,7 +568,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
Original file line number Diff line number Diff line change
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

0 comments on commit 4fdcbae

Please sign in to comment.