-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReactInstanceWin.cpp
693 lines (583 loc) · 27.3 KB
/
ReactInstanceWin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "ReactInstanceWin.h"
#include "MoveOnCopy.h"
#include "MsoUtils.h"
#include <Base/CoreNativeModules.h>
#include <ReactUWP/CreateUwpModules.h>
#include <ReactUWP/Modules/I18nModule.h>
#include <Threading/MessageDispatchQueue.h>
#include "ReactErrorProvider.h"
#include "Microsoft.ReactNative/Threading/MessageQueueThreadFactory.h"
#include "../../codegen/NativeClipboardSpec.g.h"
#include "NativeModules.h"
#include "NativeModulesProvider.h"
#include "Unicode.h"
#include <ReactWindowsCore/ViewManager.h>
#include <dispatchQueue/dispatchQueue.h>
#include "Modules/AppStateData.h"
#include "Modules/ClipboardModule.h"
#include "Modules/DevSettingsModule.h"
#include <Utils/UwpPreparedScriptStore.h>
#include <Utils/UwpScriptStore.h>
#if defined(USE_HERMES)
#include "HermesRuntimeHolder.h"
#endif // USE_HERMES
#if defined(USE_V8)
#include <winrt/Windows.Storage.h>
#include "BaseScriptStoreImpl.h"
#include "V8JSIRuntimeHolder.h"
#endif // USE_V8
#include "RedBox.h"
#include <tuple>
#include "ChakraRuntimeHolder.h"
namespace react::uwp {
void AddStandardViewManagers(
std::vector<std::unique_ptr<facebook::react::IViewManager>> &viewManagers,
std::shared_ptr<IReactInstance> const &instance) noexcept;
void AddPolyesterViewManagers(
std::vector<std::unique_ptr<facebook::react::IViewManager>> &viewManagers,
std::shared_ptr<IReactInstance> const &instance) noexcept;
std::shared_ptr<facebook::react::IUIManager> CreateUIManager2(
std::vector<react::uwp::NativeViewManager> &&viewManagers) noexcept;
} // namespace react::uwp
namespace Mso::React {
//=============================================================================================
// ReactContext implementation
//=============================================================================================
ReactContext::ReactContext(
Mso::WeakPtr<ReactInstanceWin> &&reactInstance,
winrt::Microsoft::ReactNative::IReactPropertyBag const &properties) noexcept
: m_reactInstance{std::move(reactInstance)}, m_properties{properties} {}
winrt::Microsoft::ReactNative::IReactPropertyBag ReactContext::Properties() noexcept {
return m_properties;
}
void ReactContext::CallJSFunction(std::string &&module, std::string &&method, folly::dynamic &¶ms) noexcept {
if (auto instance = m_reactInstance.GetStrongPtr()) {
if (instance->State() == ReactInstanceState::Loaded) {
if (auto fbInstance = instance->GetInnerInstance()) {
fbInstance->callJSFunction(std::move(module), std::move(method), std::move(params));
}
}
}
}
void ReactContext::DispatchEvent(int64_t viewTag, std::string &&eventName, folly::dynamic &&eventData) noexcept {
if (auto instance = m_reactInstance.GetStrongPtr()) {
if (instance->State() == ReactInstanceState::Loaded) {
instance->DispatchEvent(viewTag, std::move(eventName), std::move(eventData));
}
}
}
//=============================================================================================
// LoadedCallbackGuard ensures that the OnReactInstanceLoaded is always called.
// It calls OnReactInstanceLoaded in destructor with a cancellation error.
// If loading was previously succeeded this call with an error code is ignored.
//=============================================================================================
struct LoadedCallbackGuard {
LoadedCallbackGuard(ReactInstanceWin &reactInstance) noexcept : m_reactInstance{&reactInstance} {}
LoadedCallbackGuard(const LoadedCallbackGuard &other) = delete;
LoadedCallbackGuard &operator=(const LoadedCallbackGuard &other) = delete;
LoadedCallbackGuard(LoadedCallbackGuard &&other) = default;
LoadedCallbackGuard &operator=(LoadedCallbackGuard &&other) = default;
~LoadedCallbackGuard() noexcept {
if (m_reactInstance) {
m_reactInstance->OnReactInstanceLoaded(Mso::CancellationErrorProvider().MakeErrorCode(true));
}
}
private:
Mso::CntPtr<ReactInstanceWin> m_reactInstance;
};
//=============================================================================================
// ReactInstanceWin implementation
//=============================================================================================
ReactInstanceWin::ReactInstanceWin(
IReactHost &reactHost,
ReactOptions const &options,
Mso::Promise<void> &&whenCreated,
Mso::Promise<void> &&whenLoaded,
Mso::VoidFunctor &&updateUI) noexcept
: Super{reactHost.NativeQueue()},
m_weakReactHost{&reactHost},
m_options{options},
m_whenCreated{std::move(whenCreated)},
m_whenLoaded{std::move(whenLoaded)},
m_updateUI{std::move(updateUI)},
m_reactContext{Mso::Make<ReactContext>(this, options.Properties)},
m_legacyInstance{std::make_shared<react::uwp::UwpReactInstanceProxy>(
Mso::WeakPtr<Mso::React::IReactInstance>{this},
Mso::Copy(options.LegacySettings))} {
m_whenCreated.SetValue();
}
ReactInstanceWin::~ReactInstanceWin() noexcept {}
//! Initialize() is called from the native queue.
void ReactInstanceWin::Initialize() noexcept {
InitJSMessageThread();
InitNativeMessageThread();
InitUIMessageThread();
m_legacyReactInstance =
std::make_shared<react::uwp::UwpReactInstanceProxy>(this, Mso::Copy(m_options.LegacySettings));
// InitUIManager uses m_legacyReactInstance
InitUIManager();
Mso::PostFuture(
Mso::DispatchQueue::MainUIQueue(),
[weakThis = Mso::WeakPtr{this}]() noexcept {
// Objects that must be created on the UI thread
if (auto strongThis = weakThis.GetStrongPtr()) {
auto const &legacyInstance = strongThis->m_legacyReactInstance;
strongThis->m_deviceInfo = std::make_shared<react::uwp::DeviceInfo>(legacyInstance);
strongThis->m_appTheme =
std::make_shared<react::uwp::AppTheme>(legacyInstance, strongThis->m_uiMessageThread.LoadWithLock());
react::uwp::I18nHelper().Instance().setInfo(react::uwp::I18nModule::GetI18nInfo());
strongThis->m_appearanceListener = Mso::Make<react::uwp::AppearanceChangeListener>(legacyInstance);
}
})
.Then(Queue(), [ this, weakThis = Mso::WeakPtr{this} ]() noexcept {
if (auto strongThis = weakThis.GetStrongPtr()) {
// auto cxxModulesProviders = GetCxxModuleProviders();
auto devSettings = std::make_shared<facebook::react::DevSettings>();
devSettings->useJITCompilation = m_options.EnableJITCompilation;
devSettings->debugHost = GetDebugHost();
devSettings->debugBundlePath = m_options.DeveloperSettings.SourceBundleName;
devSettings->liveReloadCallback = GetLiveReloadCallback();
devSettings->errorCallback = GetErrorCallback();
devSettings->loggingCallback = GetLoggingCallback();
m_redboxHandler = devSettings->redboxHandler = std::move(GetRedBoxHandler());
devSettings->useDirectDebugger = m_options.DeveloperSettings.UseDirectDebugger;
devSettings->debuggerBreakOnNextLine = m_options.DeveloperSettings.DebuggerBreakOnNextLine;
devSettings->debuggerPort = m_options.DeveloperSettings.DebuggerPort;
devSettings->debuggerRuntimeName = m_options.DeveloperSettings.DebuggerRuntimeName;
devSettings->useWebDebugger = m_options.DeveloperSettings.UseWebDebugger;
devSettings->useFastRefresh = m_options.DeveloperSettings.UseFastRefresh;
// devSettings->memoryTracker = GetMemoryTracker();
devSettings->bundleRootPath =
m_options.BundleRootPath.empty() ? "ms-appx:///Bundle/" : m_options.BundleRootPath;
m_bundleRootPath = devSettings->bundleRootPath;
devSettings->waitingForDebuggerCallback = GetWaitingForDebuggerCallback();
devSettings->debuggerAttachCallback = GetDebuggerAttachCallback();
// Now that ReactNativeWindows is building outside devmain, it is missing
// fix given by PR https://github.com/microsoft/react-native-windows/pull/2624 causing
// regression. We're turning off console redirection till the fix is available in devmain.
// Bug https://office.visualstudio.com/DefaultCollection/OC/_workitems/edit/3441551 is tracking this
devSettings->debuggerConsoleRedirection =
false; // JSHost::ChangeGate::ChakraCoreDebuggerConsoleRedirection();
m_appState = std::make_shared<react::uwp::AppState2>(*m_reactContext);
// Acquire default modules and then populate with custom modules
std::vector<facebook::react::NativeModuleDescription> cxxModules = react::uwp::GetCoreModules(
m_uiManager.Load(),
m_batchingUIThread,
m_uiMessageThread.Load(),
std::move(m_deviceInfo),
std::move(m_appState),
std::move(m_appTheme),
std::move(m_appearanceListener),
m_legacyReactInstance);
auto nmp = std::make_shared<winrt::Microsoft::ReactNative::NativeModulesProvider>();
nmp->AddModuleProvider(
L"Clipboard",
winrt::Microsoft::ReactNative::MakeTurboModuleProvider<
::Microsoft::ReactNative::Clipboard,
::Microsoft::ReactNativeSpecs::ClipboardSpec>());
auto modules = nmp->GetModules(m_reactContext, m_batchingUIThread);
cxxModules.insert(
cxxModules.end(), std::make_move_iterator(modules.begin()), std::make_move_iterator(modules.end()));
cxxModules.emplace_back(
Microsoft::ReactNative::DevSettingsModule::name,
[weakReactHost = strongThis->m_weakReactHost]() {
return std::make_unique<Microsoft::ReactNative::DevSettingsModule>([weakReactHost]() noexcept {
if (auto reactHost = weakReactHost.GetStrongPtr()) {
reactHost->ReloadInstance();
}
});
},
m_batchingUIThread);
if (m_options.ModuleProvider != nullptr) {
std::vector<facebook::react::NativeModuleDescription> customCxxModules =
m_options.ModuleProvider->GetModules(m_reactContext, m_batchingUIThread);
cxxModules.insert(std::end(cxxModules), std::begin(customCxxModules), std::end(customCxxModules));
}
if (m_options.LegacySettings.UseJsi) {
std::unique_ptr<facebook::jsi::ScriptStore> scriptStore = nullptr;
std::unique_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore = nullptr;
switch (m_options.LegacySettings.jsiEngine) {
case react::uwp::JSIEngine::Hermes:
#if defined(USE_HERMES)
devSettings->jsiRuntimeHolder = std::make_shared<facebook::react::HermesRuntimeHolder>();
break;
#endif
case react::uwp::JSIEngine::V8:
#if defined(USE_V8)
preparedScriptStore =
std::make_unique<facebook::react::BasePreparedScriptStoreImpl>(getApplicationLocalFolder());
devSettings->jsiRuntimeHolder = std::make_shared<facebook::react::V8JSIRuntimeHolder>(
devSettings, m_jsMessageThread.Load(), std::move(scriptStore), std::move(preparedScriptStore));
break;
#endif
case react::uwp::JSIEngine::Chakra:
if (m_options.LegacySettings.EnableByteCodeCaching ||
!m_options.LegacySettings.ByteCodeFileUri.empty()) {
scriptStore = std::make_unique<react::uwp::UwpScriptStore>();
preparedScriptStore = std::make_unique<react::uwp::UwpPreparedScriptStore>(
winrt::to_hstring(m_options.LegacySettings.ByteCodeFileUri));
}
devSettings->jsiRuntimeHolder = std::make_shared<Microsoft::JSI::ChakraRuntimeHolder>(
devSettings, m_jsMessageThread.Load(), std::move(scriptStore), std::move(preparedScriptStore));
break;
}
}
try {
// We need to keep the instance wrapper alive as its destruction shuts down the native queue.
auto instanceWrapper = facebook::react::CreateReactInstance(
std::string(), // bundleRootPath
std::move(cxxModules),
nullptr,
m_uiManager.Load(),
m_jsMessageThread.Load(),
Mso::Copy(m_batchingUIThread),
std::move(devSettings));
m_instance.Exchange(Mso::Copy(instanceWrapper->GetInstance()));
m_instanceWrapper.Exchange(std::move(instanceWrapper));
if (auto onCreated = m_options.OnInstanceCreated.Get()) {
onCreated->Invoke(*this);
}
LoadJSBundles();
if (m_options.DeveloperSettings.IsDevModeEnabled && State() != ReactInstanceState::HasError) {
folly::dynamic params = folly::dynamic::array(
STRING(RN_PLATFORM),
m_options.DeveloperSettings.SourceBundleName.empty() ? m_options.Identity
: m_options.DeveloperSettings.SourceBundleName,
GetSourceBundleHost(),
GetSourceBundlePort(),
m_options.DeveloperSettings.UseFastRefresh);
m_instance.Load()->callJSFunction("HMRClient", "setup", std::move(params));
}
} catch (std::exception &e) {
OnErrorWithMessage(e.what());
OnErrorWithMessage("UwpReactInstance: Failed to create React Instance.");
} catch (winrt::hresult_error const &e) {
OnErrorWithMessage(Microsoft::Common::Unicode::Utf16ToUtf8(e.message().c_str(), e.message().size()));
OnErrorWithMessage("UwpReactInstance: Failed to create React Instance.");
} catch (...) {
OnErrorWithMessage("UwpReactInstance: Failed to create React Instance.");
}
}
});
}
void ReactInstanceWin::LoadJSBundles() noexcept {
//
// We use m_jsMessageThread to load JS bundles synchronously. In that case we only load
// them if the m_jsMessageThread is not shut down (quitSynchronous() is not called).
// After the load we call OnReactInstanceLoaded callback on native queue.
//
// Note that the instance could be destroyed while we are loading JS Bundles.
// Though, the JS engine is not destroyed until this work item is not finished.
// Thus, we check the m_isDestroyed flag to see if we should do an early exit.
// Also, since we have to guarantee that the OnReactInstanceLoaded callback is called before
// the OnReactInstanceDestroyed callback, the OnReactInstanceLoaded is called right before the
// OnReactInstanceDestroyed callback in the Destroy() method. In that case any OnReactInstanceLoaded
// calls after we finish this JS message queue work item is ignored.
//
// The LoadedCallbackGuard is used for the case when runOnQueue does not execute the lambda
// before destroying it. It may happen if the m_jsMessageThread is already shutdown.
// In that case, the LoadedCallbackGuard notifies about cancellation by calling OnReactInstanceLoaded.
// The OnReactInstanceLoaded internally only accepts the first call and ignores others.
//
if (m_options.DeveloperSettings.UseWebDebugger || m_options.DeveloperSettings.UseFastRefresh) {
// Getting bundle from the packager, so do everything async.
auto instanceWrapper = m_instanceWrapper.LoadWithLock();
instanceWrapper->loadBundle(Mso::Copy(m_options.Identity));
m_jsMessageThread.Load()->runOnQueue([
weakThis = Mso::WeakPtr{this},
loadCallbackGuard = Mso::MakeMoveOnCopyWrapper(LoadedCallbackGuard{*this})
]() noexcept {
if (auto strongThis = weakThis.GetStrongPtr()) {
if (strongThis->State() != ReactInstanceState::HasError) {
strongThis->OnReactInstanceLoaded(Mso::ErrorCode{});
}
}
});
} else {
m_jsMessageThread.Load()->runOnQueue([
weakThis = Mso::WeakPtr{this},
loadCallbackGuard = Mso::MakeMoveOnCopyWrapper(LoadedCallbackGuard{*this})
]() noexcept {
if (auto strongThis = weakThis.GetStrongPtr()) {
auto instance = strongThis->m_instance.LoadWithLock();
auto instanceWrapper = strongThis->m_instanceWrapper.LoadWithLock();
if (!instance || !instanceWrapper) {
return;
}
auto &options = strongThis->m_options;
try {
instanceWrapper->loadBundleSync(Mso::Copy(options.Identity));
} catch (...) {
strongThis->m_state = ReactInstanceState::HasError;
strongThis->OnReactInstanceLoaded(Mso::ExceptionErrorProvider().MakeErrorCode(std::current_exception()));
return;
}
// All JS bundles successfully loaded.
strongThis->OnReactInstanceLoaded(Mso::ErrorCode{});
}
});
}
}
void ReactInstanceWin::OnReactInstanceLoaded(const Mso::ErrorCode &errorCode) noexcept {
if (!m_isLoaded) {
Queue().InvokeElsePost([ weakThis = Mso::WeakPtr{this}, errorCode ]() noexcept {
if (auto strongThis = weakThis.GetStrongPtr()) {
if (!strongThis->m_isLoaded) {
strongThis->m_isLoaded = true;
strongThis->m_state = ReactInstanceState::Loaded;
if (auto onLoaded = strongThis->m_options.OnInstanceLoaded.Get()) {
onLoaded->Invoke(*strongThis, errorCode);
}
strongThis->m_whenLoaded.SetValue();
}
}
});
}
}
Mso::Future<void> ReactInstanceWin::Destroy() noexcept {
// This method must be called from the native queue.
VerifyIsInQueueElseCrash();
if (m_isDestroyed) {
return m_whenDestroyed.AsFuture();
}
m_isDestroyed = true;
if (!m_isLoaded) {
OnReactInstanceLoaded(Mso::CancellationErrorProvider().MakeErrorCode(true));
}
if (auto onDestroyed = m_options.OnInstanceDestroyed.Get()) {
onDestroyed->Invoke(*this);
}
// Make sure that the instance is not destroyed yet
if (auto instance = m_instance.Exchange(nullptr)) {
// Release the message queues before the ui manager and instance.
m_nativeMessageThread.Exchange(nullptr);
m_jsMessageThread.Exchange(nullptr);
m_uiManager.Exchange(nullptr);
m_instanceWrapper.Exchange(nullptr);
m_jsDispatchQueue.Exchange(nullptr);
}
return m_whenDestroyed.AsFuture();
}
const ReactOptions &ReactInstanceWin::Options() const noexcept {
return m_options;
}
ReactInstanceState ReactInstanceWin::State() const noexcept {
return m_state;
}
void ReactInstanceWin::InitJSMessageThread() noexcept {
auto jsDispatchQueue = Mso::DispatchQueue::MakeLooperQueue();
// Create MessageQueueThread for the DispatchQueue
VerifyElseCrashSz(jsDispatchQueue, "m_jsDispatchQueue must not be null");
m_jsMessageThread.Exchange(std::make_shared<MessageDispatchQueue>(
jsDispatchQueue, Mso::MakeWeakMemberFunctor(this, &ReactInstanceWin::OnError), Mso::Copy(m_whenDestroyed)));
m_jsDispatchQueue.Exchange(std::move(jsDispatchQueue));
}
void ReactInstanceWin::InitNativeMessageThread() noexcept {
// Native queue was already given us in constructor.
m_nativeMessageThread.Exchange(
std::make_shared<MessageDispatchQueue>(Queue(), Mso::MakeWeakMemberFunctor(this, &ReactInstanceWin::OnError)));
}
void ReactInstanceWin::InitUIMessageThread() noexcept {
// Native queue was already given us in constructor.
m_uiMessageThread.Exchange(std::make_shared<MessageDispatchQueue>(
Mso::DispatchQueue::MainUIQueue(), Mso::MakeWeakMemberFunctor(this, &ReactInstanceWin::OnError)));
m_batchingUIThread = react::uwp::MakeBatchingQueueThread(m_uiMessageThread.Load());
}
void ReactInstanceWin::InitUIManager() noexcept {
std::vector<std::unique_ptr<facebook::react::IViewManager>> viewManagers;
// Custom view managers
if (m_options.ViewManagerProvider) {
viewManagers = m_options.ViewManagerProvider->GetViewManagers(m_reactContext, m_legacyReactInstance);
}
react::uwp::AddStandardViewManagers(viewManagers, m_legacyReactInstance);
react::uwp::AddPolyesterViewManagers(viewManagers, m_legacyReactInstance);
auto uiManager = react::uwp::CreateUIManager2(std::move(viewManagers));
m_uiManager.Exchange(std::move(uiManager));
}
facebook::react::NativeLoggingHook ReactInstanceWin::GetLoggingCallback() noexcept {
if (m_options.OnLogging) {
return [logCallback = m_options.OnLogging](facebook::react::RCTLogLevel logLevel, const char *message) {
logCallback(static_cast<LogLevel>(logLevel), message);
};
} else {
// When no logging callback was specified, use a default one in DEBUG builds
#if DEBUG
return [telemetryTag{m_options.Identity}](facebook::react::RCTLogLevel logLevel, const char *message) {
std::ostringstream ss;
ss << "ReactNative ['" << telemetryTag << "'] (";
switch (logLevel) {
case facebook::react::RCTLogLevel::Trace:
ss << "trace";
break;
case facebook::react::RCTLogLevel::Info:
ss << "info";
break;
case facebook::react::RCTLogLevel::Warning:
ss << "warning";
break;
case facebook::react::RCTLogLevel::Error:
ss << "error";
break;
case facebook::react::RCTLogLevel::Fatal:
ss << "fatal";
break;
}
ss << "): '" << message << "'\n";
OutputDebugStringA(ss.str().c_str());
};
#else
return facebook::react::NativeLoggingHook{};
#endif
}
}
std::shared_ptr<IRedBoxHandler> ReactInstanceWin::GetRedBoxHandler() noexcept {
if (m_options.RedBoxHandler) {
return m_options.RedBoxHandler;
} else if (m_options.DeveloperSettings.IsDevModeEnabled) {
auto localWkReactHost = m_weakReactHost;
return CreateDefaultRedBoxHandler(std::move(localWkReactHost));
} else {
return {};
}
}
std::function<void()> ReactInstanceWin::GetLiveReloadCallback() noexcept {
// Live reload is enabled if we provide a callback function.
if (m_options.DeveloperSettings.UseLiveReload || m_options.DeveloperSettings.UseFastRefresh) {
return Mso::MakeWeakMemberStdFunction(this, &ReactInstanceWin::OnLiveReload);
}
return std::function<void()>{};
}
std::string ReactInstanceWin::GetSourceBundleHost() noexcept {
const ReactDevOptions &devOptions = m_options.DeveloperSettings;
return !devOptions.SourceBundleHost.empty() ? devOptions.SourceBundleHost : "localhost";
}
std::string ReactInstanceWin::GetSourceBundlePort() noexcept {
const ReactDevOptions &devOptions = m_options.DeveloperSettings;
return !devOptions.SourceBundlePort.empty() ? devOptions.SourceBundlePort : "8081";
}
std::string ReactInstanceWin::GetDebugHost() noexcept {
std::string debugHost;
const ReactDevOptions &devOptions = m_options.DeveloperSettings;
if (!devOptions.DebugHost.empty()) {
debugHost = devOptions.DebugHost;
} else {
debugHost = GetSourceBundleHost();
debugHost.append(":");
debugHost.append(GetSourceBundlePort());
}
return debugHost;
}
std::string ReactInstanceWin::GetBytecodeFileName() noexcept {
// use bytecode caching if enabled and not debugging
// (ChakraCore debugging does not work when bytecode caching is enabled)
// TODO: implement
// bool useByteCode = Mso::React::BytecodeOptimizationEnabled() && !m_options.DeveloperSettings.UseDirectDebugger;
// return useByteCode ? Mso::React::GetBytecodeFilePath(m_options.Identity) : "";
return "";
}
std::function<void(std::string)> ReactInstanceWin::GetErrorCallback() noexcept {
return Mso::MakeWeakMemberStdFunction(this, &ReactInstanceWin::OnErrorWithMessage);
}
void ReactInstanceWin::OnErrorWithMessage(const std::string &errorMessage) noexcept {
m_state = ReactInstanceState::HasError;
if (m_redboxHandler && m_redboxHandler->isDevSupportEnabled()) {
ErrorInfo errorInfo;
errorInfo.Message = errorMessage;
errorInfo.Id = 0;
m_redboxHandler->showNewError(std::move(errorInfo), ErrorType::Native);
}
OnError(Mso::React::ReactErrorProvider().MakeErrorCode(Mso::React::ReactError{errorMessage.c_str()}));
m_updateUI();
}
void ReactInstanceWin::OnError(const Mso::ErrorCode &errorCode) noexcept {
InvokeInQueue([ this, errorCode ]() noexcept { m_options.OnError(errorCode); });
}
void ReactInstanceWin::OnLiveReload() noexcept {
if (auto reactHost = m_weakReactHost.GetStrongPtr()) {
reactHost->ReloadInstance();
}
}
std::function<void()> ReactInstanceWin::GetWaitingForDebuggerCallback() noexcept {
if (m_options.DeveloperSettings.UseWebDebugger) {
return Mso::MakeWeakMemberStdFunction(this, &ReactInstanceWin::OnWaitingForDebugger);
}
return {};
}
void ReactInstanceWin::OnWaitingForDebugger() noexcept {
auto state = m_state.load();
while (state == ReactInstanceState::Loading) {
if (m_state.compare_exchange_weak(state, ReactInstanceState::WaitingForDebugger)) {
break;
}
}
m_updateUI();
}
std::function<void()> ReactInstanceWin::GetDebuggerAttachCallback() noexcept {
if (m_options.DeveloperSettings.UseWebDebugger) {
return Mso::MakeWeakMemberStdFunction(this, &ReactInstanceWin::OnDebuggerAttach);
}
return {};
}
void ReactInstanceWin::OnDebuggerAttach() noexcept {
m_updateUI();
}
void ReactInstanceWin::CallJsFunction(
std::string &&moduleName,
std::string &&method,
folly::dynamic &¶ms) noexcept {
// callJSFunction can be called from any thread. The native bridge will post the call to the right queue internally.
if (m_state == ReactInstanceState::Loaded) {
if (auto instance = m_instance.LoadWithLock()) {
instance->callJSFunction(std::move(moduleName), std::move(method), std::move(params));
}
}
}
void ReactInstanceWin::DispatchEvent(int64_t viewTag, std::string &&eventName, folly::dynamic &&eventData) noexcept {
if (m_state == ReactInstanceState::Loaded) {
if (auto instance = m_instanceWrapper.LoadWithLock()) {
instance->DispatchEvent(viewTag, eventName, std::move(eventData));
}
}
}
facebook::react::INativeUIManager *ReactInstanceWin::NativeUIManager() noexcept {
return m_uiManager.LoadWithLock()->getNativeUIManager();
}
std::shared_ptr<facebook::react::Instance> ReactInstanceWin::GetInnerInstance() noexcept {
return m_instance.LoadWithLock();
}
std::string ReactInstanceWin::GetBundleRootPath() noexcept {
return m_bundleRootPath.empty() ? m_options.LegacySettings.BundleRootPath : m_bundleRootPath;
}
std::shared_ptr<react::uwp::IReactInstance> ReactInstanceWin::UwpReactInstance() noexcept {
return m_legacyInstance;
}
void ReactInstanceWin::AttachMeasuredRootView(
facebook::react::IReactRootView *rootView,
folly::dynamic &&initialProps) noexcept {
if (auto instanceWrapper = m_instanceWrapper.LoadWithLock()) {
instanceWrapper->AttachMeasuredRootView(rootView, std::move(initialProps));
}
}
void ReactInstanceWin::DetachRootView(facebook::react::IReactRootView *rootView) noexcept {
if (auto instanceWrapper = m_instanceWrapper.LoadWithLock()) {
instanceWrapper->DetachRootView(rootView);
}
}
Mso::CntPtr<IReactInstanceInternal> MakeReactInstance(
IReactHost &reactHost,
ReactOptions &&options,
Mso::Promise<void> &&whenCreated,
Mso::Promise<void> &&whenLoaded,
Mso::VoidFunctor &&updateUI) noexcept {
return Mso::Make<ReactInstanceWin, IReactInstanceInternal>(
reactHost, std::move(options), std::move(whenCreated), std::move(whenLoaded), std::move(updateUI));
}
#if defined(USE_V8)
std::string ReactInstanceWin::getApplicationLocalFolder() {
auto local = winrt::Windows::Storage::ApplicationData::Current().LocalFolder().Path();
return Microsoft::Common::Unicode::Utf16ToUtf8(local.c_str(), local.size()) + "\\";
}
#endif
} // namespace Mso::React