Skip to content

Commit

Permalink
Implement getJSCallInvokerHolder for BridgelessCatalystInstance (#43400)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arushikesarwani94 authored and huntie committed Mar 18, 2024
1 parent 3e5dd5e commit bccedff
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3581,7 +3581,7 @@ public abstract class com/facebook/react/runtime/BindingsInstaller {
}

public final class com/facebook/react/runtime/BridgelessCatalystInstance : com/facebook/react/bridge/CatalystInstance {
public fun <init> ()V
public fun <init> (Lcom/facebook/react/runtime/ReactHostImpl;)V
public fun addBridgeIdleDebugListener (Lcom/facebook/react/bridge/NotThreadSafeBridgeIdleDebugListener;)V
public fun addJSIModules (Ljava/util/List;)V
public fun callFunction (Ljava/lang/String;Ljava/lang/String;Lcom/facebook/react/bridge/NativeArray;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import com.facebook.react.turbomodule.core.interfaces.NativeMethodCallInvokerHol

@DoNotStrip
@DeprecatedInNewArchitecture
public class BridgelessCatalystInstance : CatalystInstance {
public class BridgelessCatalystInstance(private val reactHost: ReactHostImpl) : CatalystInstance {

override fun handleMemoryPressure(level: Int) {
throw UnsupportedOperationException("Unimplemented method 'handleMemoryPressure'")
}
Expand Down Expand Up @@ -161,8 +162,8 @@ public class BridgelessCatalystInstance : CatalystInstance {
throw UnsupportedOperationException("Unimplemented method 'addJSIModules'")
}

override fun getJSCallInvokerHolder(): CallInvokerHolder {
throw UnsupportedOperationException("Unimplemented method 'getJSCallInvokerHolder'")
override fun getJSCallInvokerHolder(): CallInvokerHolder? {
return reactHost.getJSCallInvokerHolder()
}

override fun getNativeMethodCallInvokerHolder(): NativeMethodCallInvokerHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.react.runtime;

import android.content.Context;
import android.util.Log;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
Expand All @@ -18,8 +19,6 @@
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException;
import com.facebook.react.bridge.ReactSoftExceptionLogger;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.bridge.WritableNativeArray;
Expand Down Expand Up @@ -84,11 +83,10 @@ public void setSourceURL(String sourceURL) {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.facebook.react.runtime.internal.bolts.Continuation;
import com.facebook.react.runtime.internal.bolts.Task;
import com.facebook.react.runtime.internal.bolts.TaskCompletionSource;
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.BlackHoleEventDispatcher;
import com.facebook.react.uimanager.events.EventDispatcher;
Expand Down Expand Up @@ -601,6 +602,20 @@ RuntimeExecutor getRuntimeExecutor() {
return null;
}

/* package */
@Nullable
CallInvokerHolder getJSCallInvokerHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
if (reactInstance != null) {
return reactInstance.getJSCallInvokerHolder();
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException(
"Tried to get JSCallInvokerHolder while instance is not ready"));
return null;
}

/**
* To be called when the host activity receives an activity result.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ private native HybridData initHybrid(

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

private native CallInvokerHolderImpl getJSCallInvokerHolder();
/* package */ native CallInvokerHolderImpl getJSCallInvokerHolder();

private native NativeMethodCallInvokerHolderImpl getNativeMethodCallInvokerHolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ class BridgelessReactContextTest {
Assertions.assertThat(bridgelessReactContext.getFabricUIManager()).isEqualTo(fabricUiManager)
}

@Test(expected = UnsupportedOperationException::class)
fun getCatalystInstance_throwsException() {
// Disable this test for now due to mocking FabricUIManager fails
bridgelessReactContext.catalystInstance
@Test
fun getCatalystInstanceTest() {
val bridgelessCatalystInstance = BridgelessCatalystInstance(reactHost)
doReturn(bridgelessCatalystInstance).`when`(bridgelessReactContext).getCatalystInstance()
Assertions.assertThat(bridgelessReactContext.getCatalystInstance())
.isEqualTo(bridgelessCatalystInstance)
}
}

0 comments on commit bccedff

Please sign in to comment.