Skip to content

Commit

Permalink
[add] NativeEventEmitter export
Browse files Browse the repository at this point in the history
Export NativeEventEmitter and provide React Native's implementation.
  • Loading branch information
necolas committed Jun 3, 2018
1 parent f254c8e commit ea744fe
Show file tree
Hide file tree
Showing 15 changed files with 1,057 additions and 24 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
],
"lint-staged": {
"packages/react-native-web/src/index.js": [
"node ./scripts/babel/createModuleMap.js"
"node ./scripts/babel/createModuleMap.js",
"prettier --write ./packages/babel-plugin-react-native-web/src/moduleMap.js",
"git add ./packages/babel-plugin-react-native-web/src/moduleMap.js"
],
"**/*.js": [
"prettier --write",
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin-react-native-web/src/moduleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
Linking: true,
ListView: true,
Modal: true,
NativeEventEmitter: true,
NativeModules: true,
NetInfo: true,
PanResponder: true,
Expand Down
11 changes: 11 additions & 0 deletions packages/react-native-web/src/exports/NativeEventEmitter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2016-present, Nicolas Gallagher.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import NativeEventEmitter from '../../vendor/react-native/NativeEventEmitter';
export default NativeEventEmitter;
3 changes: 3 additions & 0 deletions packages/react-native-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Keyboard from './exports/Keyboard';
import InteractionManager from './exports/InteractionManager';
import LayoutAnimation from './exports/LayoutAnimation';
import Linking from './exports/Linking';
import NativeEventEmitter from './exports/NativeEventEmitter';
import NetInfo from './exports/NetInfo';
import PanResponder from './exports/PanResponder';
import PixelRatio from './exports/PixelRatio';
Expand Down Expand Up @@ -93,6 +94,7 @@ export {
Keyboard,
LayoutAnimation,
Linking,
NativeEventEmitter,
NetInfo,
PanResponder,
PixelRatio,
Expand Down Expand Up @@ -162,6 +164,7 @@ const ReactNative = {
Keyboard,
LayoutAnimation,
Linking,
NativeEventEmitter,
NetInfo,
PanResponder,
PixelRatio,
Expand Down
22 changes: 0 additions & 22 deletions packages/react-native-web/src/modules/NativeEventEmitter/index.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import invariant from 'fbjs/lib/invariant';
import NativeModules from '../../../exports/NativeModules';
import NativeEventEmitter from '../../../modules/NativeEventEmitter';
import NativeEventEmitter from '../NativeEventEmitter';

import type {AnimationConfig} from './animations/Animation';
import type {EventConfig} from './AnimatedEvent';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule NativeEventEmitter
* @flow
*/
'use strict';

import EventEmitter from '../emitter/EventEmitter';
import Platform from '../../../exports/Platform';

import invariant from 'fbjs/lib/invariant';

import type EmitterSubscription from '../emitter/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: ?NativeModule;

constructor(nativeModule: ?NativeModule) {
super();
if (Platform.OS === 'ios') {
invariant(nativeModule, 'Native module cannot be null.');
this._nativeModule = nativeModule;
}
}

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.');
const count = this.listeners(eventType).length;
if (this._nativeModule != null) {
this._nativeModule.removeListeners(count);
}
super.removeAllListeners(eventType);
}

removeSubscription(subscription: EmitterSubscription) {
if (this._nativeModule != null) {
this._nativeModule.removeListeners(1);
}
super.removeSubscription(subscription);
}
}

export default NativeEventEmitter;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule EmitterSubscription
* @flow
*/
'use strict';

import EventSubscription from './EventSubscription';

import type EventEmitter from './EventEmitter';
import type EventSubscriptionVendor from './EventSubscriptionVendor';

/**
* EmitterSubscription represents a subscription with listener and context data.
*/
class EmitterSubscription extends EventSubscription {

emitter: EventEmitter;
listener: Function;
context: ?Object;

/**
* @param {EventEmitter} emitter - The event emitter that registered this
* subscription
* @param {EventSubscriptionVendor} subscriber - The subscriber that controls
* this subscription
* @param {function} listener - Function to invoke when the specified event is
* emitted
* @param {*} context - Optional context object to use when invoking the
* listener
*/
constructor(
emitter: EventEmitter,
subscriber: EventSubscriptionVendor,
listener: Function,
context: ?Object
) {
super(subscriber);
this.emitter = emitter;
this.listener = listener;
this.context = context;
}

/**
* Removes this subscription from the emitter that registered it.
* Note: we're overriding the `remove()` method of EventSubscription here
* but deliberately not calling `super.remove()` as the responsibility
* for removing the subscription lies with the EventEmitter.
*/
remove() {
this.emitter.removeSubscription(this);
}
}

export default EmitterSubscription;
Loading

0 comments on commit ea744fe

Please sign in to comment.