Migrate to Kotlin - BridgelessDevSupportManager (#50522)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50522

This diff migrates the following file to Kotlin - BridgelessDevSupportManager
as part of our ongoing effort of migrating the codebase to Kotlin
Changelog:
[Internal] [Changed] - BridgelessDevSupportManager to Kotlin

Reviewed By: arushikesarwani94

Differential Revision: D72558016

fbshipit-source-id: 9c84f89dfca03991b1feb6f8c9ffe846350aafed
This commit is contained in:
Nicola Corti
2025-04-08 01:25:16 -07:00
committed by Facebook GitHub Bot
parent 878b61a3ca
commit 7aebb3a1be
2 changed files with 104 additions and 124 deletions
@@ -1,124 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport;
import android.content.Context;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.SurfaceDelegateFactory;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager;
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback;
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager;
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
import com.facebook.react.packagerconnection.RequestHandler;
import java.util.Map;
/**
* An implementation of {@link com.facebook.react.devsupport.interfaces.DevSupportManager} that
* extends the functionality in {@link DevSupportManagerBase} with some additional, more flexible
* APIs for asynchronously loading the JS bundle.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
class BridgelessDevSupportManager extends DevSupportManagerBase {
public BridgelessDevSupportManager(
Context context,
ReactInstanceDevHelper reactInstanceManagerHelper,
@Nullable String packagerPathForJSBundleName) {
this(
context.getApplicationContext(),
reactInstanceManagerHelper,
packagerPathForJSBundleName,
true /* enableOnCreate */,
null /* redBoxHandler */,
null /* devBundleDownloadListener */,
2 /* minNumShakes */,
null /* customPackagerCommandHandlers */,
null /* surfaceDelegateFactory */,
null /* devLoadingViewManager */,
null /* pausedInDebuggerOverlayManager */);
}
/**
* This constructor mirrors the same constructor we have for {@link BridgeDevSupportManager} and
* is kept for backward compatibility.
*/
public BridgelessDevSupportManager(
Context applicationContext,
ReactInstanceDevHelper reactInstanceManagerHelper,
@Nullable String packagerPathForJSBundleName,
boolean enableOnCreate,
@Nullable RedBoxHandler redBoxHandler,
@Nullable DevBundleDownloadListener devBundleDownloadListener,
int minNumShakes,
@Nullable Map<String, RequestHandler> customPackagerCommandHandlers,
@Nullable SurfaceDelegateFactory surfaceDelegateFactory,
@Nullable DevLoadingViewManager devLoadingViewManager,
@Nullable PausedInDebuggerOverlayManager pausedInDebuggerOverlayManager) {
super(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers,
surfaceDelegateFactory,
devLoadingViewManager,
pausedInDebuggerOverlayManager);
}
@Override
protected String getUniqueTag() {
return "Bridgeless";
}
@Override
public void loadSplitBundleFromServer(
final String bundlePath, final DevSplitBundleCallback callback) {
fetchSplitBundleAndCreateBundleLoader(
bundlePath,
new CallbackWithBundleLoader() {
@Override
public void onSuccess(final JSBundleLoader bundleLoader) {
try {
mReactInstanceDevHelper.loadBundle(bundleLoader).waitForCompletion();
String bundleURL = getDevServerHelper().getDevServerSplitBundleURL(bundlePath);
ReactContext reactContext = mReactInstanceDevHelper.getCurrentReactContext();
if (reactContext != null) {
reactContext.getJSModule(HMRClient.class).registerBundle(bundleURL);
}
callback.onSuccess();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(
"[BridgelessDevSupportManager]: Got interrupted while loading bundle", e);
}
}
@Override
public void onError(String url, Throwable cause) {
callback.onError(url, cause);
}
});
}
@Override
public void handleReloadJS() {
UiThreadUtil.assertOnUiThread();
// dismiss redbox if exists
hideRedboxDialog();
mReactInstanceDevHelper.reload("BridgelessDevSupportManager.handleReloadJS()");
}
}
@@ -0,0 +1,104 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport
import android.content.Context
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.SurfaceDelegateFactory
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager
import com.facebook.react.devsupport.interfaces.DevSplitBundleCallback
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.packagerconnection.RequestHandler
/**
* An implementation of [DevSupportManager] that extends the functionality in
* [DevSupportManagerBase] with some additional, more flexible APIs for asynchronously loading the
* JS bundle.
*
* @constructor The primary constructor mirrors the same constructor we have for
* [BridgeDevSupportManager] and
* * is kept for backward compatibility.
*/
internal class BridgelessDevSupportManager(
applicationContext: Context,
reactInstanceManagerHelper: ReactInstanceDevHelper,
packagerPathForJSBundleName: String?,
enableOnCreate: Boolean,
redBoxHandler: RedBoxHandler?,
devBundleDownloadListener: DevBundleDownloadListener?,
minNumShakes: Int,
customPackagerCommandHandlers: Map<String, RequestHandler>?,
surfaceDelegateFactory: SurfaceDelegateFactory?,
devLoadingViewManager: DevLoadingViewManager?,
pausedInDebuggerOverlayManager: PausedInDebuggerOverlayManager?
) :
DevSupportManagerBase(
applicationContext,
reactInstanceManagerHelper,
packagerPathForJSBundleName,
enableOnCreate,
redBoxHandler,
devBundleDownloadListener,
minNumShakes,
customPackagerCommandHandlers,
surfaceDelegateFactory,
devLoadingViewManager,
pausedInDebuggerOverlayManager) {
constructor(
context: Context,
reactInstanceManagerHelper: ReactInstanceDevHelper,
packagerPathForJSBundleName: String?
) : this(
applicationContext = context.applicationContext,
reactInstanceManagerHelper = reactInstanceManagerHelper,
packagerPathForJSBundleName = packagerPathForJSBundleName,
enableOnCreate = true,
redBoxHandler = null,
devBundleDownloadListener = null,
minNumShakes = 2,
customPackagerCommandHandlers = null,
surfaceDelegateFactory = null,
devLoadingViewManager = null,
pausedInDebuggerOverlayManager = null)
override fun getUniqueTag(): String = "Bridgeless"
override fun loadSplitBundleFromServer(bundlePath: String, callback: DevSplitBundleCallback) {
fetchSplitBundleAndCreateBundleLoader(
bundlePath,
object : CallbackWithBundleLoader {
override fun onSuccess(bundleLoader: JSBundleLoader) {
try {
mReactInstanceDevHelper.loadBundle(bundleLoader).waitForCompletion()
val bundleURL = devServerHelper.getDevServerSplitBundleURL(bundlePath)
val reactContext = mReactInstanceDevHelper.currentReactContext
reactContext?.getJSModule(HMRClient::class.java)?.registerBundle(bundleURL)
callback.onSuccess()
} catch (e: InterruptedException) {
Thread.currentThread().interrupt()
throw RuntimeException(
"[BridgelessDevSupportManager]: Got interrupted while loading bundle", e)
}
}
override fun onError(url: String, cause: Throwable) = callback.onError(url, cause)
})
}
override fun handleReloadJS() {
UiThreadUtil.assertOnUiThread()
// dismiss redbox if exists
hideRedboxDialog()
mReactInstanceDevHelper.reload("BridgelessDevSupportManager.handleReloadJS()")
}
}