Skip to content

Commit

Permalink
Pull react instance method impls out of ReactContext
Browse files Browse the repository at this point in the history
Summary:
## Context
Prior, ReactContext used to implement bridge logic.

For bridgeless mode, we created BridgelessReactContext < ReactContext

## Problem

This could lead to failures: we could call bridge methods in bridgeless mode.

## Changes
Primary change:
- Make all the react instance methods inside ReactContext abstract.

Secondary changes: Implement react instance methods in concrete subclasses:
- **New:** BridgeReactContext: By delegating to CatalystInstance
- **New:** ThemedReactContext: By delegating to inner ReactContext
- **Unchanged:** BridgelessReactContext: By delegating to ReactHost

## Auxiliary changes
This fixes ThemedReactContext in bridgeless mode.

**Problem:** Prior, ThemedReactContext's react instance methods did not work in bridgeless mode: ThemedReactContext wasn't initialized in bridgeless mode, so all those methods had undefined behaviour.

**Solution:** ThemedReactContext now implements all react instance methods, by just forwarding to the initialized ReactContext it decorates (which has an instance).

Changelog: [Android][Removed] Delete ReactContext.initializeWithInstance(). ReactContext now no longer contains legacy react instance methods. Please use BridgeReactInstance instead.

 ---

Reviewed By: fkgozali

Differential Revision: D55505416

fbshipit-source-id: ce1e3ab379eb788d26130dd44a66544aada3db02
  • Loading branch information
RSNara authored and facebook-github-bot committed Mar 30, 2024
1 parent e05319c commit 14fb1cc
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 181 deletions.
66 changes: 45 additions & 21 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ public abstract class com/facebook/react/bridge/BaseJavaModule : com/facebook/re

public final class com/facebook/react/bridge/BridgeReactContext : com/facebook/react/bridge/ReactApplicationContext {
public fun <init> (Landroid/content/Context;)V
public fun destroy ()V
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
public fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
public fun getJavaScriptContextHolder ()Lcom/facebook/react/bridge/JavaScriptContextHolder;
public fun getNativeModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/NativeModule;
public fun getNativeModules ()Ljava/util/Collection;
public fun getSourceURL ()Ljava/lang/String;
public fun handleException (Ljava/lang/Exception;)V
public fun hasActiveCatalystInstance ()Z
public fun hasActiveReactInstance ()Z
public fun hasCatalystInstance ()Z
public fun hasNativeModule (Ljava/lang/Class;)Z
public final fun initializeWithInstance (Lcom/facebook/react/bridge/CatalystInstance;)V
public fun isBridgeless ()Z
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
}

public abstract interface class com/facebook/react/bridge/Callback {
Expand Down Expand Up @@ -1088,36 +1104,35 @@ public abstract class com/facebook/react/bridge/ReactContext : android/content/C
public fun assertOnNativeModulesQueueThread ()V
public fun assertOnNativeModulesQueueThread (Ljava/lang/String;)V
public fun assertOnUiQueueThread ()V
public fun destroy ()V
public abstract fun destroy ()V
public fun emitDeviceEvent (Ljava/lang/String;)V
public fun emitDeviceEvent (Ljava/lang/String;Ljava/lang/Object;)V
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
public abstract fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
public fun getCurrentActivity ()Landroid/app/Activity;
public fun getExceptionHandler ()Lcom/facebook/react/bridge/JSExceptionHandler;
public fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public abstract fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public fun getJSExceptionHandler ()Lcom/facebook/react/bridge/JSExceptionHandler;
public fun getJSMessageQueueThread ()Lcom/facebook/react/bridge/queue/MessageQueueThread;
public fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
public fun getJavaScriptContextHolder ()Lcom/facebook/react/bridge/JavaScriptContextHolder;
public abstract fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
public abstract fun getJavaScriptContextHolder ()Lcom/facebook/react/bridge/JavaScriptContextHolder;
public fun getLifecycleState ()Lcom/facebook/react/common/LifecycleState;
public fun getNativeModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/NativeModule;
public fun getNativeModules ()Ljava/util/Collection;
public abstract fun getNativeModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/NativeModule;
public abstract fun getNativeModules ()Ljava/util/Collection;
public fun getNativeModulesMessageQueueThread ()Lcom/facebook/react/bridge/queue/MessageQueueThread;
public fun getSourceURL ()Ljava/lang/String;
public abstract fun getSourceURL ()Ljava/lang/String;
public fun getSystemService (Ljava/lang/String;)Ljava/lang/Object;
public fun getUiMessageQueueThread ()Lcom/facebook/react/bridge/queue/MessageQueueThread;
public fun handleException (Ljava/lang/Exception;)V
public fun hasActiveCatalystInstance ()Z
public fun hasActiveReactInstance ()Z
public fun hasCatalystInstance ()Z
public abstract fun handleException (Ljava/lang/Exception;)V
public abstract fun hasActiveCatalystInstance ()Z
public abstract fun hasActiveReactInstance ()Z
public abstract fun hasCatalystInstance ()Z
public fun hasCurrentActivity ()Z
public fun hasNativeModule (Ljava/lang/Class;)Z
public abstract fun hasNativeModule (Ljava/lang/Class;)Z
protected fun initializeFromOther (Lcom/facebook/react/bridge/ReactContext;)V
protected fun initializeInteropModules ()V
protected fun initializeInteropModules (Lcom/facebook/react/bridge/ReactContext;)V
public fun initializeMessageQueueThreads (Lcom/facebook/react/bridge/queue/ReactQueueConfiguration;)V
public fun initializeWithInstance (Lcom/facebook/react/bridge/CatalystInstance;)V
public fun internal_registerInteropModule (Ljava/lang/Class;Ljava/lang/Object;)V
public fun isBridgeless ()Z
public abstract fun isBridgeless ()Z
public fun isOnJSQueueThread ()Z
public fun isOnNativeModulesQueueThread ()Z
public fun isOnUiQueueThread ()Z
Expand All @@ -1127,7 +1142,7 @@ public abstract class com/facebook/react/bridge/ReactContext : android/content/C
public fun onHostResume (Landroid/app/Activity;)V
public fun onNewIntent (Landroid/app/Activity;Landroid/content/Intent;)V
public fun onWindowFocusChange (Z)V
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
public abstract fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
public fun removeActivityEventListener (Lcom/facebook/react/bridge/ActivityEventListener;)V
public fun removeLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
public fun removeWindowFocusChangeListener (Lcom/facebook/react/bridge/WindowFocusChangeListener;)V
Expand Down Expand Up @@ -4822,16 +4837,25 @@ public class com/facebook/react/uimanager/ThemedReactContext : com/facebook/reac
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;)V
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;)V
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;I)V
public fun addLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
public fun getCurrentActivity ()Landroid/app/Activity;
public fun destroy ()V
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
public fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
public fun getJavaScriptContextHolder ()Lcom/facebook/react/bridge/JavaScriptContextHolder;
public fun getModuleName ()Ljava/lang/String;
public fun getNativeModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/NativeModule;
public fun getNativeModules ()Ljava/util/Collection;
public fun getReactApplicationContext ()Lcom/facebook/react/bridge/ReactApplicationContext;
public fun getSourceURL ()Ljava/lang/String;
public fun getSurfaceID ()Ljava/lang/String;
public fun getSurfaceId ()I
public fun hasCurrentActivity ()Z
public fun handleException (Ljava/lang/Exception;)V
public fun hasActiveCatalystInstance ()Z
public fun hasActiveReactInstance ()Z
public fun hasCatalystInstance ()Z
public fun hasNativeModule (Ljava/lang/Class;)Z
public fun isBridgeless ()Z
public fun removeLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
}

public class com/facebook/react/uimanager/TouchTargetHelper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,194 @@
package com.facebook.react.bridge

import android.content.Context
import com.facebook.common.logging.FLog
import com.facebook.react.bridge.queue.ReactQueueConfiguration
import com.facebook.react.common.ReactConstants
import com.facebook.react.common.annotations.DeprecatedInNewArchitecture
import com.facebook.react.common.annotations.FrameworkAPI
import com.facebook.react.common.annotations.UnstableReactNativeAPI

/**
* This is the bridge-specific concrete subclass of ReactContext. ReactContext has many methods that
* delegate to the react instance. This subclass will implement those methods, by delegating to the
* delegate to the react instance. This subclass implements those methods, by delegating to the
* CatalystInstance. If you need to create a ReactContext within an "bridge context", please create
* BridgeReactContext.
*/
@DeprecatedInNewArchitecture
public class BridgeReactContext(base: Context) : ReactApplicationContext(base) {}
public class BridgeReactContext(base: Context) : ReactApplicationContext(base) {
@Volatile private var destroyed = false
private var catalystInstance: CatalystInstance? = null

public fun initializeWithInstance(otherCatalystInstance: CatalystInstance?): Unit {
if (otherCatalystInstance == null) {
throw IllegalArgumentException("CatalystInstance cannot be null.")
}
if (catalystInstance != null) {
throw IllegalStateException("ReactContext has been already initialized")
}
if (destroyed) {
ReactSoftExceptionLogger.logSoftException(
TAG, IllegalStateException("Cannot initialize ReactContext after it has been destroyed."))
}

catalystInstance = otherCatalystInstance

val queueConfig: ReactQueueConfiguration = otherCatalystInstance.reactQueueConfiguration
initializeMessageQueueThreads(queueConfig)
initializeInteropModules()
}

public override fun <T : JavaScriptModule?> getJSModule(jsInterface: Class<T>?): T? {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_JS_ACCESS_EXCEPTION_MESSAGE
else EARLY_JS_ACCESS_EXCEPTION_MESSAGE)

val interopModuleRegistry = mInteropModuleRegistry
if (interopModuleRegistry != null &&
interopModuleRegistry.shouldReturnInteropModule(jsInterface)) {
return interopModuleRegistry.getInteropModule(jsInterface)
}

return instance.getJSModule(jsInterface)
}

public override fun <T : NativeModule?> hasNativeModule(
nativeModuleInterface: Class<T>?
): Boolean {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_NATIVE_MODULE_EXCEPTION_MESSAGE
else EARLY_NATIVE_MODULE_EXCEPTION_MESSAGE)
return instance.hasNativeModule(nativeModuleInterface)
}

public override fun getNativeModules(): MutableCollection<NativeModule> {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_NATIVE_MODULE_EXCEPTION_MESSAGE
else EARLY_NATIVE_MODULE_EXCEPTION_MESSAGE)
return instance.nativeModules
}

public override fun <T : NativeModule?> getNativeModule(nativeModuleInterface: Class<T>?): T? {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_NATIVE_MODULE_EXCEPTION_MESSAGE
else EARLY_NATIVE_MODULE_EXCEPTION_MESSAGE)
return instance.getNativeModule(nativeModuleInterface)
}

@FrameworkAPI
@UnstableReactNativeAPI
public override fun getRuntimeExecutor(): RuntimeExecutor? {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_RUNTIME_EXECUTOR_ACCESS_EXCEPTION_MESSAGE
else EARLY_RUNTIME_EXECUTOR_ACCESS_EXCEPTION_MESSAGE)

return instance.getRuntimeExecutor()
}

public override fun getCatalystInstance(): CatalystInstance {
return catalystInstance!!
}

@Deprecated(
"This API is unsupported in the New Architecture.", ReplaceWith("hasActiveReactInstance()"))
public override fun hasActiveCatalystInstance(): Boolean {
return hasActiveReactInstance()
}

public override fun hasActiveReactInstance(): Boolean {
val instance = catalystInstance
return instance != null && !instance.isDestroyed
}

public override fun hasCatalystInstance(): Boolean {
return catalystInstance != null
}

public override fun destroy(): Unit {
UiThreadUtil.assertOnUiThread()

destroyed = true
catalystInstance?.destroy()
}

public override fun handleException(e: Exception?): Unit {
val jsExceptionHandler: JSExceptionHandler? = jsExceptionHandler

if (hasActiveReactInstance() && jsExceptionHandler != null) {
jsExceptionHandler.handleException(e)
} else {
FLog.e(
ReactConstants.TAG,
"Unable to handle Exception - catalystInstanceVariableExists: " +
(catalystInstance != null) +
" - isCatalystInstanceAlive: " +
hasActiveReactInstance() +
" - hasExceptionHandler: " +
(jsExceptionHandler != null),
e)
throw IllegalStateException(e)
}
}

public override fun isBridgeless(): Boolean {
return false
}

public override fun getJavaScriptContextHolder(): JavaScriptContextHolder? {
return catalystInstance?.javaScriptContextHolder
}

public override fun getFabricUIManager(): UIManager? {
val instance =
catalystInstance
?: throw IllegalStateException(
if (destroyed) LATE_FABRIC_UI_MANAGER_ACCESS_EXCEPTION_MESSAGE
else EARLY_FABRIC_UI_MANAGER_ACCESS_EXCEPTION_MESSAGE)

return instance.fabricUIManager ?: instance.getJSIModule(JSIModuleType.UIManager) as? UIManager
}

public override fun getSourceURL(): String? {
return catalystInstance?.sourceURL
}

public override fun registerSegment(segmentId: Int, path: String?, callback: Callback?): Unit {
catalystInstance!!.registerSegment(segmentId, path)
callback!!.invoke()
}

private companion object {
private const val TAG = "BridgeReactContext"

private const val EARLY_JS_ACCESS_EXCEPTION_MESSAGE =
("Tried to access a JS module before the React instance was fully set up. Calls to " +
"ReactContext#getJSModule should only happen once initialize() has been called on your " +
"native module.")
private const val LATE_JS_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a JS module after the React instance was destroyed."
private const val EARLY_NATIVE_MODULE_EXCEPTION_MESSAGE =
"Trying to call native module before CatalystInstance has been set!"
private const val LATE_NATIVE_MODULE_EXCEPTION_MESSAGE =
"Trying to call native module after CatalystInstance has been destroyed!"

private const val EARLY_RUNTIME_EXECUTOR_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a RuntimeExecutor before CatalystInstance has been set!"
private const val LATE_RUNTIME_EXECUTOR_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a RuntimeExecutor after CatalystInstance has been destroyed!"

private const val LATE_FABRIC_UI_MANAGER_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a FabricUIManager after CatalystInstance has been destroyed!"
private const val EARLY_FABRIC_UI_MANAGER_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a FabricUIManager after CatalystInstance before it has been set!"
}
}
Loading

0 comments on commit 14fb1cc

Please sign in to comment.