mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
RN: Adopt Mapped Types in EventEmitter
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
a7805bb20e
commit
75f458891f
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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];
|
||||
}
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user