UNPKG

1.92 kBJavaScriptView Raw
1import { NativeModules } from 'react-native';
2const NativeProxy = NativeModules.NativeUnimoduleProxy;
3const modulesConstantsKey = 'modulesConstants';
4const exportedMethodsKey = 'exportedMethods';
5const NativeModulesProxy = {};
6if (NativeProxy) {
7 Object.keys(NativeProxy[exportedMethodsKey]).forEach(moduleName => {
8 NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {};
9 NativeProxy[exportedMethodsKey][moduleName].forEach(methodInfo => {
10 NativeModulesProxy[moduleName][methodInfo.name] = (...args) => {
11 const { key, argumentsCount } = methodInfo;
12 if (argumentsCount !== args.length) {
13 return Promise.reject(new Error(`Native method ${moduleName}.${methodInfo.name} expects ${argumentsCount} ${argumentsCount === 1 ? 'argument' : 'arguments'} but received ${args.length}`));
14 }
15 return NativeProxy.callMethod(moduleName, key, args);
16 };
17 });
18 // These are called by EventEmitter (which is a wrapper for NativeEventEmitter)
19 // only on iOS and they use iOS-specific native module, EXReactNativeEventEmitter.
20 //
21 // On Android only {start,stop}Observing are called on the native module
22 // and these should be exported as Expo methods.
23 NativeModulesProxy[moduleName].addListener = (...args) => NativeModules.UMReactNativeEventEmitter.addProxiedListener(moduleName, ...args);
24 NativeModulesProxy[moduleName].removeListeners = (...args) => NativeModules.UMReactNativeEventEmitter.removeProxiedListeners(moduleName, ...args);
25 });
26}
27else {
28 console.warn(`The "UMNativeModulesProxy" native module is not exported through NativeModules; verify that @unimodules/react-native-adapter's native code is linked properly`);
29}
30export default NativeModulesProxy;
31//# sourceMappingURL=NativeModulesProxy.native.js.map
\No newline at end of file