diff --git a/Libraries/vendor/emitter/EventEmitter.js b/Libraries/vendor/emitter/EventEmitter.js index 28cf41afc889c3..bd9a7cc83da11a 100644 --- a/Libraries/vendor/emitter/EventEmitter.js +++ b/Libraries/vendor/emitter/EventEmitter.js @@ -16,6 +16,8 @@ const EventSubscriptionVendor = require('EventSubscriptionVendor'); const invariant = require('invariant'); +const sparseFilterPredicate = () => true; + /** * @class EventEmitter * @description @@ -151,7 +153,13 @@ class EventEmitter { listeners(eventType: string): [EmitterSubscription] { const subscriptions = this._subscriber.getSubscriptionsForType(eventType); return subscriptions - ? subscriptions.map(subscription => subscription.listener) + ? 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) : []; }