Skip to content

Commit 18e5c0f

Browse files
Lxxyxtargos
authored andcommitted
events: refactor to use optional chaining
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36763 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 740638d commit 18e5c0f

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) => {
@@ -791,7 +791,7 @@ function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
791791

792792
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
793793
if (typeof emitter.on === 'function') {
794-
if (flags && flags.once) {
794+
if (flags?.once) {
795795
emitter.once(name, listener);
796796
} else {
797797
emitter.on(name, listener);
@@ -806,9 +806,9 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
806806
}
807807

808808
function on(emitter, event, options) {
809-
const { signal } = { ...options };
809+
const signal = options?.signal;
810810
validateAbortSignal(signal, 'options.signal');
811-
if (signal && signal.aborted) {
811+
if (signal?.aborted) {
812812
throw lazyDOMException('The operation was aborted', 'AbortError');
813813
}
814814

lib/internal/event_target.js

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

@@ -183,7 +183,7 @@ ObjectDefineProperty(Event.prototype, SymbolToStringTag, {
183183
class NodeCustomEvent extends Event {
184184
constructor(type, options) {
185185
super(type, options);
186-
if (options && options.detail) {
186+
if (options?.detail) {
187187
this.detail = options.detail;
188188
}
189189
}
@@ -323,10 +323,7 @@ class EventTarget {
323323
return;
324324

325325
type = String(type);
326-
// TODO(@jasnell): If it's determined this cannot be backported
327-
// to 12.x, then this can be simplified to:
328-
// const capture = Boolean(options?.capture);
329-
const capture = options != null && options.capture === true;
326+
const capture = options?.capture === true;
330327

331328
const root = this[kEvents].get(type);
332329
if (root === undefined || root.next === undefined)
@@ -532,9 +529,7 @@ ObjectDefineProperties(NodeEventTarget.prototype, {
532529

533530
function shouldAddListener(listener) {
534531
if (typeof listener === 'function' ||
535-
(listener != null &&
536-
typeof listener === 'object' &&
537-
typeof listener.handleEvent === 'function')) {
532+
typeof listener?.handleEvent === 'function') {
538533
return true;
539534
}
540535

@@ -562,7 +557,7 @@ function validateEventListenerOptions(options) {
562557
// It stands in its current implementation as a compromise.
563558
// Ref: https://github.com/nodejs/node/pull/33661
564559
function isEventTarget(obj) {
565-
return obj && obj.constructor && obj.constructor[kIsEventTarget];
560+
return obj?.constructor?.[kIsEventTarget];
566561
}
567562

568563
function addCatch(that, promise, event) {

0 commit comments

Comments
 (0)