From 2538442c8f1c65c62f8e31023b88e254d3ab48f9 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Mon, 24 Jul 2023 14:29:11 -0700 Subject: [PATCH] Back out "RN: Adopt Mapped Types in EventEmitter" (#38599) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38599 Currently, `metro-config` does not have `hermes-parser` enabled by default. This is because `hermes-parser` does not yet support parsing all of TypeScript's syntax nodes. However, this also means we cannot yet utilize any Flow langauge features that require the use of `hermes-parser` (and are unsupported by Babel). Mapped types falls into this category, so we cannot use them. This backs out a recent change that tried to adopt Flow mapped types in `EventEmitter`, for now. We can revisit after making the necessary changes to `metro-config`. Changelog: [Internal] Reviewed By: pieterv Differential Revision: D47729818 fbshipit-source-id: f562d1456490e89f84c40b8f638ffe5eaf94dbc2 --- .../Libraries/vendor/emitter/EventEmitter.js | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/packages/react-native/Libraries/vendor/emitter/EventEmitter.js b/packages/react-native/Libraries/vendor/emitter/EventEmitter.js index 2a9c9aab0b2373..904b77a3226756 100644 --- a/packages/react-native/Libraries/vendor/emitter/EventEmitter.js +++ b/packages/react-native/Libraries/vendor/emitter/EventEmitter.js @@ -35,9 +35,10 @@ interface Registration { +remove: () => void; } -type Registry = { - [key in keyof TEventToArgsMap]?: Set>, -}; +type Registry = $ObjMap< + TEventToArgsMap, + (TArgs) => Set>, +>; /** * EventEmitter manages listeners and publishes events to them. @@ -62,7 +63,7 @@ type Registry = { export default class EventEmitter implements IEventEmitter { - _registry: Registry = emptyRegistry(); + _registry: Registry = {}; /** * Registers a listener that is called when the supplied event is emitted. @@ -121,7 +122,7 @@ export default class EventEmitter eventType?: ?TEvent, ): void { if (eventType == null) { - this._registry = emptyRegistry(); + this._registry = {}; } else { delete this._registry[eventType]; } @@ -136,13 +137,6 @@ export default class EventEmitter } } -function emptyRegistry(): Registry { - // Flow cannot enforce that `TEventToArgsMap` is an object because, for - // example, Flow permits `empty. We have to ignore this error for now. - // $FlowIgnore[incompatible-return] - return {}; -} - function allocate< TEventToArgsMap: {...}, TEvent: $Keys,