Skip to content

Commit 3fa470a

Browse files
Lxxyxdanielleadams
authored andcommitted
events: refactor to use optional chaining
Co-authored-by: Antoine du Hamel <[email protected]> PR-URL: #36763 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 21f3295 commit 3fa470a

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

lib/events.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ EventEmitter.init = function(opts) {
203203
this._maxListeners = this._maxListeners || undefined;
204204

205205

206-
if (opts && opts.captureRejections) {
206+
if (opts?.captureRejections) {
207207
if (typeof opts.captureRejections !== 'boolean') {
208208
throw new ERR_INVALID_ARG_TYPE('options.captureRejections',
209209
'boolean', opts.captureRejections);
@@ -709,9 +709,9 @@ function getEventListeners(emitterOrTarget, type) {
709709
}
710710

711711
async function once(emitter, name, options = {}) {
712-
const signal = options ? options.signal : undefined;
712+
const signal = options?.signal;
713713
validateAbortSignal(signal, 'options.signal');
714-
if (signal && signal.aborted)
714+
if (signal?.aborted)
715715
throw lazyDOMException('The operation was aborted', 'AbortError');
716716
return new Promise((resolve, reject) => {
717717
const errorListener = (err) => {
@@ -765,7 +765,7 @@ function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
765765

766766
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
767767
if (typeof emitter.on === 'function') {
768-
if (flags && flags.once) {
768+
if (flags?.once) {
769769
emitter.once(name, listener);
770770
} else {
771771
emitter.on(name, listener);
@@ -780,9 +780,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
780780
}
781781

782782
function on(emitter, event, options) {
783-
const { signal } = { ...options };
783+
const signal = options?.signal;
784784
validateAbortSignal(signal, 'options.signal');
785-
if (signal && signal.aborted) {
785+
if (signal?.aborted) {
786786
throw lazyDOMException('The operation was aborted', 'AbortError');
787787
}
788788

lib/internal/event_target.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Event {
9595
this[kDefaultPrevented] = false;
9696
this[kTimestamp] = lazyNow();
9797
this[kPropagationStopped] = false;
98-
if (options != null && options[kTrustEvent]) {
98+
if (options?.[kTrustEvent]) {
9999
isTrustedSet.add(this);
100100
}
101101

@@ -185,7 +185,7 @@ ObjectDefineProperty(Event.prototype, SymbolToStringTag, {
185185
class NodeCustomEvent extends Event {
186186
constructor(type, options) {
187187
super(type, options);
188-
if (options && options.detail) {
188+
if (options?.detail) {
189189
this.detail = options.detail;
190190
}
191191
}
@@ -340,10 +340,7 @@ class EventTarget {
340340
return;
341341

342342
type = String(type);
343-
// TODO(@jasnell): If it's determined this cannot be backported
344-
// to 12.x, then this can be simplified to:
345-
// const capture = Boolean(options?.capture);
346-
const capture = options != null && options.capture === true;
343+
const capture = options?.capture === true;
347344

348345
const root = this[kEvents].get(type);
349346
if (root === undefined || root.next === undefined)
@@ -555,9 +552,7 @@ ObjectDefineProperties(NodeEventTarget.prototype, {
555552

556553
function shouldAddListener(listener) {
557554
if (typeof listener === 'function' ||
558-
(listener != null &&
559-
typeof listener === 'object' &&
560-
typeof listener.handleEvent === 'function')) {
555+
typeof listener?.handleEvent === 'function') {
561556
return true;
562557
}
563558

@@ -586,7 +581,7 @@ function validateEventListenerOptions(options) {
586581
// It stands in its current implementation as a compromise.
587582
// Ref: https://github.com/nodejs/node/pull/33661
588583
function isEventTarget(obj) {
589-
return obj && obj.constructor && obj.constructor[kIsEventTarget];
584+
return obj?.constructor?.[kIsEventTarget];
590585
}
591586

592587
function addCatch(that, promise, event) {

0 commit comments

Comments
 (0)