Skip to content

Commit f5502fb

Browse files
rubennortefacebook-github-bot
authored andcommitted
Use native module passed to NativeEventEmitter on Android
Summary: ## Context In native modules implementing an event emitter, we can wait for JS to subscribe to an event before making the subscription to the right native API in the native module. This is only supported on iOS at the moment and we want to support it on Android too, so we can manage resources more efficiently and avoid custom code to do this on Android, like this: https://www.internalfb.com/intern/diffusion/FBS/browse/master/xplat/js/RKJSModules/public/Dating/Profile/ProfileView/ProfileGemstoneProfileView.js?commit=165ad219e6bf&lines=302-304 The way this works now is by creating instances of `NativeEventEmitter`, where we pass a reference to the native module that needs to be notified when there are new subscriptions. We have explicit code to ignore this native modules in all platforms except for iOS: https://www.internalfb.com/intern/diffusion/FBS/browsefile/master/xplat/js/react-native-github/Libraries/EventEmitter/NativeEventEmitter.js?commit=5a1e671453465e844dd851c458cb2467a2db5d03&lines=44-52 ## Changes This removes the check for iOS from `NativeEventEmitter` so we also try to use the native module to notify subscriptions on Android. We have migrated all existing code passing a native module to `NativeEventEmtiter` to only pass it on iOS, so we don't change this behavior in existing code. Any other existing code using this API is most likely fine too. It didn't work before so the expectation is that the native module wouldn't be implemented on Android anyway. Changelog: [Android][Changed] - Modified `NativeEventEmitter` to also use the passed native module to report subscriptions on Android Reviewed By: yungsters Differential Revision: D27500994 fbshipit-source-id: ef82da04020fb08cd0ea4f1cfffd1da6453ab0b9
1 parent 38cfa93 commit f5502fb

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Libraries/EventEmitter/NativeEventEmitter.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ import Platform from '../Utilities/Platform';
1818
import RCTDeviceEventEmitter from './RCTDeviceEventEmitter';
1919
import invariant from 'invariant';
2020

21-
type NativeModule = $ReadOnly<
22-
interface {
23-
addListener: (eventType: string) => void,
24-
removeListeners: (count: number) => void,
25-
},
26-
>;
21+
interface NativeModule {
22+
addListener(eventType: string): void;
23+
removeListeners(count: number): void;
24+
}
2725

2826
export type {EventSubscription};
2927

@@ -47,8 +45,8 @@ export default class NativeEventEmitter<TEventToArgsMap: {...}>
4745
nativeModule != null,
4846
'`new NativeEventEmitter()` requires a non-null argument.',
4947
);
50-
this._nativeModule = nativeModule;
5148
}
49+
this._nativeModule = nativeModule;
5250
}
5351

5452
addListener<TEvent: $Keys<TEventToArgsMap>>(

0 commit comments

Comments
 (0)