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: javache

Differential Revision: D53145010

fbshipit-source-id: 2405bc24afb00864117d3c504fc9c4cbffd7203a
  • Loading branch information
RSNara authored and facebook-github-bot committed Mar 28, 2024
1 parent 5ceb928 commit f99dc48
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 181 deletions.
70 changes: 49 additions & 21 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,27 @@ 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 static final field Companion Lcom/facebook/react/bridge/BridgeReactContext$Companion;
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 final class com/facebook/react/bridge/BridgeReactContext$Companion {
}

public abstract interface class com/facebook/react/bridge/Callback {
Expand Down Expand Up @@ -1088,36 +1108,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 +1146,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 +4841,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,192 @@
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

fun initializeWithInstance(otherCatalystInstance: CatalystInstance?) {
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()
}

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)
}

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)
}

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
}

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
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()
}

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

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

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

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

override fun destroy() {
UiThreadUtil.assertOnUiThread()

destroyed = true
catalystInstance?.destroy()
}

override fun handleException(e: Exception?) {
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)
}
}

override fun isBridgeless(): Boolean {
return false
}

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

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
}

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

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

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 f99dc48

Please sign in to comment.