Skip to content

Commit

Permalink
RN: Adopt Indexed Access Types in EventEmitter (facebook#38243)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#38243

Adopts [Indexed Access Types](https://flow.org/en/docs/types/indexed-access/) in `EventEmitter`.

Changelog:
[Internal]

Reviewed By: gkz, SamChou19815

Differential Revision: D47298095

fbshipit-source-id: d31eae8a929a13caf8d876aed4a035982f52ab38
  • Loading branch information
yungsters authored and facebook-github-bot committed Jul 7, 2023
1 parent 1706066 commit ca6134f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/react-native/Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ export interface EventSubscription {
export interface IEventEmitter<TEventToArgsMap: {...}> {
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
listener: (...args: TEventToArgsMap[TEvent]) => mixed,
context?: mixed,
): EventSubscription;

emit<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
...args: $ElementType<TEventToArgsMap, TEvent>
...args: TEventToArgsMap[TEvent]
): void;

removeAllListeners<TEvent: $Keys<TEventToArgsMap>>(eventType?: ?TEvent): void;
Expand Down Expand Up @@ -71,19 +71,19 @@ export default class EventEmitter<TEventToArgsMap: {...}>
*/
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
listener: (...args: TEventToArgsMap[TEvent]) => mixed,
context: mixed,
): EventSubscription {
if (typeof listener !== 'function') {
throw new TypeError(
'EventEmitter.addListener(...): 2nd argument must be a function.',
);
}
const registrations = allocate<_, _, TEventToArgsMap[TEvent]>(
const registrations = allocate<TEventToArgsMap, TEvent, TEventToArgsMap[TEvent]>(
this._registry,
eventType,
);
const registration: Registration<$ElementType<TEventToArgsMap, TEvent>> = {
const registration: Registration<TEventToArgsMap[TEvent]> = {
context,
listener,
remove(): void {
Expand All @@ -103,10 +103,10 @@ export default class EventEmitter<TEventToArgsMap: {...}>
*/
emit<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
...args: $ElementType<TEventToArgsMap, TEvent>
...args: TEventToArgsMap[TEvent]
): void {
const registrations: ?Set<
Registration<$ElementType<TEventToArgsMap, TEvent>>,
Registration<TEventToArgsMap[TEvent]>,
> = this._registry[eventType];
if (registrations != null) {
for (const registration of [...registrations]) {
Expand Down Expand Up @@ -140,7 +140,7 @@ export default class EventEmitter<TEventToArgsMap: {...}>
function allocate<
TEventToArgsMap: {...},
TEvent: $Keys<TEventToArgsMap>,
TEventArgs: $ElementType<TEventToArgsMap, TEvent>,
TEventArgs: TEventToArgsMap[TEvent],
>(
registry: Registry<TEventToArgsMap>,
eventType: TEvent,
Expand Down

0 comments on commit ca6134f

Please sign in to comment.