mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-06 22:37:14 +08:00
Fix sparse array handling in EventEmitter#listeners() (#24546)
Summary:
Fixes a regression in 1f8b46a2fc. The internal subscription vendor uses a sparse array to track listeners, which makes listener removal fast. When querying listeners, the sparse entries need to be removed. `Array#filter` is a built-in way to do this -> linked to the JS spec, which explains this.
[General] [Fixed] - Fixed sparse array handling in `EventEmitter#listeners()`
Pull Request resolved: https://github.com/facebook/react-native/pull/24546
Differential Revision: D15044790
Pulled By: cpojer
fbshipit-source-id: 0f1301618739357b4a0f5378b9584efe74f0f09a
This commit is contained in:
committed by
Facebook Github Bot
parent
1f6de88230
commit
b76acd3f42
10
Libraries/vendor/emitter/EventEmitter.js
vendored
10
Libraries/vendor/emitter/EventEmitter.js
vendored
@@ -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)
|
||||
: [];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user