1 | import { NativeModules } from 'react-native';
|
2 |
|
3 | const NativeProxy = NativeModules.ExpoNativeModuleProxy;
|
4 | const modulesConstantsKey = "modulesConstants";
|
5 | const exportedMethodsKey = "exportedMethods";
|
6 |
|
7 | const NativeModulesProxy = {};
|
8 |
|
9 | if (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 |
|
23 |
|
24 |
|
25 |
|
26 |
|
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 |
|
38 | module.exports = NativeModulesProxy;
|