Skip to content

Commit b60e363

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Introduce ReactHost.destroy() method to notifies when the React instance is destroyed (#47534)
Summary: Pull Request resolved: #47534 This diff is introducing a new method to destroy React instance that allows the caller to be notified when the destroy finishes This is necessary for apps to act upon destroy of the react instance changelog: [internal] internal Reviewed By: shwanton Differential Revision: D65721107 fbshipit-source-id: 2d3d9755db38461ba381b86c72df5869c542379b
1 parent ae43411 commit b60e363

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ public abstract interface class com/facebook/react/ReactHost {
205205
public abstract fun addReactInstanceEventListener (Lcom/facebook/react/ReactInstanceEventListener;)V
206206
public abstract fun createSurface (Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Lcom/facebook/react/interfaces/fabric/ReactSurface;
207207
public abstract fun destroy (Ljava/lang/String;Ljava/lang/Exception;)Lcom/facebook/react/interfaces/TaskInterface;
208+
public abstract fun destroy (Ljava/lang/String;Ljava/lang/Exception;Lkotlin/jvm/functions/Function1;)Lcom/facebook/react/interfaces/TaskInterface;
209+
public static synthetic fun destroy$default (Lcom/facebook/react/ReactHost;Ljava/lang/String;Ljava/lang/Exception;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/facebook/react/interfaces/TaskInterface;
208210
public abstract fun getCurrentReactContext ()Lcom/facebook/react/bridge/ReactContext;
209211
public abstract fun getDevSupportManager ()Lcom/facebook/react/devsupport/interfaces/DevSupportManager;
210212
public abstract fun getLifecycleState ()Lcom/facebook/react/common/LifecycleState;
@@ -3789,6 +3791,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
37893791
public fun addReactInstanceEventListener (Lcom/facebook/react/ReactInstanceEventListener;)V
37903792
public fun createSurface (Landroid/content/Context;Ljava/lang/String;Landroid/os/Bundle;)Lcom/facebook/react/interfaces/fabric/ReactSurface;
37913793
public fun destroy (Ljava/lang/String;Ljava/lang/Exception;)Lcom/facebook/react/interfaces/TaskInterface;
3794+
public fun destroy (Ljava/lang/String;Ljava/lang/Exception;Lkotlin/jvm/functions/Function1;)Lcom/facebook/react/interfaces/TaskInterface;
37923795
public fun getCurrentReactContext ()Lcom/facebook/react/bridge/ReactContext;
37933796
public fun getDevSupportManager ()Lcom/facebook/react/devsupport/interfaces/DevSupportManager;
37943797
public fun getLifecycleState ()Lcom/facebook/react/common/LifecycleState;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,26 @@ public interface ReactHost {
116116
* be used to log properly the cause of destroy operation.
117117
* @return A task that completes when React Native gets destroyed.
118118
*/
119-
public fun destroy(reason: String, ex: Exception?): TaskInterface<Void>
119+
public fun destroy(
120+
reason: String,
121+
ex: Exception?,
122+
): TaskInterface<Void>
123+
124+
/**
125+
* Entrypoint to destroy the ReactInstance. If the ReactInstance is reloading, will wait until
126+
* reload is finished, before destroying.
127+
*
128+
* @param reason describing why ReactHost is being destroyed (e.g. memmory pressure)
129+
* @param ex exception that caused the trigger to destroy ReactHost (or null) This exception will
130+
* be used to log properly the cause of destroy operation.
131+
* @param onDestroyFinished callback that will be called when React Native gets destroyed.
132+
* @return A task that completes when React Native gets destroyed.
133+
*/
134+
public fun destroy(
135+
reason: String,
136+
ex: Exception?,
137+
onDestroyFinished: (instanceDestroyedSuccessfully: Boolean) -> Unit = {}
138+
): TaskInterface<Void>
120139

121140
/**
122141
* Permanently destroys the ReactHost, including the ReactInstance (if any). The application MUST

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import java.util.concurrent.atomic.AtomicReference;
8787
import kotlin.Unit;
8888
import kotlin.jvm.functions.Function0;
89+
import kotlin.jvm.functions.Function1;
8990

9091
/**
9192
* A ReactHost is an object that manages a single {@link ReactInstance}. A ReactHost can be
@@ -542,6 +543,25 @@ private void loadNetworkResource(String url, InspectorNetworkRequestListener lis
542543
InspectorNetworkHelper.loadNetworkResource(url, listener);
543544
}
544545

546+
@NonNull
547+
@Override
548+
public TaskInterface<Void> destroy(
549+
@NonNull String reason,
550+
@Nullable Exception ex,
551+
@NonNull Function1<? super Boolean, Unit> onDestroyFinished) {
552+
Task<Void> task = (Task<Void>) destroy(reason, ex);
553+
return task.continueWith(
554+
new Continuation<Void, Void>() {
555+
@Nullable
556+
@Override
557+
public Void then(@NonNull Task<Void> task) throws Exception {
558+
boolean instanceDestroyedSuccessfully = task.isCompleted() && !task.isFaulted();
559+
onDestroyFinished.invoke(instanceDestroyedSuccessfully);
560+
return null;
561+
}
562+
});
563+
}
564+
545565
/**
546566
* Entrypoint to destroy the ReactInstance. If the ReactInstance is reloading, will wait until
547567
* reload is finished, before destroying.

0 commit comments

Comments
 (0)