-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export NativeEventEmitter and provide React Native's implementation.
- Loading branch information
Showing
15 changed files
with
1,057 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/react-native-web/src/exports/NativeEventEmitter/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/react-native-web/src/modules/NativeEventEmitter/index.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/react-native-web/src/vendor/react-native/NativeEventEmitter/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
59 changes: 59 additions & 0 deletions
59
packages/react-native-web/src/vendor/react-native/emitter/EmitterSubscription.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.