diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp index 2958350eedfb8e..9d6e57dc5f0a5d 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/TurboModuleManager.cpp @@ -196,7 +196,8 @@ TurboModuleManager::createTurboModuleProvider() { .moduleName = name, .instance = moduleInstance, .jsInvoker = jsCallInvoker, - .nativeMethodCallInvoker = nativeMethodCallInvoker}; + .nativeMethodCallInvoker = nativeMethodCallInvoker, + .shouldVoidMethodsExecuteSync = false}; auto turboModule = delegate->cthis()->getTurboModule(name, params); turboModuleCache->insert({name, turboModule}); @@ -270,7 +271,8 @@ TurboModuleManager::createLegacyModuleProvider() { .moduleName = name, .instance = moduleInstance, .jsInvoker = jsCallInvoker, - .nativeMethodCallInvoker = nativeMethodCallInvoker}; + .nativeMethodCallInvoker = nativeMethodCallInvoker, + .shouldVoidMethodsExecuteSync = false}; static auto getMethodDescriptorsFromModule = javaPart->getClass() diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp b/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp index 8d5a6f94d51ab3..521dcbaed5d9fe 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp @@ -31,7 +31,8 @@ namespace TMPL = TurboModulePerfLogger; JavaTurboModule::JavaTurboModule(const InitParams& params) : TurboModule(params.moduleName, params.jsInvoker), instance_(jni::make_global(params.instance)), - nativeMethodCallInvoker_(params.nativeMethodCallInvoker) {} + nativeMethodCallInvoker_(params.nativeMethodCallInvoker), + shouldVoidMethodsExecuteSync_(params.shouldVoidMethodsExecuteSync) {} JavaTurboModule::~JavaTurboModule() { /** @@ -454,7 +455,9 @@ jsi::Value JavaTurboModule::invokeJavaMethod( const char* methodName = methodNameStr.c_str(); const char* moduleName = name_.c_str(); - bool isMethodSync = !(valueKind == VoidKind || valueKind == PromiseKind); + bool isMethodSync = + (valueKind == VoidKind && shouldVoidMethodsExecuteSync_) || + !(valueKind == VoidKind || valueKind == PromiseKind); if (isMethodSync) { TMPL::syncMethodCallStart(moduleName, methodName); @@ -733,6 +736,18 @@ jsi::Value JavaTurboModule::invokeJavaMethod( return returnValue; } case VoidKind: { + if (shouldVoidMethodsExecuteSync_) { + env->CallVoidMethodA(instance, methodID, jargs.data()); + TMPL::syncMethodCallExecutionEnd(moduleName, methodName); + TMPL::syncMethodCallEnd(moduleName, methodName); + try { + FACEBOOK_JNI_THROW_PENDING_EXCEPTION(); + } catch (...) { + throw; + } + return jsi::Value::undefined(); + } + TMPL::asyncMethodCallArgConversionEnd(moduleName, methodName); TMPL::asyncMethodCallDispatch(moduleName, methodName); diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.h index 0e8ebd126dc496..eadf1b5065bb1f 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.h @@ -32,6 +32,7 @@ class JSI_EXPORT JavaTurboModule : public TurboModule { jni::alias_ref instance; std::shared_ptr jsInvoker; std::shared_ptr nativeMethodCallInvoker; + bool shouldVoidMethodsExecuteSync; }; JavaTurboModule(const InitParams& params); @@ -50,6 +51,7 @@ class JSI_EXPORT JavaTurboModule : public TurboModule { // instance_ can be of type JTurboModule, or JNativeModule jni::global_ref instance_; std::shared_ptr nativeMethodCallInvoker_; + bool shouldVoidMethodsExecuteSync_; }; } // namespace facebook::react