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
31 changes: 22 additions & 9 deletions packages/react-native/Libraries/Utilities/HMRClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,7 @@ Error: ${e.message}`;
const changeId = body?.changeId;
if (changeId != null && changeId !== lastMarkerChangeId) {
lastMarkerChangeId = changeId;
performance.mark('Fast Refresh - Update done', {
detail: {
devtools: {
dataType: 'marker',
color: 'primary',
tooltipText: 'Fast Refresh \u269b',
},
},
});
emitFastRefreshCompleteEvents();
}
}
});
Expand Down Expand Up @@ -393,4 +385,25 @@ function showCompileError() {
throw error;
}

function emitFastRefreshCompleteEvents() {
// Add marker entry in performance timeline
performance.mark('Fast Refresh - Update done', {
detail: {
devtools: {
dataType: 'marker',
color: 'primary',
tooltipText: 'Fast Refresh \u269b',
},
},
});

// Notify CDP clients via internal binding
if (
// $FlowFixMe[prop-missing] - Injected by RuntimeTarget
typeof globalThis.__notifyFastRefreshComplete === 'function'
) {
globalThis.__notifyFastRefreshComplete();
}
}

export default HMRClient;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "RuntimeAgent.h"
#include "SessionState.h"

#include <folly/dynamic.h>
#include <jsinspector-modern/cdp/CdpJson.h>

#include <chrono>
#include <utility>

namespace facebook::react::jsinspector_modern {
Expand Down Expand Up @@ -119,6 +123,21 @@ void RuntimeAgent::notifyBindingCalled(
"name", bindingName)("payload", payload)));
}

void RuntimeAgent::notifyFastRefreshComplete() {
if (!sessionState_.isReactNativeApplicationDomainEnabled) {
return;
}
folly::dynamic params = folly::dynamic::object(
"timestamp",
std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count());
frontendChannel_(
cdp::jsonNotification(
"ReactNativeApplication.unstable_fastRefreshComplete",
std::move(params)));
}

RuntimeAgent::ExportedState RuntimeAgent::getExportedState() {
return {
.delegateState = delegate_ ? delegate_->getExportedState() : nullptr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class RuntimeAgent final {

void notifyBindingCalled(const std::string &bindingName, const std::string &payload);

/**
* Called by RuntimeTarget when JS calls __notifyFastRefreshComplete().
* Emits a ReactNativeApplication.unstable_fastRefreshComplete CDP
* notification if the ReactNativeApplication domain is enabled.
*/
void notifyFastRefreshComplete();

struct ExportedState {
std::unique_ptr<RuntimeAgentDelegate::ExportedState> delegateState;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void RuntimeTarget::installGlobals() {
// NOTE: RuntimeTarget::installNetworkReporterAPI is in
// RuntimeTargetNetwork.cpp
installNetworkReporterAPI();

installFastRefreshHandler();
}

std::shared_ptr<RuntimeAgent> RuntimeTarget::createAgent(
Expand Down Expand Up @@ -128,6 +130,37 @@ void RuntimeTarget::installBindingHandler(const std::string& bindingName) {
});
}

void RuntimeTarget::installFastRefreshHandler() {
jsExecutor_([selfExecutor = executorFromThis()](jsi::Runtime& runtime) {
auto globalObj = runtime.global();
try {
auto name =
jsi::PropNameID::forUtf8(runtime, "__notifyFastRefreshComplete");
globalObj.setProperty(
runtime,
name,
jsi::Function::createFromHostFunction(
runtime,
name,
0,
[selfExecutor](
jsi::Runtime& /*rt*/,
const jsi::Value&,
const jsi::Value*,
size_t) -> jsi::Value {
selfExecutor([](auto& self) {
self.agents_.forEach(
[](auto& agent) { agent.notifyFastRefreshComplete(); });
});

return jsi::Value::undefined();
}));
} catch (jsi::JSError&) {
// Swallow JavaScript exceptions that occur while setting up the global.
}
});
}

void RuntimeTarget::emitDebuggerSessionCreated() {
jsExecutor_([selfExecutor = executorFromThis()](jsi::Runtime& runtime) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ class JSINSPECTOR_EXPORT RuntimeTarget : public EnableExecutorFromThis<RuntimeTa
*/
void installGlobals();

/**
* Installs __notifyFastRefreshComplete on the runtime's global object.
* When called from JS, dispatches to all connected RuntimeAgents.
*/
void installFastRefreshHandler();

/**
* Install the console API handler.
*/
Expand Down
Loading