From 18e5c0f3e2e9cd68153ab34a5639e195ae615ee4 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Mon, 4 Jan 2021 13:50:51 +0800 Subject: [PATCH] events: refactor to use optional chaining Co-authored-by: Antoine du Hamel PR-URL: https://github.com/nodejs/node/pull/36763 Reviewed-By: Antoine du Hamel --- lib/events.js | 12 ++++++------ lib/internal/event_target.js | 15 +++++---------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lib/events.js b/lib/events.js index 9e60a1946ad7b1..18500888251b87 100644 --- a/lib/events.js +++ b/lib/events.js @@ -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) => { @@ -791,7 +791,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); @@ -806,9 +806,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'); } diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index b51bf836c810f4..8117b96a2d40a4 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -93,7 +93,7 @@ class Event { this[kDefaultPrevented] = false; this[kTimestamp] = lazyNow(); this[kPropagationStopped] = false; - if (options != null && options[kTrustEvent]) { + if (options?.[kTrustEvent]) { isTrustedSet.add(this); } @@ -183,7 +183,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; } } @@ -323,10 +323,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) @@ -532,9 +529,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; } @@ -562,7 +557,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) {