Skip to content

Commit bad396b

Browse files
EdmondChuiHWfacebook-github-bot
authored andcommitted
Fusebox reload-to-profile (facebook#46856)
Summary: Changelog: [General][Added] - Add support for reload-to-profile in Fusebox. CDT: facebook/react-native-devtools-frontend#117 React: facebook/react#31021 This diff adds support for reload2profile under bridge and bridgeless for iOS, Android, and C++ Differential Revision: D63233256
1 parent ff1c382 commit bad396b

File tree

17 files changed

+413
-1
lines changed

17 files changed

+413
-1
lines changed

packages/react-native/Libraries/Core/setUpReactDevTools.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
'use strict';
1212

1313
import type {Domain} from '../../src/private/debugging/setUpFuseboxReactDevToolsDispatcher';
14+
import type {
15+
PartialReloadAndProfileConfig,
16+
Spec as NativeReactDevToolsRuntimeSettingsModuleSpec,
17+
} from '../../src/private/fusebox/specs/NativeReactDevToolsRuntimeSettingsModule';
1418

1519
if (__DEV__) {
1620
// Register dispatcher on global, which can be used later by Chrome DevTools frontend
@@ -24,6 +28,8 @@ if (__DEV__) {
2428
const reactDevToolsSettingsManager = require('../../src/private/debugging/ReactDevToolsSettingsManager');
2529
const serializedHookSettings =
2630
reactDevToolsSettingsManager.getGlobalHookSettings();
31+
const maybeReactDevToolsRuntimeSettingsModuleModule =
32+
require('../../src/private/fusebox/specs/NativeReactDevToolsRuntimeSettingsModule').default;
2733

2834
let hookSettings = null;
2935
if (serializedHookSettings != null) {
@@ -36,8 +42,17 @@ if (__DEV__) {
3642
);
3743
}
3844
}
45+
46+
const reloadAndProfileConfigPersistence =
47+
makeReloadAndProfileConfigPersistence(
48+
maybeReactDevToolsRuntimeSettingsModuleModule,
49+
);
50+
3951
// Install hook before React is loaded.
40-
initialize(hookSettings);
52+
initialize(
53+
hookSettings,
54+
reloadAndProfileConfigPersistence?.getReloadAndProfileConfig(),
55+
);
4156

4257
// This should be defined in DEV, otherwise error is expected.
4358
const fuseboxReactDevToolsDispatcher =
@@ -76,6 +91,9 @@ if (__DEV__) {
7691
nativeStyleEditorValidAttributes: Object.keys(ReactNativeStyleAttributes),
7792
resolveRNStyle,
7893
onSettingsUpdated: handleReactDevToolsSettingsUpdate,
94+
isReloadAndProfileSupported:
95+
maybeReactDevToolsRuntimeSettingsModuleModule != null,
96+
reloadAndProfileConfigPersistence,
7997
});
8098
}
8199

@@ -135,6 +153,9 @@ if (__DEV__) {
135153
),
136154
websocket: ws,
137155
onSettingsUpdated: handleReactDevToolsSettingsUpdate,
156+
isReloadAndProfileSupported:
157+
maybeReactDevToolsRuntimeSettingsModuleModule != null,
158+
reloadAndProfileConfigPersistence,
138159
});
139160
}
140161
}
@@ -166,3 +187,20 @@ if (__DEV__) {
166187
);
167188
connectToWSBasedReactDevToolsFrontend(); // Try connecting once on load
168189
}
190+
191+
function makeReloadAndProfileConfigPersistence(
192+
maybeModule: ?NativeReactDevToolsRuntimeSettingsModuleSpec,
193+
) {
194+
if (maybeModule == null) {
195+
return;
196+
}
197+
198+
return {
199+
setReloadAndProfileConfig(config: PartialReloadAndProfileConfig): void {
200+
maybeModule.setReloadAndProfileConfig(config);
201+
},
202+
getReloadAndProfileConfig() {
203+
return maybeModule.getReloadAndProfileConfig();
204+
},
205+
};
206+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
@interface RCTDevToolsRuntimeSettingsModule : NSObject
11+
12+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <FBReactNativeSpec/FBReactNativeSpec.h>
9+
#import <React/RCTDevToolsRuntimeSettingsModule.h>
10+
11+
#import "CoreModulesPlugins.h"
12+
13+
struct Config {
14+
bool shouldReloadAndProfile = false;
15+
bool recordChangeDescriptions = false;
16+
};
17+
18+
// static to persist across Turbo Module reloads
19+
static Config _config;
20+
21+
@interface RCTDevToolsRuntimeSettingsModule () <NativeReactDevToolsRuntimeSettingsModuleSpec> {
22+
}
23+
@end
24+
25+
@implementation RCTDevToolsRuntimeSettingsModule
26+
RCT_EXPORT_MODULE(ReactDevToolsRuntimeSettingsModule)
27+
28+
RCT_EXPORT_METHOD(setReloadAndProfileConfig
29+
: (JS::NativeReactDevToolsRuntimeSettingsModule::PartialReloadAndProfileConfig &)config)
30+
{
31+
if (config.shouldReloadAndProfile().has_value()) {
32+
_config.shouldReloadAndProfile = config.shouldReloadAndProfile().value();
33+
}
34+
if (config.recordChangeDescriptions().has_value()) {
35+
_config.recordChangeDescriptions = config.recordChangeDescriptions().value();
36+
}
37+
}
38+
39+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getReloadAndProfileConfig)
40+
{
41+
return @{
42+
@"shouldReloadAndProfile" : @(_config.shouldReloadAndProfile),
43+
@"recordChangeDescriptions" : @(_config.recordChangeDescriptions),
44+
};
45+
}
46+
47+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
48+
(const facebook::react::ObjCTurboModule::InitParams &)params
49+
{
50+
return std::make_shared<facebook::react::NativeReactDevToolsRuntimeSettingsModuleSpecJSI>(params);
51+
}
52+
53+
@end
54+
55+
Class RCTDevToolsRuntimeSettingsCls(void)
56+
{
57+
return RCTDevToolsRuntimeSettingsModule.class;
58+
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,12 @@ public final class com/facebook/react/modules/devloading/DevLoadingModule : com/
33733373
public final class com/facebook/react/modules/devloading/DevLoadingModule$Companion {
33743374
}
33753375

3376+
public final class com/facebook/react/modules/devtoolsruntimesettings/ReactDevToolsRuntimeSettingsModule : com/facebook/fbreact/specs/NativeReactDevToolsRuntimeSettingsModuleSpec {
3377+
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
3378+
public fun getReloadAndProfileConfig ()Lcom/facebook/react/bridge/WritableMap;
3379+
public fun setReloadAndProfileConfig (Lcom/facebook/react/bridge/ReadableMap;)V
3380+
}
3381+
33763382
public class com/facebook/react/modules/dialog/AlertFragment : androidx/fragment/app/DialogFragment, android/content/DialogInterface$OnClickListener {
33773383
public fun <init> ()V
33783384
public fun <init> (Lcom/facebook/react/modules/dialog/DialogModule$AlertFragmentListener;Landroid/os/Bundle;)V

packages/react-native/ReactAndroid/build.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ val preparePrefab by
106106
Pair("../ReactCommon/cxxreact/", "cxxreact/"),
107107
// react_featureflags
108108
Pair("../ReactCommon/react/featureflags/", "react/featureflags/"),
109+
// react_devtoolsruntimesettings
110+
Pair(
111+
"../ReactCommon/react/devtoolsruntimesettings/",
112+
"react/devtoolsruntimesettings/"),
109113
// react_render_animations
110114
Pair(
111115
"../ReactCommon/react/renderer/animations/",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.modules.devtoolsruntimesettings
9+
10+
import com.facebook.fbreact.specs.NativeReactDevToolsRuntimeSettingsModuleSpec
11+
import com.facebook.jni.annotations.DoNotStripAny
12+
import com.facebook.react.bridge.Arguments
13+
import com.facebook.react.bridge.ReactApplicationContext
14+
import com.facebook.react.bridge.ReadableMap
15+
import com.facebook.react.bridge.WritableMap
16+
import com.facebook.react.module.annotations.ReactModule
17+
18+
private class Settings {
19+
var shouldReloadAndProfile: Boolean = false
20+
var recordChangeDescriptions: Boolean = false
21+
}
22+
23+
@DoNotStripAny
24+
@ReactModule(name = NativeReactDevToolsRuntimeSettingsModuleSpec.NAME)
25+
public class ReactDevToolsRuntimeSettingsModule(reactContext: ReactApplicationContext?) :
26+
NativeReactDevToolsRuntimeSettingsModuleSpec(reactContext) {
27+
28+
// static to persist across Turbo Module reloads
29+
private companion object {
30+
private val settings = Settings()
31+
}
32+
33+
override fun setReloadAndProfileConfig(map: ReadableMap) {
34+
if (map.hasKey("shouldReloadAndProfile")) {
35+
settings.shouldReloadAndProfile = map.getBoolean("shouldReloadAndProfile")
36+
}
37+
if (map.hasKey("recordChangeDescriptions")) {
38+
settings.recordChangeDescriptions = map.getBoolean("recordChangeDescriptions")
39+
}
40+
}
41+
42+
override fun getReloadAndProfileConfig(): WritableMap {
43+
val map = Arguments.createMap()
44+
map.putBoolean("shouldReloadAndProfile", settings.shouldReloadAndProfile)
45+
map.putBoolean("recordChangeDescriptions", settings.recordChangeDescriptions)
46+
return map
47+
}
48+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.facebook.react.modules.camera.ImageStoreManager;
2929
import com.facebook.react.modules.clipboard.ClipboardModule;
3030
import com.facebook.react.modules.devloading.DevLoadingModule;
31+
import com.facebook.react.modules.devtoolsruntimesettings.ReactDevToolsRuntimeSettingsModule;
3132
import com.facebook.react.modules.dialog.DialogModule;
3233
import com.facebook.react.modules.fresco.FrescoModule;
3334
import com.facebook.react.modules.i18nmanager.I18nManagerModule;
@@ -88,6 +89,7 @@
8889
NetworkingModule.class,
8990
PermissionsModule.class,
9091
ReactDevToolsSettingsManagerModule.class,
92+
ReactDevToolsRuntimeSettingsModule.class,
9193
ShareModule.class,
9294
SoundManagerModule.class,
9395
StatusBarModule.class,
@@ -156,6 +158,8 @@ public MainReactPackage(MainPackageConfig config) {
156158
return new WebSocketModule(context);
157159
case ReactDevToolsSettingsManagerModule.NAME:
158160
return new ReactDevToolsSettingsManagerModule(context);
161+
case ReactDevToolsRuntimeSettingsModule.NAME:
162+
return new ReactDevToolsRuntimeSettingsModule(context);
159163
default:
160164
return null;
161165
}
@@ -302,6 +306,7 @@ private ReactModuleInfoProvider fallbackForMissingClass() {
302306
NetworkingModule.class,
303307
PermissionsModule.class,
304308
ReactDevToolsSettingsManagerModule.class,
309+
ReactDevToolsRuntimeSettingsModule.class,
305310
ShareModule.class,
306311
StatusBarModule.class,
307312
SoundManagerModule.class,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
add_compile_options(
10+
-fexceptions
11+
-frtti
12+
-std=c++20
13+
-Wall
14+
-Wpedantic)
15+
16+
add_library(react_devtoolsruntimesettingscxx INTERFACE)
17+
18+
target_include_directories(react_devtoolsruntimesettingscxx INTERFACE .)
19+
20+
target_link_libraries(react_devtoolsruntimesettingscxx
21+
jsi
22+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "DevToolsRuntimeSettings.h"
9+
10+
namespace facebook::react {
11+
12+
void DevToolsRuntimeSettings::setReloadAndProfileConfig(
13+
NativePartialReloadAndProfileConfig config) {
14+
if (config.shouldReloadAndProfile.has_value()) {
15+
_config.shouldReloadAndProfile = config.shouldReloadAndProfile.value();
16+
}
17+
if (config.shouldReloadAndProfile.has_value()) {
18+
_config.recordChangeDescriptions = config.recordChangeDescriptions.value();
19+
}
20+
};
21+
22+
NativeReloadAndProfileConfig
23+
DevToolsRuntimeSettings::getReloadAndProfileConfig() const {
24+
return _config;
25+
};
26+
27+
} // namespace facebook::react
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
11+
12+
namespace facebook::react {
13+
14+
using NativePartialReloadAndProfileConfig =
15+
NativeReactDevToolsRuntimeSettingsModulePartialReloadAndProfileConfig<
16+
std::optional<bool>,
17+
std::optional<bool>>;
18+
19+
template <>
20+
struct Bridging<NativePartialReloadAndProfileConfig>
21+
: NativeReactDevToolsRuntimeSettingsModulePartialReloadAndProfileConfigBridging<
22+
NativePartialReloadAndProfileConfig> {};
23+
24+
using NativeReloadAndProfileConfig =
25+
NativeReactDevToolsRuntimeSettingsModuleReloadAndProfileConfig<bool, bool>;
26+
27+
template <>
28+
struct Bridging<NativeReloadAndProfileConfig>
29+
: NativeReactDevToolsRuntimeSettingsModuleReloadAndProfileConfigBridging<
30+
NativeReloadAndProfileConfig> {};
31+
32+
class DevToolsRuntimeSettings {
33+
public:
34+
// static to persist across Turbo Module reloads
35+
static DevToolsRuntimeSettings& getInstance() {
36+
static DevToolsRuntimeSettings instance;
37+
return instance;
38+
}
39+
40+
private:
41+
NativeReloadAndProfileConfig _config;
42+
43+
DevToolsRuntimeSettings() : _config() {}
44+
45+
public:
46+
~DevToolsRuntimeSettings() = default;
47+
DevToolsRuntimeSettings(const DevToolsRuntimeSettings&) = delete;
48+
DevToolsRuntimeSettings(DevToolsRuntimeSettings&&) = delete;
49+
void operator=(const DevToolsRuntimeSettings&) = delete;
50+
51+
void setReloadAndProfileConfig(NativePartialReloadAndProfileConfig config);
52+
NativeReloadAndProfileConfig getReloadAndProfileConfig() const;
53+
};
54+
55+
} // namespace facebook::react

packages/react-native/ReactCommon/react/nativemodule/defaults/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_include_directories(react_nativemodule_defaults PUBLIC ${REACT_COMMON_DIR
2121

2222
target_link_libraries(react_nativemodule_defaults
2323
react_nativemodule_dom
24+
react_nativemodule_devtoolsruntimesettings
2425
react_nativemodule_featureflags
2526
react_nativemodule_microtasks
2627
react_nativemodule_idlecallbacks

packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <react/nativemodule/idlecallbacks/NativeIdleCallbacks.h>
1212
#include <react/nativemodule/microtasks/NativeMicrotasks.h>
1313

14+
#ifdef HERMES_ENABLE_DEBUGGER
15+
#include <react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.h>
16+
#endif
17+
1418
namespace facebook::react {
1519

1620
/* static */ std::shared_ptr<TurboModule> DefaultTurboModules::getTurboModule(
@@ -32,6 +36,12 @@ namespace facebook::react {
3236
return std::make_shared<NativeDOM>(jsInvoker);
3337
}
3438

39+
#ifdef HERMES_ENABLE_DEBUGGER
40+
if (name == DevToolsRuntimeSettingsModule::kModuleName) {
41+
return std::make_shared<DevToolsRuntimeSettingsModule>(jsInvoker);
42+
}
43+
#endif
44+
3545
return nullptr;
3646
}
3747

0 commit comments

Comments
 (0)