Skip to content

Commit c6076bc

Browse files
Implement getJSCallInvokerHolder for BridgelessCatalystInstance (#43400)
Summary: Pull Request resolved: #43400 Implement `getJSCallInvokerHolder()` for BridgelessCatalystInstance Changelog: [Android][Breaking] Implement `getJSCallInvokerHolder()` for Bridgeless Catalyst Instance Reviewed By: cortinico Differential Revision: D54650305 fbshipit-source-id: effac3daaad5173c2fd78ab11bbe3f3156a9c07b
1 parent a7fde7c commit c6076bc

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3570,7 +3570,7 @@ public abstract class com/facebook/react/runtime/BindingsInstaller {
35703570
}
35713571

35723572
public final class com/facebook/react/runtime/BridgelessCatalystInstance : com/facebook/react/bridge/CatalystInstance {
3573-
public fun <init> ()V
3573+
public fun <init> (Lcom/facebook/react/runtime/ReactHostImpl;)V
35743574
public fun addBridgeIdleDebugListener (Lcom/facebook/react/bridge/NotThreadSafeBridgeIdleDebugListener;)V
35753575
public fun addJSIModules (Ljava/util/List;)V
35763576
public fun callFunction (Ljava/lang/String;Ljava/lang/String;Lcom/facebook/react/bridge/NativeArray;)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessCatalystInstance.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import com.facebook.react.turbomodule.core.interfaces.NativeMethodCallInvokerHol
3232

3333
@DoNotStrip
3434
@DeprecatedInNewArchitecture
35-
public class BridgelessCatalystInstance : CatalystInstance {
35+
public class BridgelessCatalystInstance(private val reactHost: ReactHostImpl) : CatalystInstance {
36+
3637
override fun handleMemoryPressure(level: Int) {
3738
throw UnsupportedOperationException("Unimplemented method 'handleMemoryPressure'")
3839
}
@@ -161,8 +162,8 @@ public class BridgelessCatalystInstance : CatalystInstance {
161162
throw UnsupportedOperationException("Unimplemented method 'addJSIModules'")
162163
}
163164

164-
override fun getJSCallInvokerHolder(): CallInvokerHolder {
165-
throw UnsupportedOperationException("Unimplemented method 'getJSCallInvokerHolder'")
165+
override fun getJSCallInvokerHolder(): CallInvokerHolder? {
166+
return reactHost.getJSCallInvokerHolder()
166167
}
167168

168169
override fun getNativeMethodCallInvokerHolder(): NativeMethodCallInvokerHolder {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.runtime;
99

1010
import android.content.Context;
11+
import android.util.Log;
1112
import com.facebook.infer.annotation.Nullsafe;
1213
import com.facebook.react.bridge.Arguments;
1314
import com.facebook.react.bridge.Callback;
@@ -18,8 +19,6 @@
1819
import com.facebook.react.bridge.NativeArray;
1920
import com.facebook.react.bridge.NativeModule;
2021
import com.facebook.react.bridge.ReactApplicationContext;
21-
import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException;
22-
import com.facebook.react.bridge.ReactSoftExceptionLogger;
2322
import com.facebook.react.bridge.RuntimeExecutor;
2423
import com.facebook.react.bridge.UIManager;
2524
import com.facebook.react.bridge.WritableNativeArray;
@@ -84,11 +83,10 @@ public void setSourceURL(String sourceURL) {
8483

8584
@Override
8685
public CatalystInstance getCatalystInstance() {
87-
ReactSoftExceptionLogger.logSoftExceptionVerbose(
86+
Log.w(
8887
TAG,
89-
new ReactNoCrashBridgeNotAllowedSoftException(
90-
"getCatalystInstance() cannot be called when the bridge is disabled"));
91-
throw new UnsupportedOperationException("There is no Catalyst instance in bridgeless mode.");
88+
"[WARNING] Bridgeless doesn't support CatalystInstance. Accessing an API that's not part of the new architecture is not encouraged usage.");
89+
return new BridgelessCatalystInstance(mReactHost);
9290
}
9391

9492
@Override

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.facebook.react.runtime.internal.bolts.Continuation;
6262
import com.facebook.react.runtime.internal.bolts.Task;
6363
import com.facebook.react.runtime.internal.bolts.TaskCompletionSource;
64+
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
6465
import com.facebook.react.uimanager.UIManagerModule;
6566
import com.facebook.react.uimanager.events.BlackHoleEventDispatcher;
6667
import com.facebook.react.uimanager.events.EventDispatcher;
@@ -604,6 +605,20 @@ RuntimeExecutor getRuntimeExecutor() {
604605
return null;
605606
}
606607

608+
/* package */
609+
@Nullable
610+
CallInvokerHolder getJSCallInvokerHolder() {
611+
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
612+
if (reactInstance != null) {
613+
return reactInstance.getJSCallInvokerHolder();
614+
}
615+
ReactSoftExceptionLogger.logSoftException(
616+
TAG,
617+
new ReactNoCrashSoftException(
618+
"Tried to get JSCallInvokerHolder while instance is not ready"));
619+
return null;
620+
}
621+
607622
/**
608623
* To be called when the host activity receives an activity result.
609624
*

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ private native HybridData initHybrid(
461461

462462
private native void loadJSBundleFromAssets(AssetManager assetManager, String assetURL);
463463

464-
private native CallInvokerHolderImpl getJSCallInvokerHolder();
464+
/* package */ native CallInvokerHolderImpl getJSCallInvokerHolder();
465465

466466
private native NativeMethodCallInvokerHolderImpl getNativeMethodCallInvokerHolder();
467467

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/BridgelessReactContextTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class BridgelessReactContextTest {
5555
Assertions.assertThat(bridgelessReactContext.getFabricUIManager()).isEqualTo(fabricUiManager)
5656
}
5757

58-
@Test(expected = UnsupportedOperationException::class)
59-
fun getCatalystInstance_throwsException() {
60-
// Disable this test for now due to mocking FabricUIManager fails
61-
bridgelessReactContext.catalystInstance
58+
@Test
59+
fun getCatalystInstanceTest() {
60+
val bridgelessCatalystInstance = BridgelessCatalystInstance(reactHost)
61+
doReturn(bridgelessCatalystInstance).`when`(bridgelessReactContext).getCatalystInstance()
62+
Assertions.assertThat(bridgelessReactContext.getCatalystInstance())
63+
.isEqualTo(bridgelessCatalystInstance)
6264
}
6365
}

0 commit comments

Comments
 (0)