Skip to content

Commit

Permalink
RN: Use a Private Property in EventEmitter (facebook#39395)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#39395

Switches `EventEmitter` to use a private property.

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

Changelog:
[Internal]

Differential Revision: D49167908

fbshipit-source-id: f2e9f9d6e3b73639b92ad6fe9e652aaad20dd3df
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 12, 2023
1 parent 2a48c95 commit 89b39f1
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 89b39f1

Please sign in to comment.