@@ -79,6 +79,11 @@ const lazyDOMException = hideStackFrames((message, name) => {
79
79
} ) ;
80
80
81
81
82
+ /**
83
+ * Creates a new `EventEmitter` instance.
84
+ * @param {{ captureRejections?: boolean; } } [opts]
85
+ * @returns {EventEmitter }
86
+ */
82
87
function EventEmitter ( opts ) {
83
88
EventEmitter . init . call ( this , opts ) ;
84
89
}
@@ -161,6 +166,12 @@ ObjectDefineProperties(EventEmitter, {
161
166
}
162
167
} ) ;
163
168
169
+ /**
170
+ * Sets the max listeners.
171
+ * @param {number } n
172
+ * @param {EventTarget[] | EventEmitter[] } [eventTargets]
173
+ * @returns {void }
174
+ */
164
175
EventEmitter . setMaxListeners =
165
176
function ( n = defaultMaxListeners , ...eventTargets ) {
166
177
if ( typeof n !== 'number' || n < 0 || NumberIsNaN ( n ) )
@@ -255,8 +266,11 @@ function emitUnhandledRejectionOrErr(ee, err, type, args) {
255
266
}
256
267
}
257
268
258
- // Obviously not all Emitters should be limited to 10. This function allows
259
- // that to be increased. Set to zero for unlimited.
269
+ /**
270
+ * Increases the max listeners of the event emitter.
271
+ * @param {number } n
272
+ * @returns {EventEmitter }
273
+ */
260
274
EventEmitter . prototype . setMaxListeners = function setMaxListeners ( n ) {
261
275
if ( typeof n !== 'number' || n < 0 || NumberIsNaN ( n ) ) {
262
276
throw new ERR_OUT_OF_RANGE ( 'n' , 'a non-negative number' , n ) ;
@@ -271,6 +285,10 @@ function _getMaxListeners(that) {
271
285
return that . _maxListeners ;
272
286
}
273
287
288
+ /**
289
+ * Returns the current max listener value for the event emitter.
290
+ * @returns {number }
291
+ */
274
292
EventEmitter . prototype . getMaxListeners = function getMaxListeners ( ) {
275
293
return _getMaxListeners ( this ) ;
276
294
} ;
@@ -321,6 +339,13 @@ function enhanceStackTrace(err, own) {
321
339
return err . stack + sep + ownStack . join ( '\n' ) ;
322
340
}
323
341
342
+ /**
343
+ * Synchronously calls each of the listeners registered
344
+ * for the event.
345
+ * @param {string | symbol } type
346
+ * @param {...any } [args]
347
+ * @returns {boolean }
348
+ */
324
349
EventEmitter . prototype . emit = function emit ( type , ...args ) {
325
350
let doError = ( type === 'error' ) ;
326
351
@@ -462,12 +487,25 @@ function _addListener(target, type, listener, prepend) {
462
487
return target ;
463
488
}
464
489
490
+ /**
491
+ * Adds a listener to the event emitter.
492
+ * @param {string | symbol } type
493
+ * @param {Function } listener
494
+ * @returns {EventEmitter }
495
+ */
465
496
EventEmitter . prototype . addListener = function addListener ( type , listener ) {
466
497
return _addListener ( this , type , listener , false ) ;
467
498
} ;
468
499
469
500
EventEmitter . prototype . on = EventEmitter . prototype . addListener ;
470
501
502
+ /**
503
+ * Adds the `listener` function to the beginning of
504
+ * the listeners array.
505
+ * @param {string | symbol } type
506
+ * @param {Function } listener
507
+ * @returns {EventEmitter }
508
+ */
471
509
EventEmitter . prototype . prependListener =
472
510
function prependListener ( type , listener ) {
473
511
return _addListener ( this , type , listener , true ) ;
@@ -491,13 +529,26 @@ function _onceWrap(target, type, listener) {
491
529
return wrapped ;
492
530
}
493
531
532
+ /**
533
+ * Adds a one-time `listener` function to the event emitter.
534
+ * @param {string | symbol } type
535
+ * @param {Function } listener
536
+ * @returns {EventEmitter }
537
+ */
494
538
EventEmitter . prototype . once = function once ( type , listener ) {
495
539
checkListener ( listener ) ;
496
540
497
541
this . on ( type , _onceWrap ( this , type , listener ) ) ;
498
542
return this ;
499
543
} ;
500
544
545
+ /**
546
+ * Adds a one-time `listener` function to the beginning of
547
+ * the listeners array.
548
+ * @param {string | symbol } type
549
+ * @param {Function } listener
550
+ * @returns {EventEmitter }
551
+ */
501
552
EventEmitter . prototype . prependOnceListener =
502
553
function prependOnceListener ( type , listener ) {
503
554
checkListener ( listener ) ;
@@ -506,7 +557,12 @@ EventEmitter.prototype.prependOnceListener =
506
557
return this ;
507
558
} ;
508
559
509
- // Emits a 'removeListener' event if and only if the listener was removed.
560
+ /**
561
+ * Removes the specified `listener` from the listeners array.
562
+ * @param {string | symbol } type
563
+ * @param {Function } listener
564
+ * @returns {EventEmitter }
565
+ */
510
566
EventEmitter . prototype . removeListener =
511
567
function removeListener ( type , listener ) {
512
568
checkListener ( listener ) ;
@@ -560,6 +616,13 @@ EventEmitter.prototype.removeListener =
560
616
561
617
EventEmitter . prototype . off = EventEmitter . prototype . removeListener ;
562
618
619
+ /**
620
+ * Removes all listeners from the event emitter. (Only
621
+ * removes listeners for a specific event name if specified
622
+ * as `type`).
623
+ * @param {string | symbol } [type]
624
+ * @returns {EventEmitter }
625
+ */
563
626
EventEmitter . prototype . removeAllListeners =
564
627
function removeAllListeners ( type ) {
565
628
const events = this . _events ;
@@ -623,14 +686,34 @@ function _listeners(target, type, unwrap) {
623
686
unwrapListeners ( evlistener ) : arrayClone ( evlistener ) ;
624
687
}
625
688
689
+ /**
690
+ * Returns a copy of the array of listeners for the event name
691
+ * specified as `type`.
692
+ * @param {string | symbol } type
693
+ * @returns {Function[] }
694
+ */
626
695
EventEmitter . prototype . listeners = function listeners ( type ) {
627
696
return _listeners ( this , type , true ) ;
628
697
} ;
629
698
699
+ /**
700
+ * Returns a copy of the array of listeners and wrappers for
701
+ * the event name specified as `type`.
702
+ * @param {string | symbol } type
703
+ * @returns {Function[] }
704
+ */
630
705
EventEmitter . prototype . rawListeners = function rawListeners ( type ) {
631
706
return _listeners ( this , type , false ) ;
632
707
} ;
633
708
709
+ /**
710
+ * Returns the number of listeners listening to the event name
711
+ * specified as `type`.
712
+ * @deprecated since v3.2.0
713
+ * @param {EventEmitter } emitter
714
+ * @param {string | symbol } type
715
+ * @returns {number }
716
+ */
634
717
EventEmitter . listenerCount = function ( emitter , type ) {
635
718
if ( typeof emitter . listenerCount === 'function' ) {
636
719
return emitter . listenerCount ( type ) ;
@@ -639,6 +722,13 @@ EventEmitter.listenerCount = function(emitter, type) {
639
722
} ;
640
723
641
724
EventEmitter . prototype . listenerCount = listenerCount ;
725
+
726
+ /**
727
+ * Returns the number of listeners listening to event name
728
+ * specified as `type`.
729
+ * @param {string | symbol } type
730
+ * @returns {number }
731
+ */
642
732
function listenerCount ( type ) {
643
733
const events = this . _events ;
644
734
@@ -655,6 +745,11 @@ function listenerCount(type) {
655
745
return 0 ;
656
746
}
657
747
748
+ /**
749
+ * Returns an array listing the events for which
750
+ * the emitter has registered listeners.
751
+ * @returns {any[] }
752
+ */
658
753
EventEmitter . prototype . eventNames = function eventNames ( ) {
659
754
return this . _eventsCount > 0 ? ReflectOwnKeys ( this . _events ) : [ ] ;
660
755
} ;
@@ -682,6 +777,13 @@ function unwrapListeners(arr) {
682
777
return ret ;
683
778
}
684
779
780
+ /**
781
+ * Returns a copy of the array of listeners for the event name
782
+ * specified as `type`.
783
+ * @param {EventEmitter | EventTarget } emitterOrTarget
784
+ * @param {string | symbol } type
785
+ * @returns {Function[] }
786
+ */
685
787
function getEventListeners ( emitterOrTarget , type ) {
686
788
// First check if EventEmitter
687
789
if ( typeof emitterOrTarget . listeners === 'function' ) {
@@ -704,6 +806,14 @@ function getEventListeners(emitterOrTarget, type) {
704
806
emitterOrTarget ) ;
705
807
}
706
808
809
+ /**
810
+ * Creates a `Promise` that is fulfilled when the emitter
811
+ * emits the given event.
812
+ * @param {EventEmitter } emitter
813
+ * @param {string } name
814
+ * @param {{ signal: AbortSignal; } } [options]
815
+ * @returns {Promise }
816
+ */
707
817
async function once ( emitter , name , options = { } ) {
708
818
const signal = options ?. signal ;
709
819
validateAbortSignal ( signal , 'options.signal' ) ;
@@ -801,6 +911,13 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
801
911
}
802
912
}
803
913
914
+ /**
915
+ * Returns an `AsyncIterator` that iterates `event` events.
916
+ * @param {EventEmitter } emitter
917
+ * @param {string | symbol } event
918
+ * @param {{ signal: AbortSignal; } } [options]
919
+ * @returns {AsyncIterator }
920
+ */
804
921
function on ( emitter , event , options ) {
805
922
const signal = options ?. signal ;
806
923
validateAbortSignal ( signal , 'options.signal' ) ;
0 commit comments