Skip to content

Commit

Permalink
RN: Improve NativeEventEmitter Flow Types
Browse files Browse the repository at this point in the history
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 2fff445
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Libraries/EventEmitter/NativeEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2fff445

Please sign in to comment.