mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
EventEmitter: Replace listeners() with listenerCount()
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b26556125f
commit
b11d6ecbb8
@@ -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);
|
||||
}
|
||||
|
||||
+9
-11
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user