Skip to content

Commit bb79f34

Browse files
committed
Wire HostTarget through to instance initialization
This change wires the HostTarget created in ReactNativeHost through to the instance initialization step for bridge and bridgeless architectures.
1 parent 268aaf7 commit bb79f34

File tree

8 files changed

+53
-2
lines changed

8 files changed

+53
-2
lines changed

vnext/Microsoft.ReactNative/ReactHost/React.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <unordered_set>
99
#include <vector>
1010

11+
#include "FuseboxHostTargetSettings.h"
1112
#include "JSBundle.h"
1213
#include "RedBoxHandler.h"
1314
#include "dispatchQueue/dispatchQueue.h"
@@ -343,6 +344,9 @@ struct ReactOptions {
343344
//! The callback is called when IReactInstance is destroyed and must not be used anymore.
344345
//! It is called from the native queue.
345346
OnReactInstanceDestroyedCallback OnInstanceDestroyed;
347+
348+
//! The HostTarget instance and its corresponding thread for callbacks
349+
Microsoft::ReactNative::FuseboxHostTargetSettings HostTargetSettings;
346350
};
347351

348352
//! IReactHost manages a ReactNative instance.

vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ ReactInstanceWin::ReactInstanceWin(
291291
}
292292

293293
ReactInstanceWin::~ReactInstanceWin() noexcept {
294+
#ifdef USE_FABRIC
295+
if (m_bridgelessReactInstance) {
296+
m_bridgelessReactInstance->unregisterFromInspector();
297+
}
298+
#endif
299+
294300
std::scoped_lock lock{s_registryMutex};
295301
auto it = std::find(s_instanceRegistry.begin(), s_instanceRegistry.end(), this);
296302
if (it != s_instanceRegistry.end()) {
@@ -527,6 +533,8 @@ std::shared_ptr<facebook::react::DevSettings> ReactInstanceWin::CreateDevSetting
527533

528534
devSettings->useRuntimeScheduler = useRuntimeScheduler;
529535

536+
devSettings->hostTargetSettings = m_options.HostTargetSettings;
537+
530538
return devSettings;
531539
}
532540

@@ -619,7 +627,11 @@ void ReactInstanceWin::InitializeBridgeless() noexcept {
619627
auto jsRuntime = std::make_unique<Microsoft::ReactNative::HermesJSRuntime>(m_jsiRuntimeHolder);
620628
jsRuntime->getRuntime();
621629
m_bridgelessReactInstance = std::make_unique<facebook::react::ReactInstance>(
622-
std::move(jsRuntime), m_jsMessageThread.Load(), timerManager, jsErrorHandlingFunc);
630+
std::move(jsRuntime),
631+
m_jsMessageThread.Load(),
632+
timerManager,
633+
jsErrorHandlingFunc,
634+
m_options.HostTargetSettings.Instance);
623635

624636
auto bufferedRuntimeExecutor = m_bridgelessReactInstance->getBufferedRuntimeExecutor();
625637
timerManager->setRuntimeExecutor(bufferedRuntimeExecutor);

vnext/Microsoft.ReactNative/ReactNativeHost.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ IAsyncAction ReactNativeHost::ReloadInstance() noexcept {
261261
}
262262

263263
reactOptions.Identity = jsBundleFile;
264+
reactOptions.HostTargetSettings = {m_inspectorThread, m_inspectorTarget.get()};
264265
return make<Mso::AsyncActionFutureAdapter>(m_reactHost->ReloadInstanceWithOptions(std::move(reactOptions)));
265266
}
266267

vnext/Shared/DevSettings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#pragma once
55

6+
#include "FuseboxHostTargetSettings.h"
67
#include "JSI/JSExecutorFactoryDelegate.h"
78
#include "Logging.h"
89

@@ -114,6 +115,9 @@ struct DevSettings {
114115

115116
// Enable concurrent mode by installing runtimeScheduler
116117
bool useRuntimeScheduler{false};
118+
119+
//! The HostTarget instance and its corresponding thread for callbacks
120+
Microsoft::ReactNative::FuseboxHostTargetSettings hostTargetSettings{};
117121
};
118122

119123
} // namespace react
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
namespace Mso {
7+
struct DispatchQueue;
8+
} // namespace Mso
9+
10+
namespace facebook::react::jsinspector_modern {
11+
class HostTarget;
12+
} // namespace facebook::react::jsinspector_modern
13+
14+
namespace Microsoft::ReactNative {
15+
16+
struct FuseboxHostTargetSettings {
17+
std::shared_ptr<Mso::DispatchQueue> InspectorThread;
18+
19+
// Do not use shared or weak pointers to ensure host has full control
20+
// over the lifecycle of the HostTarget.
21+
facebook::react::jsinspector_modern::HostTarget *Instance;
22+
};
23+
24+
} // namespace Microsoft::ReactNative

vnext/Shared/OInstance.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,8 @@ InstanceImpl::InstanceImpl(
485485
}
486486
}
487487

488-
m_innerInstance->initializeBridge(std::move(callback), jsef, m_jsThread, m_moduleRegistry);
488+
m_innerInstance->initializeBridge(
489+
std::move(callback), jsef, m_jsThread, m_moduleRegistry, m_devSettings->hostTargetSettings.Instance);
489490

490491
// For RuntimeScheduler to work properly, we need to install TurboModuleManager with RuntimeSchedulerCallbackInvoker.
491492
// To be able to do that, we need to be able to call m_innerInstance->getRuntimeExecutor(), which we can only do after
@@ -588,6 +589,7 @@ void InstanceImpl::loadBundleInternal(std::string &&jsBundleRelativePath, bool s
588589
}
589590

590591
InstanceImpl::~InstanceImpl() {
592+
m_innerInstance->unregisterFromInspector();
591593
if (shouldStartHermesInspector(*m_devSettings) && m_devSettings->jsiRuntimeHolder) {
592594
m_devSettings->jsiRuntimeHolder->teardown();
593595
}

vnext/Shared/Shared.vcxitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@
412412
<ClInclude Include="$(MSBuildThisFileDirectory)CxxMessageQueue.h" />
413413
<ClInclude Include="$(MSBuildThisFileDirectory)DevServerHelper.h" />
414414
<ClInclude Include="$(MSBuildThisFileDirectory)DevSettings.h" />
415+
<ClInclude Include="$(MSBuildThisFileDirectory)FuseboxHostTargetSettings.h" />
415416
<ClInclude Include="$(MSBuildThisFileDirectory)Executors\WebSocketJSExecutor.h" />
416417
<ClInclude Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.h" />
417418
<ClInclude Include="$(MSBuildThisFileDirectory)InspectorPackagerConnection.h" />

vnext/Shared/Shared.vcxitems.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@
507507
<ClInclude Include="$(MSBuildThisFileDirectory)DevSettings.h">
508508
<Filter>Header Files</Filter>
509509
</ClInclude>
510+
<ClInclude Include="$(MSBuildThisFileDirectory)FuseboxHostTargetSettings.h">
511+
<Filter>Header Files</Filter>
512+
</ClInclude>
510513
<ClInclude Include="$(MSBuildThisFileDirectory)IDevSupportManager.h">
511514
<Filter>Header Files</Filter>
512515
</ClInclude>

0 commit comments

Comments
 (0)