Skip to content

Commit

Permalink
Fusebox reload-to-profile
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[General][Added] - Add support for reload-to-profile in Fusebox.

CDT: facebookexperimental/rn-chrome-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
  • Loading branch information
EdmondChuiHW authored and facebook-github-bot committed Oct 7, 2024
1 parent cc6d1eb commit 39be9ec
Show file tree
Hide file tree
Showing 19 changed files with 416 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/react-native/Libraries/Core/setUpReactDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -36,8 +42,17 @@ if (__DEV__) {
);
}
}

const reloadAndProfileConfigPersistence =
makeReloadAndProfileConfigPersistence(
maybeReactDevToolsRuntimeSettingsModuleModule,
);

// Install hook before React is loaded.
initialize(hookSettings);
initialize(
hookSettings,
reloadAndProfileConfigPersistence?.getReloadAndProfileConfig(),
);

// This should be defined in DEV, otherwise error is expected.
const fuseboxReactDevToolsDispatcher =
Expand Down Expand Up @@ -76,6 +91,9 @@ if (__DEV__) {
nativeStyleEditorValidAttributes: Object.keys(ReactNativeStyleAttributes),
resolveRNStyle,
onSettingsUpdated: handleReactDevToolsSettingsUpdate,
isReloadAndProfileSupported:
maybeReactDevToolsRuntimeSettingsModuleModule != null,
reloadAndProfileConfigPersistence,
});
}

Expand Down Expand Up @@ -135,6 +153,9 @@ if (__DEV__) {
),
websocket: ws,
onSettingsUpdated: handleReactDevToolsSettingsUpdate,
isReloadAndProfileSupported:
maybeReactDevToolsRuntimeSettingsModuleModule != null,
reloadAndProfileConfigPersistence,
});
}
}
Expand Down Expand Up @@ -166,3 +187,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();
},
};
}
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.
*/

#import <Foundation/Foundation.h>

@interface RCTDevToolsRuntimeSettingsModule : NSObject

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

#import <FBReactNativeSpec/FBReactNativeSpec.h>
#import <React/RCTDevToolsRuntimeSettingsModule.h>

#import "CoreModulesPlugins.h"

struct Config {
bool shouldReloadAndProfile = false;
bool recordChangeDescriptions = false;
};

// static to persist across Turbo Module reloads
static Config _config;

@interface RCTDevToolsRuntimeSettingsModule () <NativeReactDevToolsRuntimeSettingsModuleSpec> {
}
@end

@implementation RCTDevToolsRuntimeSettingsModule
RCT_EXPORT_MODULE(ReactDevToolsRuntimeSettingsModule)

RCT_EXPORT_METHOD(setReloadAndProfileConfig
: (JS::NativeReactDevToolsRuntimeSettingsModule::PartialReloadAndProfileConfig &)config)
{
if (config.shouldReloadAndProfile().has_value()) {
_config.shouldReloadAndProfile = config.shouldReloadAndProfile().value();
}
if (config.recordChangeDescriptions().has_value()) {
_config.recordChangeDescriptions = config.recordChangeDescriptions().value();
}
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getReloadAndProfileConfig)
{
return @{
@"shouldReloadAndProfile" : @(_config.shouldReloadAndProfile),
@"recordChangeDescriptions" : @(_config.recordChangeDescriptions),
};
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeReactDevToolsRuntimeSettingsModuleSpecJSI>(params);
}

@end

Class RCTDevToolsRuntimeSettingsCls(void)
{
return RCTDevToolsRuntimeSettingsModule.class;
}
6 changes: 6 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,12 @@ public final class com/facebook/react/modules/devloading/DevLoadingModule : com/
public final class com/facebook/react/modules/devloading/DevLoadingModule$Companion {
}

public final class com/facebook/react/modules/devtoolsruntimesettings/ReactDevToolsRuntimeSettingsModule : com/facebook/fbreact/specs/NativeReactDevToolsRuntimeSettingsModuleSpec {
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun getReloadAndProfileConfig ()Lcom/facebook/react/bridge/WritableMap;
public fun setReloadAndProfileConfig (Lcom/facebook/react/bridge/ReadableMap;)V
}

public class com/facebook/react/modules/dialog/AlertFragment : androidx/fragment/app/DialogFragment, android/content/DialogInterface$OnClickListener {
public fun <init> ()V
public fun <init> (Lcom/facebook/react/modules/dialog/DialogModule$AlertFragmentListener;Landroid/os/Bundle;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.modules.devtoolsruntimesettings

import com.facebook.fbreact.specs.NativeReactDevToolsRuntimeSettingsModuleSpec
import com.facebook.jni.annotations.DoNotStripAny
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableMap
import com.facebook.react.module.annotations.ReactModule

private class Settings {
var shouldReloadAndProfile: Boolean = false
var recordChangeDescriptions: Boolean = false
}

@DoNotStripAny
@ReactModule(name = NativeReactDevToolsRuntimeSettingsModuleSpec.NAME)
public class ReactDevToolsRuntimeSettingsModule(reactContext: ReactApplicationContext?) :
NativeReactDevToolsRuntimeSettingsModuleSpec(reactContext) {

// static to persist across Turbo Module reloads
private companion object {
private val settings = Settings()
}

override fun setReloadAndProfileConfig(map: ReadableMap) {
if (map.hasKey("shouldReloadAndProfile")) {
settings.shouldReloadAndProfile = map.getBoolean("shouldReloadAndProfile")
}
if (map.hasKey("recordChangeDescriptions")) {
settings.recordChangeDescriptions = map.getBoolean("recordChangeDescriptions")
}
}

override fun getReloadAndProfileConfig(): WritableMap {
val map = Arguments.createMap()
map.putBoolean("shouldReloadAndProfile", settings.shouldReloadAndProfile)
map.putBoolean("recordChangeDescriptions", settings.recordChangeDescriptions)
return map
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.react.modules.camera.ImageStoreManager;
import com.facebook.react.modules.clipboard.ClipboardModule;
import com.facebook.react.modules.devloading.DevLoadingModule;
import com.facebook.react.modules.devtoolsruntimesettings.ReactDevToolsRuntimeSettingsModule;
import com.facebook.react.modules.dialog.DialogModule;
import com.facebook.react.modules.fresco.FrescoModule;
import com.facebook.react.modules.i18nmanager.I18nManagerModule;
Expand Down Expand Up @@ -88,6 +89,7 @@
NetworkingModule.class,
PermissionsModule.class,
ReactDevToolsSettingsManagerModule.class,
ReactDevToolsRuntimeSettingsModule.class,
ShareModule.class,
SoundManagerModule.class,
StatusBarModule.class,
Expand Down Expand Up @@ -156,6 +158,8 @@ public MainReactPackage(MainPackageConfig config) {
return new WebSocketModule(context);
case ReactDevToolsSettingsManagerModule.NAME:
return new ReactDevToolsSettingsManagerModule(context);
case ReactDevToolsRuntimeSettingsModule.NAME:
return new ReactDevToolsRuntimeSettingsModule(context);
default:
return null;
}
Expand Down Expand Up @@ -302,6 +306,7 @@ private ReactModuleInfoProvider fallbackForMissingClass() {
NetworkingModule.class,
PermissionsModule.class,
ReactDevToolsSettingsManagerModule.class,
ReactDevToolsRuntimeSettingsModule.class,
ShareModule.class,
StatusBarModule.class,
SoundManagerModule.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ add_library(reactnative
$<TARGET_OBJECTS:react_cxxreact>
$<TARGET_OBJECTS:react_debug>
$<TARGET_OBJECTS:react_devsupportjni>
$<TARGET_OBJECTS:react_devtoolsruntimesettingscxx>
$<TARGET_OBJECTS:react_featureflags>
$<TARGET_OBJECTS:react_featureflagsjni>
$<TARGET_OBJECTS:react_nativemodule_core>
$<TARGET_OBJECTS:react_nativemodule_defaults>
$<TARGET_OBJECTS:react_nativemodule_devtoolsruntimesettings>
$<TARGET_OBJECTS:react_nativemodule_dom>
$<TARGET_OBJECTS:react_nativemodule_featureflags>
$<TARGET_OBJECTS:react_nativemodule_idlecallbacks>
Expand Down Expand Up @@ -254,10 +256,12 @@ target_include_directories(reactnative
$<TARGET_PROPERTY:react_cxxreact,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_debug,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_devsupportjni,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_devtoolsruntimesettingscxx,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_featureflags,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_featureflagsjni,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_core,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_defaults,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_devtoolsruntimesettings,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_dom,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_featureflags,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:react_nativemodule_idlecallbacks,INTERFACE_INCLUDE_DIRECTORIES>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ target_link_libraries(
mapbufferjni
react_codegen_rncore
react_debug
react_devtoolsruntimesettingscxx
react_featureflags
react_render_animations
react_render_attributedstring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ target_link_libraries(react_newarchdefaults
react_codegen_rncore
react_cxxreactpackage
react_nativemodule_defaults
react_nativemodule_devtoolsruntimesettings
react_nativemodule_dom
react_nativemodule_featureflags
react_nativemodule_microtasks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(
-fexceptions
-frtti
-std=c++20
-Wall
-Wpedantic)

add_library(devtoolsruntimesettingscxx INTERFACE)

target_include_directories(devtoolsruntimesettingscxx INTERFACE .)

target_link_libraries(devtoolsruntimesettingscxx
jsi
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

#include "DevToolsRuntimeSettings.h"

namespace facebook::react {

void DevToolsRuntimeSettings::setReloadAndProfileConfig(
NativePartialReloadAndProfileConfig config) {
if (config.shouldReloadAndProfile.has_value()) {
_config.shouldReloadAndProfile = config.shouldReloadAndProfile.value();
}
if (config.shouldReloadAndProfile.has_value()) {
_config.recordChangeDescriptions = config.recordChangeDescriptions.value();
}
};

NativeReloadAndProfileConfig
DevToolsRuntimeSettings::getReloadAndProfileConfig() const {
return _config;
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.
*/

#pragma once

#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>

namespace facebook::react {

using NativePartialReloadAndProfileConfig =
NativeReactDevToolsRuntimeSettingsModulePartialReloadAndProfileConfig<
std::optional<bool>,
std::optional<bool>>;

template <>
struct Bridging<NativePartialReloadAndProfileConfig>
: NativeReactDevToolsRuntimeSettingsModulePartialReloadAndProfileConfigBridging<
NativePartialReloadAndProfileConfig> {};

using NativeReloadAndProfileConfig =
NativeReactDevToolsRuntimeSettingsModuleReloadAndProfileConfig<bool, bool>;

template <>
struct Bridging<NativeReloadAndProfileConfig>
: NativeReactDevToolsRuntimeSettingsModuleReloadAndProfileConfigBridging<
NativeReloadAndProfileConfig> {};

class DevToolsRuntimeSettings {
public:
// static to persist across Turbo Module reloads
static DevToolsRuntimeSettings& getInstance() {
static DevToolsRuntimeSettings instance;
return instance;
}

private:
NativeReloadAndProfileConfig _config;

DevToolsRuntimeSettings() : _config() {}

public:
DevToolsRuntimeSettings(const DevToolsRuntimeSettings&) = delete;
void operator=(const DevToolsRuntimeSettings&) = delete;

void setReloadAndProfileConfig(NativePartialReloadAndProfileConfig config);
NativeReloadAndProfileConfig getReloadAndProfileConfig() const;
};

} // namespace facebook::react
Loading

0 comments on commit 39be9ec

Please sign in to comment.