GeezerVison is showing github.com — events: refactor to use more primordials · nodejs/node@82393ae
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/events.js +9-6Lines changed: 9 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | 'use strict'; | 23 | 23 |
| 24 | 24 | const { | | 25 | + ArrayPrototypeForEach, | 25 | 26 | ArrayPrototypePush, | | 27 | + ArrayPrototypeSlice, | 26 | 28 | Boolean, | 27 | 29 | Error, | 28 | 30 | ErrorCaptureStackTrace, | | 31 | + FunctionPrototypeCall, | 29 | 32 | MathMin, | 30 | 33 | NumberIsNaN, | 31 | 34 | ObjectCreate, | @@ -81,7 +84,7 @@ const lazyDOMException = hideStackFrames((message, name) => { | 81 | 84 |
| 82 | 85 |
| 83 | 86 | function EventEmitter(opts) { | 84 | | - EventEmitter.init.call(this, opts); | | 87 | + FunctionPrototypeCall(EventEmitter.init, this, opts); | 85 | 88 | } | 86 | 89 | module.exports = EventEmitter; | 87 | 90 | module.exports.once = once; | @@ -173,7 +176,7 @@ EventEmitter.setMaxListeners = | 173 | 176 | isEventTarget = require('internal/event_target').isEventTarget; | 174 | 177 |
| 175 | 178 | // Performance for forEach is now comparable with regular for-loop | 176 | | - eventTargets.forEach((target) => { | | 179 | + ArrayPrototypeForEach(eventTargets, (target) => { | 177 | 180 | if (isEventTarget(target)) { | 178 | 181 | target[kMaxEventTargetListeners] = n; | 179 | 182 | target[kMaxEventTargetListenersWarned] = false; | @@ -224,7 +227,7 @@ function addCatch(that, promise, type, args) { | 224 | 227 | const then = promise.then; | 225 | 228 |
| 226 | 229 | if (typeof then === 'function') { | 227 | | - then.call(promise, undefined, function(err) { | | 230 | + FunctionPrototypeCall(then, promise, undefined, function(err) { | 228 | 231 | // The callback is called with nextTick to avoid a follow-up | 229 | 232 | // rejection from this promise. | 230 | 233 | process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args); | @@ -670,7 +673,7 @@ function arrayClone(arr) { | 670 | 673 | case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]]; | 671 | 674 | case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]]; | 672 | 675 | } | 673 | | - return arr.slice(); | | 676 | + return ArrayPrototypeSlice(arr); | 674 | 677 | } | 675 | 678 |
| 676 | 679 | function unwrapListeners(arr) { | @@ -813,7 +816,7 @@ function on(emitter, event, options) { | 813 | 816 |
| 814 | 817 | // Wait until an event happens | 815 | 818 | return new Promise(function(resolve, reject) { | 816 | | - unconsumedPromises.push({ resolve, reject }); | | 819 | + ArrayPrototypePush(unconsumedPromises, { resolve, reject }); | 817 | 820 | }); | 818 | 821 | }, | 819 | 822 |
| @@ -877,7 +880,7 @@ function on(emitter, event, options) { | 877 | 880 | if (promise) { | 878 | 881 | promise.resolve(createIterResult(args, false)); | 879 | 882 | } else { | 880 | | - unconsumedEvents.push(args); | | 883 | + ArrayPrototypePush(unconsumedEvents, args); | 881 | 884 | } | 882 | 885 | } | 883 | 886 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/event_target.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | ArrayFrom, | 5 | 5 | Boolean, | 6 | 6 | Error, | | 7 | + FunctionPrototypeBind, | 7 | 8 | FunctionPrototypeCall, | 8 | 9 | NumberIsInteger, | 9 | 10 | ObjectAssign, | @@ -212,7 +213,7 @@ class Listener { | 212 | 213 | this.callback = | 213 | 214 | typeof listener === 'function' ? | 214 | 215 | listener : | 215 | | - listener.handleEvent.bind(listener); | | 216 | + FunctionPrototypeBind(listener.handleEvent, listener); | 216 | 217 | } | 217 | 218 |
| 218 | 219 | same(listener, capture) { | @@ -419,7 +420,7 @@ class EventTarget { | 419 | 420 | } else { | 420 | 421 | arg = createEvent(); | 421 | 422 | } | 422 | | - const result = handler.callback.call(this, arg); | | 423 | + const result = FunctionPrototypeCall(handler.callback, this, arg); | 423 | 424 | if (result !== undefined && result !== null) | 424 | 425 | addCatch(this, result, createEvent()); | 425 | 426 | } catch (err) { | @@ -590,7 +591,7 @@ function isEventTarget(obj) { | 590 | 591 | function addCatch(that, promise, event) { | 591 | 592 | const then = promise.then; | 592 | 593 | if (typeof then === 'function') { | 593 | | - then.call(promise, undefined, function(err) { | | 594 | + FunctionPrototypeCall(then, promise, undefined, function(err) { | 594 | 595 | // The callback is called with nextTick to avoid a follow-up | 595 | 596 | // rejection from this promise. | 596 | 597 | process.nextTick(emitUnhandledRejectionOrErr, that, err, event); | |
Collapse file Copy file name to clipboardExpand all lines: lib/trace_events.js +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayIsArray, | 5 | | - Set, | | 5 | + ArrayPrototypeJoin, | | 6 | + SafeSet, | 6 | 7 | Symbol, | 7 | 8 | } = primordials; | 8 | 9 |
| @@ -27,7 +28,7 @@ const { CategorySet, getEnabledCategories } = internalBinding('trace_events'); | 27 | 28 | const { customInspectSymbol } = require('internal/util'); | 28 | 29 | const { format } = require('internal/util/inspect'); | 29 | 30 |
| 30 | | -const enabledTracingObjects = new Set(); | | 31 | +const enabledTracingObjects = new SafeSet(); | 31 | 32 |
| 32 | 33 | class Tracing { | 33 | 34 | constructor(categories) { | @@ -63,7 +64,7 @@ class Tracing { | 63 | 64 | } | 64 | 65 |
| 65 | 66 | get categories() { | 66 | | - return this[kCategories].join(','); | | 67 | + return ArrayPrototypeJoin(this[kCategories], ','); | 67 | 68 | } | 68 | 69 |
| 69 | 70 | [customInspectSymbol](depth, opts) { | |
|