Skip to content

Commit

Permalink
RN: Simplify RCTDeviceEventEmitter
Browse files Browse the repository at this point in the history
Summary:
Simplifies `RCTDeviceEventEmitter` to simply be an `EventEmitter`.

The only thing special about it is that all native events are emitted on it and that `NativeEventEmitter` composes it.

Changelog:
[General][Removed] - Removed `RCTDeviceEventEmitter.sharedSubscribers`.

Reviewed By: RSNara

Differential Revision: D26163660

fbshipit-source-id: aedff8323d86947220fc293a74a19a3981fd875a
  • Loading branch information
yungsters authored and facebook-github-bot committed Feb 17, 2021
1 parent 34d3efe commit 3af0c84
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
40 changes: 9 additions & 31 deletions Libraries/EventEmitter/RCTDeviceEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,19 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
* @flow
*/

import EventEmitter from '../vendor/emitter/EventEmitter';
import type EmitterSubscription from '../vendor/emitter/_EmitterSubscription';
import EventSubscriptionVendor from '../vendor/emitter/_EventSubscriptionVendor';

/**
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
* adding all event listeners directly to RCTDeviceEventEmitter.
*/
class RCTDeviceEventEmitter<
EventDefinitions: {...},
> extends EventEmitter<EventDefinitions> {
sharedSubscriber: EventSubscriptionVendor<EventDefinitions>;

constructor() {
const sharedSubscriber = new EventSubscriptionVendor<EventDefinitions>();
super(sharedSubscriber);
this.sharedSubscriber = sharedSubscriber;
}

removeSubscription<K: $Keys<EventDefinitions>>(
subscription: EmitterSubscription<EventDefinitions, K>,
): void {
if (subscription.emitter !== this) {
subscription.emitter.removeSubscription(subscription);
} else {
super.removeSubscription(subscription);
}
}
}
import EventEmitter, {type IEventEmitter} from '../vendor/emitter/EventEmitter';

// FIXME: use typed events
type RCTDeviceEventDefinitions = $FlowFixMe;

export default (new RCTDeviceEventEmitter(): RCTDeviceEventEmitter<RCTDeviceEventDefinitions>);
/**
* Global EventEmitter used by the native platform to emit events to JavaScript.
* Events are identified by globally unique event names.
*
* NativeModules that emit events should instead subclass `NativeEventEmitter`.
*/
export default (new EventEmitter(): IEventEmitter<RCTDeviceEventDefinitions>);
39 changes: 31 additions & 8 deletions Libraries/EventEmitter/__mocks__/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,42 @@
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @flow strict-local
*/

import EventEmitter from '../../vendor/emitter/EventEmitter';
import {
type EventSubscription,
type IEventEmitter,
} from '../../vendor/emitter/EventEmitter';
import RCTDeviceEventEmitter from '../RCTDeviceEventEmitter';

/**
* Mock the NativeEventEmitter as a normal JS EventEmitter.
* Mock `NativeEventEmitter` to ignore Native Modules.
*/
export default class NativeEventEmitter<
EventDefinitions: {...},
> extends EventEmitter<EventDefinitions> {
constructor() {
super(RCTDeviceEventEmitter.sharedSubscriber);
export default class NativeEventEmitter<TEventToArgsMap: {...}>
implements IEventEmitter<TEventToArgsMap> {
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
context?: mixed,
): EventSubscription {
return RCTDeviceEventEmitter.addListener(eventType, listener, context);
}

emit<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
...args: $ElementType<TEventToArgsMap, TEvent>
): void {
RCTDeviceEventEmitter.emit(eventType, ...args);
}

removeAllListeners<TEvent: $Keys<TEventToArgsMap>>(
eventType?: ?TEvent,
): void {
RCTDeviceEventEmitter.removeAllListeners(eventType);
}

listenerCount<TEvent: $Keys<TEventToArgsMap>>(eventType: TEvent): number {
return RCTDeviceEventEmitter.listenerCount(eventType);
}
}
2 changes: 1 addition & 1 deletion Libraries/vendor/emitter/EventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface IEventEmitter<TEventToArgsMap: {...}> {
*/
addListener<TEvent: $Keys<TEventToArgsMap>>(
eventType: TEvent,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => void,
listener: (...args: $ElementType<TEventToArgsMap, TEvent>) => mixed,
context?: mixed,
): EventSubscription;

Expand Down

0 comments on commit 3af0c84

Please sign in to comment.