mirror of
https://github.com/status-im/react-native.git
synced 2025-02-26 08:05:34 +00:00
RN: Improve NativeEventEmitter Flow Types
Reviewed By: fkgozali Differential Revision: D6050987 fbshipit-source-id: 1c911bed23f7d26800aafed4b7e7c30a1197f64b
This commit is contained in:
parent
4a2d3e9653
commit
2fff445b13
@ -14,18 +14,24 @@
|
||||
const EventEmitter = require('EventEmitter');
|
||||
const Platform = require('Platform');
|
||||
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||
|
||||
const invariant = require('fbjs/lib/invariant');
|
||||
|
||||
import type EmitterSubscription from 'EmitterSubscription';
|
||||
|
||||
type NativeModule = {
|
||||
+addListener: (eventType: string) => void,
|
||||
+removeListeners: (count: number) => void,
|
||||
};
|
||||
|
||||
/**
|
||||
* Abstract base class for implementing event-emitting modules. This implements
|
||||
* a subset of the standard EventEmitter node module API.
|
||||
*/
|
||||
class NativeEventEmitter extends EventEmitter {
|
||||
_nativeModule: Object;
|
||||
_nativeModule: ?NativeModule;
|
||||
|
||||
constructor(nativeModule: Object) {
|
||||
constructor(nativeModule: ?NativeModule) {
|
||||
super(RCTDeviceEventEmitter.sharedSubscriber);
|
||||
if (Platform.OS === 'ios') {
|
||||
invariant(nativeModule, 'Native module cannot be null.');
|
||||
@ -33,8 +39,12 @@ class NativeEventEmitter extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
addListener(eventType: string, listener: Function, context: ?Object): EmitterSubscription {
|
||||
if (Platform.OS === 'ios') {
|
||||
addListener(
|
||||
eventType: string,
|
||||
listener: Function,
|
||||
context: ?Object,
|
||||
): EmitterSubscription {
|
||||
if (this._nativeModule != null) {
|
||||
this._nativeModule.addListener(eventType);
|
||||
}
|
||||
return super.addListener(eventType, listener, context);
|
||||
@ -42,15 +52,15 @@ class NativeEventEmitter extends EventEmitter {
|
||||
|
||||
removeAllListeners(eventType: string) {
|
||||
invariant(eventType, 'eventType argument is required.');
|
||||
if (Platform.OS === 'ios') {
|
||||
const count = this.listeners(eventType).length;
|
||||
if (this._nativeModule != null) {
|
||||
this._nativeModule.removeListeners(count);
|
||||
}
|
||||
super.removeAllListeners(eventType);
|
||||
}
|
||||
|
||||
removeSubscription(subscription: EmitterSubscription) {
|
||||
if (Platform.OS === 'ios') {
|
||||
if (this._nativeModule != null) {
|
||||
this._nativeModule.removeListeners(1);
|
||||
}
|
||||
super.removeSubscription(subscription);
|
||||
|
Loading…
x
Reference in New Issue
Block a user