Skip to content

Commit

Permalink
Fusebox reload-to-profile (Part 1 of 2: native) (#46856)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #46856

Changelog:
[General][Added] - Add support for reload-to-profile in Fusebox. (Part 1 of 2: native)

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++

Reviewed By: hoxyq

Differential Revision: D63233256

fbshipit-source-id: f2f2814491e785b03af143cc26639ef178881fff
  • Loading branch information
EdmondChuiHW authored and facebook-github-bot committed Oct 10, 2024
1 parent 77889af commit 91a40a2
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 0 deletions.
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
4 changes: 4 additions & 0 deletions packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ val preparePrefab by
Pair("../ReactCommon/cxxreact/", "cxxreact/"),
// react_featureflags
Pair("../ReactCommon/react/featureflags/", "react/featureflags/"),
// react_devtoolsruntimesettings
Pair(
"../ReactCommon/react/devtoolsruntimesettings/",
"react/devtoolsruntimesettings/"),
// react_render_animations
Pair(
"../ReactCommon/react/renderer/animations/",
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
@@ -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(react_devtoolsruntimesettingscxx INTERFACE)

target_include_directories(react_devtoolsruntimesettingscxx INTERFACE .)

target_link_libraries(react_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,56 @@
/*
* 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() = default;
DevToolsRuntimeSettings(const DevToolsRuntimeSettings&) = delete;
DevToolsRuntimeSettings(DevToolsRuntimeSettings&&) = delete;
void operator=(const DevToolsRuntimeSettings&) = delete;
void operator=(DevToolsRuntimeSettings&&) = delete;

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

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_include_directories(react_nativemodule_defaults PUBLIC ${REACT_COMMON_DIR

target_link_libraries(react_nativemodule_defaults
react_nativemodule_dom
react_nativemodule_devtoolsruntimesettings
react_nativemodule_featureflags
react_nativemodule_microtasks
react_nativemodule_idlecallbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <react/nativemodule/idlecallbacks/NativeIdleCallbacks.h>
#include <react/nativemodule/microtasks/NativeMicrotasks.h>

#ifdef HERMES_ENABLE_DEBUGGER
#include <react/nativemodule/devtoolsruntimesettings/DevToolsRuntimeSettingsModule.h>
#endif

namespace facebook::react {

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

#ifdef HERMES_ENABLE_DEBUGGER
if (name == DevToolsRuntimeSettingsModule::kModuleName) {
return std::make_shared<DevToolsRuntimeSettingsModule>(jsInvoker);
}
#endif

return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
-DLOG_TAG=\"ReactNative\")

file(GLOB react_nativemodule_devtoolsruntimesettings_SRC CONFIGURE_DEPENDS *.cpp)
add_library(react_nativemodule_devtoolsruntimesettings OBJECT ${react_nativemodule_devtoolsruntimesettings_SRC})

target_include_directories(react_nativemodule_devtoolsruntimesettings PUBLIC ${REACT_COMMON_DIR})

target_link_libraries(react_nativemodule_devtoolsruntimesettings
react_devtoolsruntimesettingscxx
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 "DevToolsRuntimeSettingsModule.h"
#include "Plugins.h"

std::shared_ptr<facebook::react::TurboModule>
ReactDevToolsRuntimeSettingsModuleProvider(
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
return std::make_shared<facebook::react::DevToolsRuntimeSettingsModule>(
std::move(jsInvoker));
}

namespace facebook::react {

DevToolsRuntimeSettingsModule::DevToolsRuntimeSettingsModule(
std::shared_ptr<CallInvoker> jsInvoker)
: NativeReactDevToolsRuntimeSettingsModuleCxxSpec(std::move(jsInvoker)) {}

void DevToolsRuntimeSettingsModule::setReloadAndProfileConfig(
jsi::Runtime& /*rt*/,
NativePartialReloadAndProfileConfig config) {
DevToolsRuntimeSettings::getInstance().setReloadAndProfileConfig(config);
};

NativeReloadAndProfileConfig
DevToolsRuntimeSettingsModule::getReloadAndProfileConfig(jsi::Runtime& /*rt*/) {
return DevToolsRuntimeSettings::getInstance().getReloadAndProfileConfig();
};

} // namespace facebook::react
Loading

0 comments on commit 91a40a2

Please sign in to comment.