Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -1973,7 +1973,7 @@ public abstract interface class com/facebook/react/devsupport/DevServerHelper$Pa
public abstract fun onPackagerReloadCommand ()V
}

public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/facebook/react/devsupport/interfaces/DevSupportManager, com/facebook/react/devsupport/interfaces/PerfMonitorV2Handler {
public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/facebook/react/devsupport/interfaces/DevSupportManager {
public static final field Companion Lcom/facebook/react/devsupport/DevSupportManagerBase$Companion;
public fun <init> (Landroid/content/Context;Lcom/facebook/react/devsupport/ReactInstanceDevHelper;Ljava/lang/String;ZLcom/facebook/react/devsupport/interfaces/RedBoxHandler;Lcom/facebook/react/devsupport/interfaces/DevBundleDownloadListener;ILjava/util/Map;Lcom/facebook/react/common/SurfaceDelegateFactory;Lcom/facebook/react/devsupport/interfaces/DevLoadingViewManager;Lcom/facebook/react/devsupport/interfaces/PausedInDebuggerOverlayManager;)V
public fun addCustomDevOption (Ljava/lang/String;Lcom/facebook/react/devsupport/interfaces/DevOptionHandler;)V
Expand Down Expand Up @@ -2031,7 +2031,6 @@ public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/
public fun startInspector ()V
public fun stopInspector ()V
public fun toggleElementInspector ()V
public fun unstable_updatePerfMonitor (Ljava/lang/String;III)V
}

public abstract interface class com/facebook/react/devsupport/DevSupportManagerBase$CallbackWithBundleLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ import com.facebook.react.devsupport.interfaces.ErrorCustomizer
import com.facebook.react.devsupport.interfaces.ErrorType
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager
import com.facebook.react.devsupport.interfaces.PerfMonitorOverlayManager
import com.facebook.react.devsupport.interfaces.PerfMonitorV2Handler
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.devsupport.interfaces.StackFrame
import com.facebook.react.devsupport.perfmonitor.PerfMonitorDevHelper
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags
import com.facebook.react.modules.core.RCTNativeAppEventEmitter
Expand All @@ -92,7 +91,7 @@ public abstract class DevSupportManagerBase(
private val surfaceDelegateFactory: SurfaceDelegateFactory?,
public var devLoadingViewManager: DevLoadingViewManager?,
private var pausedInDebuggerOverlayManager: PausedInDebuggerOverlayManager?,
) : DevSupportManager, PerfMonitorV2Handler {
) : DevSupportManager {

public interface CallbackWithBundleLoader {
public fun onSuccess(bundleLoader: JSBundleLoader)
Expand Down Expand Up @@ -184,7 +183,7 @@ public abstract class DevSupportManagerBase(
null
}

private var perfMonitorOverlayManager: PerfMonitorOverlayManager? = null
private var perfMonitorOverlayManager: PerfMonitorOverlayViewManager? = null

init {
// We store JS bundle loaded from dev server in a single destination in app's data dir.
Expand Down Expand Up @@ -216,6 +215,7 @@ public abstract class DevSupportManagerBase(
if (
ReactNativeNewArchitectureFeatureFlags.enableBridgelessArchitecture() &&
ReactNativeFeatureFlags.perfMonitorV2Enabled() &&
reactInstanceDevHelper is PerfMonitorDevHelper &&
perfMonitorOverlayManager == null
) {
perfMonitorOverlayManager =
Expand All @@ -227,7 +227,7 @@ public abstract class DevSupportManagerBase(
}
context
},
{ openDebugger() },
reactInstanceDevHelper.inspectorTarget,
)
}
}
Expand Down Expand Up @@ -507,6 +507,12 @@ public abstract class DevSupportManagerBase(

override fun onNewReactContextCreated(reactContext: ReactContext) {
resetCurrentContext(reactContext)

if (perfMonitorOverlayManager != null && reactInstanceDevHelper is PerfMonitorDevHelper) {
perfMonitorOverlayManager?.let { manager ->
reactInstanceDevHelper.inspectorTarget?.addPerfMonitorListener(manager)
}
}
}

override fun onReactInstanceDestroyed(reactContext: ReactContext) {
Expand Down Expand Up @@ -936,22 +942,6 @@ public abstract class DevSupportManagerBase(
pausedInDebuggerOverlayManager?.hidePausedInDebuggerOverlay()
}

override fun unstable_updatePerfMonitor(
eventName: String,
durationMs: Int,
responsivenessScore: Int,
ttl: Int,
) {
perfMonitorOverlayManager?.update(
PerfMonitorOverlayManager.PerfMonitorUpdateData(
eventName,
durationMs,
responsivenessScore,
ttl,
)
)
}

override fun setAdditionalOptionForPackager(name: String, value: String) {
devSettings.packagerConnectionSettings.setAdditionalOptionForPackager(name, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ import androidx.core.view.WindowInsetsCompat
import com.facebook.react.R
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.devsupport.interfaces.PerfMonitorOverlayManager
import com.facebook.react.devsupport.perfmonitor.PerfMonitorInspectorTargetBinding
import com.facebook.react.devsupport.perfmonitor.PerfMonitorUpdateListener
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.PixelUtil
import java.util.Locale

internal class PerfMonitorOverlayViewManager(
private val contextSupplier: Supplier<Context?>,
private val onRequestAnalyzeTrace: () -> Unit,
) : PerfMonitorOverlayManager {
private val inspectorTarget: PerfMonitorInspectorTargetBinding?,
) : PerfMonitorOverlayManager, PerfMonitorUpdateListener {
private var initialized: Boolean = false
private var enabled: Boolean = false
private var hasInteractionData: Boolean = false
private var interactionDialog: Dialog? = null
private var buttonDialog: Dialog? = null
private var interactionNameLabel: TextView? = null
private var durationLabel: TextView? = null
private var ttl: Int = 0
private var hideAfterTimeoutHandler: Handler? = null
Expand Down Expand Up @@ -67,14 +68,13 @@ internal class PerfMonitorOverlayViewManager(
}
}

override fun update(data: PerfMonitorOverlayManager.PerfMonitorUpdateData) {
override fun onNewFocusedEvent(data: PerfMonitorUpdateListener.LongTaskEventData) {
UiThreadUtil.runOnUiThread {
ensureInitialized()
interactionNameLabel?.text = data.eventName
durationLabel?.text = String.format(Locale.US, "%d ms", data.durationMs)
durationLabel?.setTextColor(getDurationHighlightColor(data.responsivenessScore))
hasInteractionData = true
this.ttl = data.ttl
ttl = data.ttl

hideAfterTimeoutHandler?.removeCallbacksAndMessages(null)

Expand Down Expand Up @@ -115,9 +115,10 @@ internal class PerfMonitorOverlayViewManager(

private fun createDialog(context: Context) {
val containerLayout = createInnerLayout(context)
interactionNameLabel =
val longTaskLabel =
TextView(context).apply {
textSize = TEXT_SIZE_PRIMARY
text = "Long Task"
setTextColor(Color.WHITE)
typeface = TYPEFACE_BOLD
}
Expand All @@ -127,7 +128,7 @@ internal class PerfMonitorOverlayViewManager(
setTextColor(COLOR_TEXT_GREEN)
typeface = TYPEFACE_BOLD
}
containerLayout.addView(interactionNameLabel)
containerLayout.addView(longTaskLabel)
containerLayout.addView(durationLabel)

val dialog =
Expand Down Expand Up @@ -176,7 +177,7 @@ internal class PerfMonitorOverlayViewManager(
dpToPx(8f).toInt(),
)
addView(buttonInner)
setOnClickListener { onRequestAnalyzeTrace() }
setOnClickListener { inspectorTarget?.pauseAndAnalyzeTrace() }
}
val dialog =
createAnchoredDialog(context, dpToPx(0f), dpToPx(0f)).apply { setContentView(buttonView) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ package com.facebook.react.devsupport.interfaces

/** [Experimental] Interface to manage the V2 Perf Monitor overlay. */
internal interface PerfMonitorOverlayManager {
data class PerfMonitorUpdateData(
val eventName: String,
val durationMs: Int,
val responsivenessScore: Int,
val ttl: Int,
)

/** Enable the Perf Monitor overlay. Will be shown when updates are received. */
public fun enable()

Expand All @@ -24,7 +17,4 @@ internal interface PerfMonitorOverlayManager {

/** Reset the Perf Monitor overlay, e.g. after a reload. */
public fun reset()

/** Update the state of the Perf Monitor overlay. */
public fun update(data: PerfMonitorUpdateData)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.perfmonitor

/**
* Interface implemented by [com.facebook.react.runtime.ReactHostImplDevHelper] exposing additional
* hooks used to implement the V2 Perf Monitor overlay (experimental).
*/
internal interface PerfMonitorDevHelper {
/**
* The inspector target object. Matches the lifetime of the ReactHost. May be null if modern JS
* debugging is disabled.
*/
public val inspectorTarget: PerfMonitorInspectorTarget?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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.perfmonitor

internal interface PerfMonitorEventDispatcher {
public fun addPerfMonitorListener(listener: PerfMonitorUpdateListener)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.perfmonitor

internal interface PerfMonitorInspectorTarget :
PerfMonitorEventDispatcher, PerfMonitorInspectorTargetBinding {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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.perfmonitor

/**
* [Experimental] Interface implemented by [com.facebook.react.runtime.ReactHostInspectorTarget]
* exposing actions for the V2 Perf Monitor.
*/
internal interface PerfMonitorInspectorTargetBinding {
public fun pauseAndAnalyzeTrace()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.perfmonitor

/** [Experimental] An interface for subscribing to updates for the V2 Perf Monitor. */
internal interface PerfMonitorUpdateListener {
data class LongTaskEventData(
val durationMs: Int,
val responsivenessScore: Int,
val ttl: Int,
)

/** Called when a new active performance event should be displayed. */
fun onNewFocusedEvent(data: LongTaskEventData)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import com.facebook.react.devsupport.inspector.InspectorNetworkRequestListener
import com.facebook.react.devsupport.interfaces.BundleLoadCallback
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.devsupport.interfaces.DevSupportManager.PausedInDebuggerOverlayCommandListener
import com.facebook.react.devsupport.interfaces.PerfMonitorV2Handler
import com.facebook.react.fabric.ComponentFactory
import com.facebook.react.fabric.FabricUIManager
import com.facebook.react.interfaces.TaskInterface
Expand Down Expand Up @@ -133,7 +132,7 @@ public class ReactHostImpl(
CopyOnWriteArrayList()
private val beforeDestroyListeners: MutableList<() -> Unit> = CopyOnWriteArrayList()

private var reactHostInspectorTarget: ReactHostInspectorTarget? = null
internal var reactHostInspectorTarget: ReactHostInspectorTarget? = null

@Volatile private var hostInvalidated = false

Expand Down Expand Up @@ -419,18 +418,6 @@ public class ReactHostImpl(
}
}

@DoNotStrip
private fun unstable_updatePerfMonitor(
eventName: String,
durationMs: Int,
responsivenessScore: Int,
ttl: Int,
) {
if (devSupportManager is PerfMonitorV2Handler) {
devSupportManager.unstable_updatePerfMonitor(eventName, durationMs, responsivenessScore, ttl)
}
}

@get:DoNotStrip
private val hostMetadata: Map<String, String?>
get() = AndroidInfoHelpers.getInspectorHostMetadata(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.facebook.react.bridge.ReactContext
import com.facebook.react.common.annotations.FrameworkAPI
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.devsupport.ReactInstanceDevHelper
import com.facebook.react.devsupport.perfmonitor.PerfMonitorDevHelper
import com.facebook.react.devsupport.perfmonitor.PerfMonitorInspectorTarget
import com.facebook.react.interfaces.TaskInterface
import com.facebook.react.modules.core.DeviceEventManagerModule

Expand All @@ -28,7 +30,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
@UnstableReactNativeAPI
@OptIn(FrameworkAPI::class)
internal class ReactHostImplDevHelper(private val delegate: ReactHostImpl) :
ReactInstanceDevHelper {
ReactInstanceDevHelper, PerfMonitorDevHelper {

override val currentActivity: Activity?
get() = delegate.lastUsedActivity
Expand All @@ -39,6 +41,9 @@ internal class ReactHostImplDevHelper(private val delegate: ReactHostImpl) :
override val currentReactContext: ReactContext?
get() = delegate.currentReactContext

override val inspectorTarget: PerfMonitorInspectorTarget?
get() = delegate.reactHostInspectorTarget

override fun onJSBundleLoadedFromServer() {
// Not implemented, only referenced by BridgeDevSupportManager
}
Expand Down
Loading
Loading