Skip to content

Commit 9ce2b5b

Browse files
EvanBaconnecolas
authored andcommitted
[fix] NativeEventEmitter implementation
Close #1275
1 parent cf7b020 commit 9ce2b5b

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow
9+
*/
10+
11+
'use strict';
12+
13+
import EventEmitter from '../emitter/EventEmitter';
14+
import EventSubscriptionVendor from '../emitter/EventSubscriptionVendor';
15+
16+
import type EmitterSubscription from '../emitter/EmitterSubscription';
17+
18+
const __DEV__ = process.env.NODE_ENV !== 'production';
19+
20+
function checkNativeEventModule(eventType: ?string) {
21+
if (eventType) {
22+
if (eventType === 'appStateDidChange' || eventType === 'memoryWarning') {
23+
throw new Error(
24+
'`' +
25+
eventType +
26+
'` event should be registered via the AppState module',
27+
);
28+
}
29+
}
30+
}
31+
32+
/**
33+
* Deprecated - subclass NativeEventEmitter to create granular event modules instead of
34+
* adding all event listeners directly to RCTDeviceEventEmitter.
35+
*/
36+
class RCTDeviceEventEmitter extends EventEmitter {
37+
sharedSubscriber: EventSubscriptionVendor;
38+
39+
constructor() {
40+
const sharedSubscriber = new EventSubscriptionVendor();
41+
super(sharedSubscriber);
42+
this.sharedSubscriber = sharedSubscriber;
43+
}
44+
45+
addListener(
46+
eventType: string,
47+
listener: Function,
48+
context: ?Object,
49+
): EmitterSubscription {
50+
if (__DEV__) {
51+
checkNativeEventModule(eventType);
52+
}
53+
return super.addListener(eventType, listener, context);
54+
}
55+
56+
removeAllListeners(eventType: ?string) {
57+
if (__DEV__) {
58+
checkNativeEventModule(eventType);
59+
}
60+
super.removeAllListeners(eventType);
61+
}
62+
63+
removeSubscription(subscription: EmitterSubscription) {
64+
if (subscription.emitter !== this) {
65+
subscription.emitter.removeSubscription(subscription);
66+
} else {
67+
super.removeSubscription(subscription);
68+
}
69+
}
70+
}
71+
72+
export default new RCTDeviceEventEmitter();

packages/react-native-web/src/vendor/react-native/NativeEventEmitter/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
* @flow
99
*/
1010
'use strict';
11+
import invariant from 'fbjs/lib/invariant';
1112

1213
import EventEmitter from '../emitter/EventEmitter';
13-
import Platform from '../../../exports/Platform';
14+
import RCTDeviceEventEmitter from './RCTDeviceEventEmitter';
1415

15-
import invariant from 'fbjs/lib/invariant';
1616

1717
import type EmitterSubscription from '../emitter/EmitterSubscription';
1818

@@ -29,11 +29,7 @@ class NativeEventEmitter extends EventEmitter {
2929
_nativeModule: ?NativeModule;
3030

3131
constructor(nativeModule: ?NativeModule) {
32-
super();
33-
if (Platform.OS === 'ios') {
34-
invariant(nativeModule, 'Native module cannot be null.');
35-
this._nativeModule = nativeModule;
36-
}
32+
super(RCTDeviceEventEmitter.sharedSubscriber);
3733
}
3834

3935
addListener(

0 commit comments

Comments
 (0)