Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: refactor to use optional chaining #36763

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);
Expand All @@ -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');
}

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

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

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