Permalink
Browse files

RN: Improve NativeEventEmitter Flow Types

Reviewed By: fkgozali

Differential Revision: D6050987

fbshipit-source-id: 1c911bed23f7d26800aafed4b7e7c30a1197f64b
  • Loading branch information...
yungsters authored and facebook-github-bot committed Oct 13, 2017
1 parent 4a2d3e9 commit 2fff445b13da114cc5238ad9064ed2100030d5b9
Showing with 17 additions and 7 deletions.
  1. +17 −7 Libraries/EventEmitter/NativeEventEmitter.js
@@ -14,43 +14,53 @@
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.');
this._nativeModule = nativeModule;
}
}
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);
}
removeAllListeners(eventType: string) {
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);
}
super.removeAllListeners(eventType);
}
removeSubscription(subscription: EmitterSubscription) {
if (Platform.OS === 'ios') {
if (this._nativeModule != null) {
this._nativeModule.removeListeners(1);
}
super.removeSubscription(subscription);

0 comments on commit 2fff445

Please sign in to comment.