1import { NativeModules } from 'react-native'; 2const ExpoNativeProxy = global.ExpoModules?.NativeModulesProxy; 3const LegacyNativeProxy = NativeModules.NativeUnimoduleProxy; 4const modulesConstantsKey = 'modulesConstants'; 5const exportedMethodsKey = 'exportedMethods'; 6const NativeModulesProxy = {}; 7if (LegacyNativeProxy) { 8 // use JSI proxy if available, fallback to legacy RN proxy 9 const NativeProxy = ExpoNativeProxy ?? LegacyNativeProxy; 10 Object.keys(NativeProxy[exportedMethodsKey]).forEach((moduleName) => { 11 // copy constants 12 NativeModulesProxy[moduleName] = NativeProxy[modulesConstantsKey][moduleName] || {}; 13 // copy methods 14 NativeProxy[exportedMethodsKey][moduleName].forEach((methodInfo) => { 15 NativeModulesProxy[moduleName][methodInfo.name] = (...args) => { 16 const { key, argumentsCount } = methodInfo; 17 if (argumentsCount !== args.length) { 18 return Promise.reject(new Error(`Native method ${moduleName}.${methodInfo.name} expects ${argumentsCount} ${argumentsCount === 1 ? 'argument' : 'arguments'} but received ${args.length}`)); 19 } 20 // We still want to call methods using the legacy proxy in SDK 46 21 return LegacyNativeProxy.callMethod(moduleName, key, args); 22 }; 23 }); 24 // These are called by EventEmitter (which is a wrapper for NativeEventEmitter) 25 // only on iOS and they use iOS-specific native module, EXReactNativeEventEmitter. 26 // 27 // On Android only {start,stop}Observing are called on the native module 28 // and these should be exported as Expo methods. 29 // 30 // Before the RN 65, addListener/removeListeners weren't called on Android. However, it no longer stays true. 31 // See https://github.com/facebook/react-native/commit/f5502fbda9fe271ff6e1d0da773a3a8ee206a453. 32 // That's why, we check if the `EXReactNativeEventEmitter` exists and only if yes, we use it in the listener implementation. 33 // Otherwise, those methods are NOOP. 34 if (NativeModules.EXReactNativeEventEmitter) { 35 NativeModulesProxy[moduleName].addListener = (...args) => NativeModules.EXReactNativeEventEmitter.addProxiedListener(moduleName, ...args); 36 NativeModulesProxy[moduleName].removeListeners = (...args) => NativeModules.EXReactNativeEventEmitter.removeProxiedListeners(moduleName, ...args); 37 } 38 else { 39 // Fixes on Android: 40 // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `addListener` method. 41 // WARN `new NativeEventEmitter()` was called with a non-null argument without the required `removeListeners` method. 42 NativeModulesProxy[moduleName].addListener = () => { }; 43 NativeModulesProxy[moduleName].removeListeners = () => { }; 44 } 45 }); 46} 47else { 48 console.warn(`The "EXNativeModulesProxy" native module is not exported through NativeModules; verify that expo-modules-core's native code is linked properly`); 49} 50export default NativeModulesProxy; 51//# sourceMappingURL=NativeModulesProxy.native.js.map