Skip to content

Commit

Permalink
EventEmitter: Replace listeners() with listenerCount()
Browse files Browse the repository at this point in the history
Summary:
Replaces the `listeners()` method on `EventEmitter` with a `listenerCount()` method.

Changelog:
[General][Removed] - Removed `listeners()` from `DeviceEventEmitter` and `NativeEventEmitter`.
[General][Added] - Added `listenerCount()` to `DeviceEventEmitter` and `NativeEventEmitter`.

Reviewed By: cpojer

Differential Revision: D22204156

fbshipit-source-id: 15029525aeef55de9934a4f319910e666ecbe1d8
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 3, 2020
1 parent b265561 commit b11d6ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Libraries/EventEmitter/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class NativeEventEmitter extends EventEmitter {

removeAllListeners(eventType: string) {
invariant(eventType, 'eventType argument is required.');
const count = this.listeners(eventType).length;
const count = this.listenerCount(eventType);
if (this._nativeModule != null) {
this._nativeModule.removeListeners(count);
}
Expand Down
20 changes: 9 additions & 11 deletions Libraries/vendor/emitter/_EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,21 @@ class EventEmitter {
}

/**
* Returns an array of listeners that are currently registered for the given
* Returns the number of listeners that are currently registered for the given
* event.
*
* @param {string} eventType - Name of the event to query
* @returns {array}
* @returns {number}
*/
listeners(eventType: string): [EmitterSubscription] {
listenerCount(eventType: string): number {
const subscriptions = this._subscriber.getSubscriptionsForType(eventType);
return subscriptions
? subscriptions
// We filter out missing entries because the array is sparse.
// "callbackfn is called only for elements of the array which actually
// exist; it is not called for missing elements of the array."
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.filter
.filter(sparseFilterPredicate)
.map(subscription => subscription.listener)
: [];
? // We filter out missing entries because the array is sparse.
// "callbackfn is called only for elements of the array which actually
// exist; it is not called for missing elements of the array."
// https://www.ecma-international.org/ecma-262/9.0/index.html#sec-array.prototype.filter
subscriptions.filter(sparseFilterPredicate).length
: 0;
}

/**
Expand Down

0 comments on commit b11d6ec

Please sign in to comment.