Simplify InteropModuleRegistry API (#50863)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50863

No need for `shouldReturnInteropModule` if we can just use the nullability of what's returned by `getInteropModule` instead.

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D73501845

fbshipit-source-id: 9b7628707edc3eb288733baffaca59a9d3c40b40
This commit is contained in:
Pieter De Baets
2025-04-24 03:20:12 -07:00
committed by Facebook GitHub Bot
parent 22f2247a96
commit fc55cc3318
5 changed files with 9 additions and 43 deletions
@@ -105,9 +105,11 @@ public class BridgeReactContext extends ReactApplicationContext {
}
throw new IllegalStateException(EARLY_JS_ACCESS_EXCEPTION_MESSAGE);
}
if (mInteropModuleRegistry != null
&& mInteropModuleRegistry.shouldReturnInteropModule(jsInterface)) {
return mInteropModuleRegistry.getInteropModule(jsInterface);
if (mInteropModuleRegistry != null) {
T jsModule = mInteropModuleRegistry.getInteropModule(jsInterface);
if (jsModule != null) {
return jsModule;
}
}
return mCatalystInstance.getJSModule(jsInterface);
}
@@ -44,7 +44,7 @@ import java.util.concurrent.atomic.AtomicInteger;
/**
* This provides an implementation of the public CatalystInstance instance. It is public because it
* is built by XReactInstanceManager which is in a different package.
* is built by ReactInstanceManager which is in a different package.
*/
@DoNotStrip
@LegacyArchitecture
@@ -26,14 +26,10 @@ internal class InteropModuleRegistry {
private val supportedModules = mutableMapOf<Class<*>, Any?>()
fun <T : JavaScriptModule?> shouldReturnInteropModule(requestedModule: Class<T>): Boolean {
return checkReactFeatureFlagsConditions() && supportedModules.containsKey(requestedModule)
}
fun <T : JavaScriptModule?> getInteropModule(requestedModule: Class<T>): T? {
return if (checkReactFeatureFlagsConditions()) {
@Suppress("UNCHECKED_CAST")
supportedModules[requestedModule] as? T?
supportedModules[requestedModule] as? T
} else {
null
}
@@ -108,10 +108,8 @@ internal class BridgelessReactContext(context: Context, private val reactHost: R
}
override fun <T : JavaScriptModule> getJSModule(jsInterface: Class<T>): T? {
mInteropModuleRegistry?.let { reg ->
if (reg.shouldReturnInteropModule(jsInterface)) {
return reg.getInteropModule(jsInterface)
}
mInteropModuleRegistry?.getInteropModule(jsInterface)?.let {
return it
}
// TODO T189052462: ReactContext caches JavaScriptModule instances
@@ -37,36 +37,6 @@ class InteropModuleRegistryTest {
ReactNativeFeatureFlags.dangerouslyReset()
}
@Test
fun shouldReturnInteropModule_withFabricDisabled_returnsFalse() {
overrideFeatureFlags(false, false)
assertThat(underTest.shouldReturnInteropModule(RCTEventEmitter::class.java)).isFalse()
}
@Test
fun shouldReturnInteropModule_withFabricInteropDisabled_returnsFalse() {
overrideFeatureFlags(false, true)
assertThat(underTest.shouldReturnInteropModule(RCTEventEmitter::class.java)).isFalse()
}
@Test
fun shouldReturnInteropModule_withUnregisteredClass_returnsFalse() {
overrideFeatureFlags(true, true)
assertThat(underTest.shouldReturnInteropModule(JSTimers::class.java)).isFalse()
}
@Test
fun shouldReturnInteropModule_withRegisteredClass_returnsTrue() {
overrideFeatureFlags(true, true)
underTest.registerInteropModule(RCTEventEmitter::class.java, FakeRCTEventEmitter())
assertThat(underTest.shouldReturnInteropModule(RCTEventEmitter::class.java)).isTrue()
}
@Test
fun getInteropModule_withRegisteredClassAndInvalidFlags_returnsNull() {
overrideFeatureFlags(false, false)