mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
RN: Simplify RCTDeviceEventEmitter
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
34d3efe2ef
commit
3af0c84aa5
@@ -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>);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user