events: refactor to use optional chaining

Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/36763
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
ZiJian LiuandAntoine du Hamel authored and Danielle Adams committed 2021-01-12 07:11:40 -05:00
1 parent 21f329532f
commit 3fa470a3c9
2 files changed
+11 -16

No files matched your search

+6 -6
View File
@@ -203,7 +203,7 @@ EventEmitter.init = function(opts) {
this._maxListeners = this._maxListeners || undefined;
if (opts && opts.captureRejections) {
if (opts?.captureRejections) {
if (typeof opts.captureRejections !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE('options.captureRejections',
'boolean', opts.captureRejections);
@@ -709,9 +709,9 @@ function getEventListeners(emitterOrTarget, type) {
}
async function once(emitter, name, options = {}) {
const signal = options ? options.signal : undefined;
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal && signal.aborted)
if (signal?.aborted)
throw lazyDOMException('The operation was aborted', 'AbortError');
return new Promise((resolve, reject) => {
const errorListener = (err) => {
@@ -765,7 +765,7 @@ function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
if (typeof emitter.on === 'function') {
if (flags && flags.once) {
if (flags?.once) {
emitter.once(name, listener);
} else {
emitter.on(name, listener);
@@ -780,9 +780,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
}
function on(emitter, event, options) {
const { signal } = { ...options };
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal && signal.aborted) {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
+5 -10
View File
@@ -95,7 +95,7 @@ class Event {
this[kDefaultPrevented] = false;
this[kTimestamp] = lazyNow();
this[kPropagationStopped] = false;
if (options != null && options[kTrustEvent]) {
if (options?.[kTrustEvent]) {
isTrustedSet.add(this);
}
@@ -185,7 +185,7 @@ ObjectDefineProperty(Event.prototype, SymbolToStringTag, {
class NodeCustomEvent extends Event {
constructor(type, options) {
super(type, options);
if (options && options.detail) {
if (options?.detail) {
this.detail = options.detail;
}
}
@@ -340,10 +340,7 @@ class EventTarget {
return;
type = String(type);
// TODO(@jasnell): If it's determined this cannot be backported
// to 12.x, then this can be simplified to:
// const capture = Boolean(options?.capture);
const capture = options != null && options.capture === true;
const capture = options?.capture === true;
const root = this[kEvents].get(type);
if (root === undefined || root.next === undefined)
@@ -555,9 +552,7 @@ ObjectDefineProperties(NodeEventTarget.prototype, {
function shouldAddListener(listener) {
if (typeof listener === 'function' ||
(listener != null &&
typeof listener === 'object' &&
typeof listener.handleEvent === 'function')) {
typeof listener?.handleEvent === 'function') {
return true;
}
@@ -586,7 +581,7 @@ function validateEventListenerOptions(options) {
// It stands in its current implementation as a compromise.
// Ref: https://github.com/nodejs/node/pull/33661
function isEventTarget(obj) {
return obj && obj.constructor && obj.constructor[kIsEventTarget];
return obj?.constructor?.[kIsEventTarget];
}
function addCatch(that, promise, event) {