UNPKG

1.7 kBJavaScriptView Raw
1import { NativeModules } from 'react-native';
2
3const NativeProxy = NativeModules.ExpoNativeModuleProxy;
4const modulesConstantsKey = "modulesConstants";
5const exportedMethodsKey = "exportedMethods";
6
7const NativeModulesProxy = {};
8
9if (NativeProxy) {
10 Object.keys(NativeProxy[exportedMethodsKey]).forEach(moduleName => {
11 NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {};
12 NativeProxy[exportedMethodsKey][moduleName].forEach(methodInfo => {
13 NativeModulesProxy[moduleName][methodInfo.name] = async (...args) => {
14 const { key, argumentsCount } = methodInfo;
15 if (argumentsCount !== args.length) {
16 throw new Error(`Arguments count mismatch, ${args.length} provided, ${argumentsCount} have been expected.`);
17 }
18 return await NativeProxy.callMethod(moduleName, key, args);
19 };
20 });
21
22 // These are called by EventEmitter (which is a wrapper for NativeEventEmitter)
23 // only on iOS and they use iOS-specific native module, EXReactNativeEventEmitter.
24 //
25 // On Android only {start,stop}Observing are called on the native module
26 // and these should be exported as Expo methods.
27 NativeModulesProxy[moduleName].addListener = (...args) =>
28 NativeModules.EXReactNativeEventEmitter.addProxiedListener(moduleName, ...args);
29 NativeModulesProxy[moduleName].removeListeners = (...args) =>
30 NativeModules.EXReactNativeEventEmitter.removeProxiedListeners(moduleName, ...args);
31 });
32} else {
33 console.warn(
34 "No native NativeModulesProxy found among NativeModules, are you sure the expo-react-native-adapter's modules are linked properly?"
35 );
36}
37
38module.exports = NativeModulesProxy;