Skip to content

Commit

Permalink
Push ReactContext logic in derived classes (#44226)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44226

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

Yet another attempt to land this (last one was D55964787).

Copy-pasting below the amazing summary from RSNara.

## 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).

NOTE: Intentionally not converting `BridgeReactContext` to Kotlin to minimize the risk of these changes.

Reviewed By: cortinico

Differential Revision: D56064036

fbshipit-source-id: 2e380bf7ee46892c5fc0044b03a929f12d122157
  • Loading branch information
RSNara authored and facebook-github-bot committed May 21, 2024
1 parent cfa784c commit fb23470
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 188 deletions.
75 changes: 55 additions & 20 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,29 @@ public abstract class com/facebook/react/bridge/BaseJavaModule : com/facebook/re
public fun invalidate ()V
}

public final class com/facebook/react/bridge/BridgeReactContext : com/facebook/react/bridge/ReactApplicationContext {
public 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 getJSCallInvokerHolder ()Lcom/facebook/react/turbomodule/core/interfaces/CallInvokerHolder;
public fun getJSModule (Ljava/lang/Class;)Lcom/facebook/react/bridge/JavaScriptModule;
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 fun hasReactInstance ()Z
public 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/BridgeReactContext$RCTDeviceEventEmitter : com/facebook/react/bridge/JavaScriptModule {
public abstract fun emit (Ljava/lang/String;Ljava/lang/Object;)V
}

public abstract interface class com/facebook/react/bridge/Callback {
Expand Down Expand Up @@ -1071,38 +1092,37 @@ 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 fun getJSCallInvokerHolder ()Lcom/facebook/react/turbomodule/core/interfaces/CallInvokerHolder;
public abstract fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public abstract fun getJSCallInvokerHolder ()Lcom/facebook/react/turbomodule/core/interfaces/CallInvokerHolder;
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 fun hasReactInstance ()Z
public abstract fun hasNativeModule (Ljava/lang/Class;)Z
public abstract fun hasReactInstance ()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 @@ -1113,7 +1133,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 @@ -4964,14 +4984,29 @@ public class com/facebook/react/uimanager/ThemedReactContext : com/facebook/reac
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 destroy ()V
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
public fun getCurrentActivity ()Landroid/app/Activity;
public fun getFabricUIManager ()Lcom/facebook/react/bridge/UIManager;
public fun getJSCallInvokerHolder ()Lcom/facebook/react/turbomodule/core/interfaces/CallInvokerHolder;
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 handleException (Ljava/lang/Exception;)V
public fun hasActiveCatalystInstance ()Z
public fun hasActiveReactInstance ()Z
public fun hasCatalystInstance ()Z
public fun hasCurrentActivity ()Z
public fun hasNativeModule (Ljava/lang/Class;)Z
public fun hasReactInstance ()Z
public fun isBridgeless ()Z
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
public fun removeLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
}

Expand Down
Loading

0 comments on commit fb23470

Please sign in to comment.