Skip to content

Commit 3fa470a

Browse files
Lxxyxdanielleadams
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 21f3295 commit 3fa470a

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

‎lib/events.js‎

Lines changed: 6 additions & 6 deletions
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(typeofopts.captureRejections!=='boolean'){
208208
thrownewERR_INVALID_ARG_TYPE('options.captureRejections',
209209
'boolean',opts.captureRejections);
@@ -709,9 +709,9 @@ function getEventListeners(emitterOrTarget, type) {
709709
}
710710

711711
asyncfunctiononce(emitter,name,options={}){
712-
constsignal=options ? options.signal : undefined;
712+
constsignal=options?.signal;
713713
validateAbortSignal(signal,'options.signal');
714-
if(signal&&signal.aborted)
714+
if(signal?.aborted)
715715
throwlazyDOMException('The operation was aborted','AbortError');
716716
returnnewPromise((resolve,reject)=>{
717717
consterrorListener=(err)=>{
@@ -765,7 +765,7 @@ function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
765765

766766
functioneventTargetAgnosticAddListener(emitter,name,listener,flags){
767767
if(typeofemitter.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
functionon(emitter,event,options){
783-
const{signal }={ ...options};
783+
constsignal=options?.signal;
784784
validateAbortSignal(signal,'options.signal');
785-
if(signal&&signal.aborted){
785+
if(signal?.aborted){
786786
throwlazyDOMException('The operation was aborted','AbortError');
787787
}
788788

‎lib/internal/event_target.js‎

Lines changed: 5 additions & 10 deletions
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
classNodeCustomEventextendsEvent{
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-
constcapture=options!=null&&options.capture===true;
343+
constcapture=options?.capture===true;
347344

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

556553
functionshouldAddListener(listener){
557554
if(typeoflistener==='function'||
558-
(listener!=null&&
559-
typeoflistener==='object'&&
560-
typeoflistener.handleEvent==='function')){
555+
typeoflistener?.handleEvent==='function'){
561556
returntrue;
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
functionisEventTarget(obj){
589-
returnobj&&obj.constructor&&obj.constructor[kIsEventTarget];
584+
returnobj?.constructor?.[kIsEventTarget];
590585
}
591586

592587
functionaddCatch(that,promise,event){

0 commit comments

Comments
 (0)