Skip to content

Commit

Permalink
events: refactor to use optional chaining
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <[email protected]>

PR-URL: #36763
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
Lxxyx authored and targos committed Jun 11, 2021
1 parent 740638d commit 18e5c0f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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');
}

Expand Down
15 changes: 5 additions & 10 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 18e5c0f

Please sign in to comment.