From 4df224ca6df9dc593ab7090520578f27c520f361 Mon Sep 17 00:00:00 2001 From: Edmond Chui Date: Wed, 16 Oct 2024 01:27:28 -0700 Subject: [PATCH] Fusebox reload-to-profile (Part 2 of 2: JS) (#46905) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46905 Changelog: [General][Added] - Add support for reload-to-profile in Fusebox (Part 2 of 2: JS) Reviewed By: hoxyq Differential Revision: D64077813 fbshipit-source-id: a6d3326d8514f49e47074d21d37e00044ad62fd8 --- .../Libraries/Core/setUpReactDevTools.js | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/packages/react-native/Libraries/Core/setUpReactDevTools.js b/packages/react-native/Libraries/Core/setUpReactDevTools.js index 3448673178267d..cdfc33df66d717 100644 --- a/packages/react-native/Libraries/Core/setUpReactDevTools.js +++ b/packages/react-native/Libraries/Core/setUpReactDevTools.js @@ -11,6 +11,10 @@ 'use strict'; import type {Domain} from '../../src/private/debugging/setUpFuseboxReactDevToolsDispatcher'; +import type { + PartialReloadAndProfileConfig, + Spec as NativeReactDevToolsRuntimeSettingsModuleSpec, +} from '../../src/private/fusebox/specs/NativeReactDevToolsRuntimeSettingsModule'; if (__DEV__) { // Register dispatcher on global, which can be used later by Chrome DevTools frontend @@ -24,6 +28,8 @@ if (__DEV__) { const reactDevToolsSettingsManager = require('../../src/private/debugging/ReactDevToolsSettingsManager'); const serializedHookSettings = reactDevToolsSettingsManager.getGlobalHookSettings(); + const maybeReactDevToolsRuntimeSettingsModuleModule = + require('../../src/private/fusebox/specs/NativeReactDevToolsRuntimeSettingsModule').default; let hookSettings = null; if (serializedHookSettings != null) { @@ -36,8 +42,23 @@ if (__DEV__) { ); } } + + const reloadAndProfileConfigPersistence = + makeReloadAndProfileConfigPersistence( + maybeReactDevToolsRuntimeSettingsModuleModule, + ); + + const shouldStartProfilingNow = + reloadAndProfileConfigPersistence?.getReloadAndProfileConfig() + ?.shouldReloadAndProfile === true; + const profilingSettings = { + recordChangeDescriptions: + reloadAndProfileConfigPersistence?.getReloadAndProfileConfig() + ?.recordChangeDescriptions === true, + recordTimeline: false, + }; // Install hook before React is loaded. - initialize(hookSettings); + initialize(hookSettings, shouldStartProfilingNow, profilingSettings); // This should be defined in DEV, otherwise error is expected. const fuseboxReactDevToolsDispatcher = @@ -76,6 +97,23 @@ if (__DEV__) { nativeStyleEditorValidAttributes: Object.keys(ReactNativeStyleAttributes), resolveRNStyle, onSettingsUpdated: handleReactDevToolsSettingsUpdate, + isReloadAndProfileSupported: + maybeReactDevToolsRuntimeSettingsModuleModule != null, + isProfiling: + reloadAndProfileConfigPersistence?.getReloadAndProfileConfig() + ?.shouldReloadAndProfile === true, + onReloadAndProfile: (recordChangeDescriptions: boolean) => { + reloadAndProfileConfigPersistence?.setReloadAndProfileConfig({ + shouldReloadAndProfile: true, + recordChangeDescriptions, + }); + }, + onReloadAndProfileFlagsReset: () => { + reloadAndProfileConfigPersistence?.setReloadAndProfileConfig({ + shouldReloadAndProfile: false, + recordChangeDescriptions: false, + }); + }, }); } @@ -135,6 +173,23 @@ if (__DEV__) { ), websocket: ws, onSettingsUpdated: handleReactDevToolsSettingsUpdate, + isReloadAndProfileSupported: + maybeReactDevToolsRuntimeSettingsModuleModule != null, + isProfiling: + reloadAndProfileConfigPersistence?.getReloadAndProfileConfig() + ?.shouldReloadAndProfile === true, + onReloadAndProfile: (recordChangeDescriptions: boolean) => { + reloadAndProfileConfigPersistence?.setReloadAndProfileConfig({ + shouldReloadAndProfile: true, + recordChangeDescriptions, + }); + }, + onReloadAndProfileFlagsReset: () => { + reloadAndProfileConfigPersistence?.setReloadAndProfileConfig({ + shouldReloadAndProfile: false, + recordChangeDescriptions: false, + }); + }, }); } } @@ -166,3 +221,20 @@ if (__DEV__) { ); connectToWSBasedReactDevToolsFrontend(); // Try connecting once on load } + +function makeReloadAndProfileConfigPersistence( + maybeModule: ?NativeReactDevToolsRuntimeSettingsModuleSpec, +) { + if (maybeModule == null) { + return; + } + + return { + setReloadAndProfileConfig(config: PartialReloadAndProfileConfig): void { + maybeModule.setReloadAndProfileConfig(config); + }, + getReloadAndProfileConfig() { + return maybeModule.getReloadAndProfileConfig(); + }, + }; +}