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 EventEmitter = require('EventEmitter');
|
||||||
const Platform = require('Platform');
|
const Platform = require('Platform');
|
||||||
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
||||||
|
|
||||||
const invariant = require('fbjs/lib/invariant');
|
const invariant = require('fbjs/lib/invariant');
|
||||||
|
|
||||||
import type EmitterSubscription from 'EmitterSubscription';
|
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
|
* Abstract base class for implementing event-emitting modules. This implements
|
||||||
* a subset of the standard EventEmitter node module API.
|
* a subset of the standard EventEmitter node module API.
|
||||||
*/
|
*/
|
||||||
class NativeEventEmitter extends EventEmitter {
|
class NativeEventEmitter extends EventEmitter {
|
||||||
_nativeModule: Object;
|
_nativeModule: ?NativeModule;
|
||||||
|
|
||||||
constructor(nativeModule: Object) {
|
constructor(nativeModule: ?NativeModule) {
|
||||||
super(RCTDeviceEventEmitter.sharedSubscriber);
|
super(RCTDeviceEventEmitter.sharedSubscriber);
|
||||||
if (Platform.OS === 'ios') {
|
if (Platform.OS === 'ios') {
|
||||||
invariant(nativeModule, 'Native module cannot be null.');
|
invariant(nativeModule, 'Native module cannot be null.');
|
||||||
@ -33,8 +39,12 @@ class NativeEventEmitter extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addListener(eventType: string, listener: Function, context: ?Object): EmitterSubscription {
|
addListener(
|
||||||
if (Platform.OS === 'ios') {
|
eventType: string,
|
||||||
|
listener: Function,
|
||||||
|
context: ?Object,
|
||||||
|
): EmitterSubscription {
|
||||||
|
if (this._nativeModule != null) {
|
||||||
this._nativeModule.addListener(eventType);
|
this._nativeModule.addListener(eventType);
|
||||||
}
|
}
|
||||||
return super.addListener(eventType, listener, context);
|
return super.addListener(eventType, listener, context);
|
||||||
@ -42,15 +52,15 @@ class NativeEventEmitter extends EventEmitter {
|
|||||||
|
|
||||||
removeAllListeners(eventType: string) {
|
removeAllListeners(eventType: string) {
|
||||||
invariant(eventType, 'eventType argument is required.');
|
invariant(eventType, 'eventType argument is required.');
|
||||||
if (Platform.OS === 'ios') {
|
const count = this.listeners(eventType).length;
|
||||||
const count = this.listeners(eventType).length;
|
if (this._nativeModule != null) {
|
||||||
this._nativeModule.removeListeners(count);
|
this._nativeModule.removeListeners(count);
|
||||||
}
|
}
|
||||||
super.removeAllListeners(eventType);
|
super.removeAllListeners(eventType);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeSubscription(subscription: EmitterSubscription) {
|
removeSubscription(subscription: EmitterSubscription) {
|
||||||
if (Platform.OS === 'ios') {
|
if (this._nativeModule != null) {
|
||||||
this._nativeModule.removeListeners(1);
|
this._nativeModule.removeListeners(1);
|
||||||
}
|
}
|
||||||
super.removeSubscription(subscription);
|
super.removeSubscription(subscription);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user