Skip to content

Commit eafd035

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Switch EventEmitter to Array.from(...) (facebook#39525)
Summary: Switches `EventEmitter#emit` to use `Array.from` instead of the spread operator. This should be functionally identical (with marginally less overhead of the runtime having to determine the type of `registrations`), but there seems to be [some unexpected Babel configurations in the community](facebook#35577 (comment)) that causes this line of code to do the wrong things. Although we should independently root cause the Babel plugin configuration problems, this change might provide immediate relief and is also not any worse (e.g. in terms of code readability). This also adds a descriptive comment explaining the intention of the call to `Array.from`. Changelog: [Fixed][General] - Fix a potential bug in `EventEmitter` when used with certain Babel configurations that incorrectly polyfill the spread operator for iterables. Reviewed By: javache Differential Revision: D49389813
1 parent 06d40f4 commit eafd035

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

packages/react-native/Libraries/vendor/emitter/EventEmitter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export default class EventEmitter<TEventToArgsMap: {...}>
109109
const registrations: ?Set<Registration<TEventToArgsMap[TEvent]>> =
110110
this.#registry[eventType];
111111
if (registrations != null) {
112-
for (const registration of [...registrations]) {
112+
// Copy `registrations` to take a snapshot when we invoke `emit`, in case
113+
// registrations are added or removed when listeners are invoked.
114+
for (const registration of Array.from(registrations)) {
113115
registration.listener.apply(registration.context, args);
114116
}
115117
}

0 commit comments

Comments
 (0)