Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4fdcbae

Browse files
aduh95targos
authored andcommittedJun 11, 2021
events: refactor to use more primordials
PR-URL: #36304 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent a467125 commit 4fdcbae

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed
 

‎lib/events.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
'use strict';
2323

2424
const {
25+
ArrayPrototypeForEach,
2526
ArrayPrototypePush,
27+
ArrayPrototypeSlice,
2628
Boolean,
2729
Error,
2830
ErrorCaptureStackTrace,
31+
FunctionPrototypeCall,
2932
MathMin,
3033
NumberIsNaN,
3134
ObjectCreate,
@@ -81,7 +84,7 @@ const lazyDOMException = hideStackFrames((message, name) => {
8184

8285

8386
function EventEmitter(opts) {
84-
EventEmitter.init.call(this, opts);
87+
FunctionPrototypeCall(EventEmitter.init, this, opts);
8588
}
8689
module.exports = EventEmitter;
8790
module.exports.once = once;
@@ -173,7 +176,7 @@ EventEmitter.setMaxListeners =
173176
isEventTarget = require('internal/event_target').isEventTarget;
174177

175178
// Performance for forEach is now comparable with regular for-loop
176-
eventTargets.forEach((target) => {
179+
ArrayPrototypeForEach(eventTargets, (target) => {
177180
if (isEventTarget(target)) {
178181
target[kMaxEventTargetListeners] = n;
179182
target[kMaxEventTargetListenersWarned] = false;
@@ -224,7 +227,7 @@ function addCatch(that, promise, type, args) {
224227
const then = promise.then;
225228

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

676679
function unwrapListeners(arr) {
@@ -839,7 +842,7 @@ function on(emitter, event, options) {
839842

840843
// Wait until an event happens
841844
return new Promise(function(resolve, reject) {
842-
unconsumedPromises.push({ resolve, reject });
845+
ArrayPrototypePush(unconsumedPromises, { resolve, reject });
843846
});
844847
},
845848

@@ -903,7 +906,7 @@ function on(emitter, event, options) {
903906
if (promise) {
904907
promise.resolve(createIterResult(args, false));
905908
} else {
906-
unconsumedEvents.push(args);
909+
ArrayPrototypePush(unconsumedEvents, args);
907910
}
908911
}
909912

‎lib/internal/event_target.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const {
44
ArrayFrom,
55
Boolean,
66
Error,
7+
FunctionPrototypeBind,
8+
FunctionPrototypeCall,
79
NumberIsInteger,
810
ObjectAssign,
911
ObjectDefineProperties,
@@ -208,7 +210,7 @@ class Listener {
208210
this.callback =
209211
typeof listener === 'function' ?
210212
listener :
211-
listener.handleEvent.bind(listener);
213+
FunctionPrototypeBind(listener.handleEvent, listener);
212214
}
213215

214216
same(listener, capture) {
@@ -396,7 +398,7 @@ class EventTarget {
396398
} else {
397399
arg = createEvent();
398400
}
399-
const result = handler.callback.call(this, arg);
401+
const result = FunctionPrototypeCall(handler.callback, this, arg);
400402
if (result !== undefined && result !== null)
401403
addCatch(this, result, createEvent());
402404
} catch (err) {
@@ -566,7 +568,7 @@ function isEventTarget(obj) {
566568
function addCatch(that, promise, event) {
567569
const then = promise.then;
568570
if (typeof then === 'function') {
569-
then.call(promise, undefined, function(err) {
571+
FunctionPrototypeCall(then, promise, undefined, function(err) {
570572
// The callback is called with nextTick to avoid a follow-up
571573
// rejection from this promise.
572574
process.nextTick(emitUnhandledRejectionOrErr, that, err, event);

‎lib/trace_events.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const {
44
ArrayIsArray,
5-
Set,
5+
ArrayPrototypeJoin,
6+
SafeSet,
67
Symbol,
78
} = primordials;
89

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

30-
const enabledTracingObjects = new Set();
31+
const enabledTracingObjects = new SafeSet();
3132

3233
class Tracing {
3334
constructor(categories) {
@@ -63,7 +64,7 @@ class Tracing {
6364
}
6465

6566
get categories() {
66-
return this[kCategories].join(',');
67+
return ArrayPrototypeJoin(this[kCategories], ',');
6768
}
6869

6970
[customInspectSymbol](depth, opts) {

0 commit comments

Comments
 (0)
Please sign in to comment.