Skip to content

Commit

Permalink
RN: Adopt Mapped Types in EventEmitter
Browse files Browse the repository at this point in the history
Summary:
Migrates `EventEmitter` in React Native to use the newly introduced mapped types in Flow, instead of `$ObjMap`.

Changelog:
[Internal]

Reviewed By: jbrown215

Differential Revision: D47296240

fbshipit-source-id: 435d0a19242dcd13a4a6c7824daf0e894445cfa7
  • Loading branch information
yungsters authored and facebook-github-bot committed Jul 24, 2023
1 parent a7805bb commit 75f4588
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/react-native/Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ interface Registration<TArgs> {
+remove: () => void;
}

type Registry<TEventToArgsMap: {...}> = $ObjMap<
TEventToArgsMap,
<TArgs>(TArgs) => Set<Registration<TArgs>>,
>;
type Registry<TEventToArgsMap: {...}> = {
[key in keyof TEventToArgsMap]?: Set<Registration<TEventToArgsMap[key]>>,
};

/**
* EventEmitter manages listeners and publishes events to them.
Expand All @@ -63,7 +62,7 @@ type Registry<TEventToArgsMap: {...}> = $ObjMap<
export default class EventEmitter<TEventToArgsMap: {...}>
implements IEventEmitter<TEventToArgsMap>
{
_registry: Registry<TEventToArgsMap> = {};
_registry: Registry<TEventToArgsMap> = emptyRegistry<TEventToArgsMap>();

/**
* Registers a listener that is called when the supplied event is emitted.
Expand Down Expand Up @@ -122,7 +121,7 @@ export default class EventEmitter<TEventToArgsMap: {...}>
eventType?: ?TEvent,
): void {
if (eventType == null) {
this._registry = {};
this._registry = emptyRegistry<TEventToArgsMap>();
} else {
delete this._registry[eventType];
}
Expand All @@ -137,6 +136,13 @@ export default class EventEmitter<TEventToArgsMap: {...}>
}
}

function emptyRegistry<TEventToArgsMap: {...}>(): Registry<TEventToArgsMap> {
// 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<TEventToArgsMap>,
Expand Down

0 comments on commit 75f4588

Please sign in to comment.