Skip to content

Commit 4fdcbae

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

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎lib/events.js‎

Lines changed: 9 additions & 6 deletions
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
functionEventEmitter(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
constthen=promise.then;
225228

226229
if(typeofthen==='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
case5: return[arr[0],arr[1],arr[2],arr[3],arr[4]];
671674
case6: return[arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]];
672675
}
673-
returnarr.slice();
676+
returnArrayPrototypeSlice(arr);
674677
}
675678

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

840843
// Wait until an event happens
841844
returnnewPromise(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‎

Lines changed: 5 additions & 3 deletions
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
typeoflistener==='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-
constresult=handler.callback.call(this,arg);
401+
constresult=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
functionaddCatch(that,promise,event){
567569
constthen=promise.then;
568570
if(typeofthen==='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‎

Lines changed: 4 additions & 3 deletions
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-
constenabledTracingObjects=newSet();
31+
constenabledTracingObjects=newSafeSet();
3132

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

6566
getcategories(){
66-
returnthis[kCategories].join(',');
67+
returnArrayPrototypeJoin(this[kCategories],',');
6768
}
6869

6970
[customInspectSymbol](depth,opts){

0 commit comments

Comments
 (0)