@@ -79,6 +79,11 @@ const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners');
79
79
const kMaxEventTargetListenersWarned =
80
80
Symbol ( 'events.maxEventTargetListenersWarned' ) ;
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
}
@@ -156,6 +161,12 @@ ObjectDefineProperties(EventEmitter, {
156
161
}
157
162
} ) ;
158
163
164
+ /**
165
+ * Sets the max listeners.
166
+ * @param {number } n
167
+ * @param {EventTarget[] | EventEmitter[] } [eventTargets]
168
+ * @returns {void }
169
+ */
159
170
EventEmitter . setMaxListeners =
160
171
function ( n = defaultMaxListeners , ...eventTargets ) {
161
172
if ( typeof n !== 'number' || n < 0 || NumberIsNaN ( n ) )
@@ -247,8 +258,11 @@ function emitUnhandledRejectionOrErr(ee, err, type, args) {
247
258
}
248
259
}
249
260
250
- // Obviously not all Emitters should be limited to 10. This function allows
251
- // that to be increased. Set to zero for unlimited.
261
+ /**
262
+ * Increases the max listeners of the event emitter.
263
+ * @param {number } n
264
+ * @returns {EventEmitter }
265
+ */
252
266
EventEmitter . prototype . setMaxListeners = function setMaxListeners ( n ) {
253
267
if ( typeof n !== 'number' || n < 0 || NumberIsNaN ( n ) ) {
254
268
throw new ERR_OUT_OF_RANGE ( 'n' , 'a non-negative number' , n ) ;
@@ -263,6 +277,10 @@ function _getMaxListeners(that) {
263
277
return that . _maxListeners ;
264
278
}
265
279
280
+ /**
281
+ * Returns the current max listener value for the event emitter.
282
+ * @returns {number }
283
+ */
266
284
EventEmitter . prototype . getMaxListeners = function getMaxListeners ( ) {
267
285
return _getMaxListeners ( this ) ;
268
286
} ;
@@ -315,6 +333,13 @@ function enhanceStackTrace(err, own) {
315
333
return err . stack + sep + ArrayPrototypeJoin ( ownStack , '\n' ) ;
316
334
}
317
335
336
+ /**
337
+ * Synchronously calls each of the listeners registered
338
+ * for the event.
339
+ * @param {string | symbol } type
340
+ * @param {...any } [args]
341
+ * @returns {boolean }
342
+ */
318
343
EventEmitter . prototype . emit = function emit ( type , ...args ) {
319
344
let doError = ( type === 'error' ) ;
320
345
@@ -456,12 +481,25 @@ function _addListener(target, type, listener, prepend) {
456
481
return target ;
457
482
}
458
483
484
+ /**
485
+ * Adds a listener to the event emitter.
486
+ * @param {string | symbol } type
487
+ * @param {Function } listener
488
+ * @returns {EventEmitter }
489
+ */
459
490
EventEmitter . prototype . addListener = function addListener ( type , listener ) {
460
491
return _addListener ( this , type , listener , false ) ;
461
492
} ;
462
493
463
494
EventEmitter . prototype . on = EventEmitter . prototype . addListener ;
464
495
496
+ /**
497
+ * Adds the `listener` function to the beginning of
498
+ * the listeners array.
499
+ * @param {string | symbol } type
500
+ * @param {Function } listener
501
+ * @returns {EventEmitter }
502
+ */
465
503
EventEmitter . prototype . prependListener =
466
504
function prependListener ( type , listener ) {
467
505
return _addListener ( this , type , listener , true ) ;
@@ -485,13 +523,26 @@ function _onceWrap(target, type, listener) {
485
523
return wrapped ;
486
524
}
487
525
526
+ /**
527
+ * Adds a one-time `listener` function to the event emitter.
528
+ * @param {string | symbol } type
529
+ * @param {Function } listener
530
+ * @returns {EventEmitter }
531
+ */
488
532
EventEmitter . prototype . once = function once ( type , listener ) {
489
533
checkListener ( listener ) ;
490
534
491
535
this . on ( type , _onceWrap ( this , type , listener ) ) ;
492
536
return this ;
493
537
} ;
494
538
539
+ /**
540
+ * Adds a one-time `listener` function to the beginning of
541
+ * the listeners array.
542
+ * @param {string | symbol } type
543
+ * @param {Function } listener
544
+ * @returns {EventEmitter }
545
+ */
495
546
EventEmitter . prototype . prependOnceListener =
496
547
function prependOnceListener ( type , listener ) {
497
548
checkListener ( listener ) ;
@@ -500,7 +551,12 @@ EventEmitter.prototype.prependOnceListener =
500
551
return this ;
501
552
} ;
502
553
503
- // Emits a 'removeListener' event if and only if the listener was removed.
554
+ /**
555
+ * Removes the specified `listener` from the listeners array.
556
+ * @param {string | symbol } type
557
+ * @param {Function } listener
558
+ * @returns {EventEmitter }
559
+ */
504
560
EventEmitter . prototype . removeListener =
505
561
function removeListener ( type , listener ) {
506
562
checkListener ( listener ) ;
@@ -554,6 +610,13 @@ EventEmitter.prototype.removeListener =
554
610
555
611
EventEmitter . prototype . off = EventEmitter . prototype . removeListener ;
556
612
613
+ /**
614
+ * Removes all listeners from the event emitter. (Only
615
+ * removes listeners for a specific event name if specified
616
+ * as `type`).
617
+ * @param {string | symbol } [type]
618
+ * @returns {EventEmitter }
619
+ */
557
620
EventEmitter . prototype . removeAllListeners =
558
621
function removeAllListeners ( type ) {
559
622
const events = this . _events ;
@@ -617,14 +680,34 @@ function _listeners(target, type, unwrap) {
617
680
unwrapListeners ( evlistener ) : arrayClone ( evlistener ) ;
618
681
}
619
682
683
+ /**
684
+ * Returns a copy of the array of listeners for the event name
685
+ * specified as `type`.
686
+ * @param {string | symbol } type
687
+ * @returns {Function[] }
688
+ */
620
689
EventEmitter . prototype . listeners = function listeners ( type ) {
621
690
return _listeners ( this , type , true ) ;
622
691
} ;
623
692
693
+ /**
694
+ * Returns a copy of the array of listeners and wrappers for
695
+ * the event name specified as `type`.
696
+ * @param {string | symbol } type
697
+ * @returns {Function[] }
698
+ */
624
699
EventEmitter . prototype . rawListeners = function rawListeners ( type ) {
625
700
return _listeners ( this , type , false ) ;
626
701
} ;
627
702
703
+ /**
704
+ * Returns the number of listeners listening to the event name
705
+ * specified as `type`.
706
+ * @deprecated since v3.2.0
707
+ * @param {EventEmitter } emitter
708
+ * @param {string | symbol } type
709
+ * @returns {number }
710
+ */
628
711
EventEmitter . listenerCount = function ( emitter , type ) {
629
712
if ( typeof emitter . listenerCount === 'function' ) {
630
713
return emitter . listenerCount ( type ) ;
@@ -633,6 +716,13 @@ EventEmitter.listenerCount = function(emitter, type) {
633
716
} ;
634
717
635
718
EventEmitter . prototype . listenerCount = listenerCount ;
719
+
720
+ /**
721
+ * Returns the number of listeners listening to event name
722
+ * specified as `type`.
723
+ * @param {string | symbol } type
724
+ * @returns {number }
725
+ */
636
726
function listenerCount ( type ) {
637
727
const events = this . _events ;
638
728
@@ -649,6 +739,11 @@ function listenerCount(type) {
649
739
return 0 ;
650
740
}
651
741
742
+ /**
743
+ * Returns an array listing the events for which
744
+ * the emitter has registered listeners.
745
+ * @returns {any[] }
746
+ */
652
747
EventEmitter . prototype . eventNames = function eventNames ( ) {
653
748
return this . _eventsCount > 0 ? ReflectOwnKeys ( this . _events ) : [ ] ;
654
749
} ;
@@ -676,6 +771,13 @@ function unwrapListeners(arr) {
676
771
return ret ;
677
772
}
678
773
774
+ /**
775
+ * Returns a copy of the array of listeners for the event name
776
+ * specified as `type`.
777
+ * @param {EventEmitter | EventTarget } emitterOrTarget
778
+ * @param {string | symbol } type
779
+ * @returns {Function[] }
780
+ */
679
781
function getEventListeners ( emitterOrTarget , type ) {
680
782
// First check if EventEmitter
681
783
if ( typeof emitterOrTarget . listeners === 'function' ) {
@@ -700,6 +802,14 @@ function getEventListeners(emitterOrTarget, type) {
700
802
emitterOrTarget ) ;
701
803
}
702
804
805
+ /**
806
+ * Creates a `Promise` that is fulfilled when the emitter
807
+ * emits the given event.
808
+ * @param {EventEmitter } emitter
809
+ * @param {string } name
810
+ * @param {{ signal: AbortSignal; } } [options]
811
+ * @returns {Promise }
812
+ */
703
813
async function once ( emitter , name , options = { } ) {
704
814
const signal = options ?. signal ;
705
815
validateAbortSignal ( signal , 'options.signal' ) ;
@@ -771,6 +881,13 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
771
881
}
772
882
}
773
883
884
+ /**
885
+ * Returns an `AsyncIterator` that iterates `event` events.
886
+ * @param {EventEmitter } emitter
887
+ * @param {string | symbol } event
888
+ * @param {{ signal: AbortSignal; } } [options]
889
+ * @returns {AsyncIterator }
890
+ */
774
891
function on ( emitter , event , options ) {
775
892
const signal = options ?. signal ;
776
893
validateAbortSignal ( signal , 'options.signal' ) ;
0 commit comments