Skip to content

Commit

Permalink
RN: Use a Private Property in EventEmitter
Browse files Browse the repository at this point in the history
Summary:
Switches `EventEmitter` to use a private property.

Support for private properties was [only recently added](#39318), so this will be the first end-to-end validation of support in the `facebook/react-native` project.

Changelog:
[Internal]

Differential Revision: D49167908
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 11, 2023
1 parent 2a48c95 commit ce5ac71
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/react-native/Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Registry<TEventToArgsMap: {...}> = $ObjMap<
export default class EventEmitter<TEventToArgsMap: {...}>
implements IEventEmitter<TEventToArgsMap>
{
_registry: Registry<TEventToArgsMap> = {};
#registry: Registry<TEventToArgsMap> = {};

/**
* Registers a listener that is called when the supplied event is emitted.
Expand All @@ -83,7 +83,7 @@ export default class EventEmitter<TEventToArgsMap: {...}>
TEventToArgsMap,
TEvent,
TEventToArgsMap[TEvent],
>(this._registry, eventType);
>(this.#registry, eventType);
const registration: Registration<TEventToArgsMap[TEvent]> = {
context,
listener,
Expand All @@ -107,7 +107,7 @@ export default class EventEmitter<TEventToArgsMap: {...}>
...args: TEventToArgsMap[TEvent]
): void {
const registrations: ?Set<Registration<TEventToArgsMap[TEvent]>> =
this._registry[eventType];
this.#registry[eventType];
if (registrations != null) {
for (const registration of [...registrations]) {
registration.listener.apply(registration.context, args);
Expand All @@ -122,17 +122,17 @@ export default class EventEmitter<TEventToArgsMap: {...}>
eventType?: ?TEvent,
): void {
if (eventType == null) {
this._registry = {};
this.#registry = {};
} else {
delete this._registry[eventType];
delete this.#registry[eventType];
}
}

/**
* Returns the number of registered listeners for the supplied event.
*/
listenerCount<TEvent: $Keys<TEventToArgsMap>>(eventType: TEvent): number {
const registrations: ?Set<Registration<mixed>> = this._registry[eventType];
const registrations: ?Set<Registration<mixed>> = this.#registry[eventType];
return registrations == null ? 0 : registrations.size;
}
}
Expand Down

0 comments on commit ce5ac71

Please sign in to comment.