Skip to content
Merged
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
8 changes: 8 additions & 0 deletions change/react-native-windows-2020-04-21-14-06-12-master.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "prerelease",
"comment": "Hook up to packager websocket to allow 'r' to reload instance",
"packageName": "react-native-windows",
"email": "acoates@microsoft.com",
"dependentChangeType": "patch",
"date": "2020-04-21T21:06:12.961Z"
}
2 changes: 1 addition & 1 deletion vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ std::shared_ptr<IRedBoxHandler> ReactInstanceWin::GetRedBoxHandler() noexcept {

std::function<void()> ReactInstanceWin::GetLiveReloadCallback() noexcept {
// Live reload is enabled if we provide a callback function.
if (m_options.DeveloperSettings.UseLiveReload) {
if (m_options.DeveloperSettings.UseLiveReload || m_options.DeveloperSettings.UseFastRefresh) {
return Mso::MakeWeakMemberStdFunction(this, &ReactInstanceWin::OnLiveReload);
}
return std::function<void()>{};
Expand Down
53 changes: 53 additions & 0 deletions vnext/ReactUWP/Modules/DevSupportManagerUwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "Utilities.h"

#include <Utils/CppWinrtLessExceptions.h>
#include <winrt/Windows.Data.Json.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Web.Http.Filters.h>
#include <winrt/Windows.Web.Http.Headers.h>
Expand Down Expand Up @@ -73,12 +74,64 @@ void DevSupportManager::LaunchDevTools(const facebook::react::DevSettings &setti
DownloadFromAsync(facebook::react::DevServerHelper::get_LaunchDevToolsCommandUrl(settings.debugHost)).get();
}

std::future<void> DevSupportManager::CreatePackagerConnection(const facebook::react::DevSettings &settings) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::future [](start = 0, length = 17)

Is it possible to avoid using the std::future? It does not offer a continuation. The only way to observe it is to block a thread which is an anti-pattern.
It is better to use either IAsyncAction or Mso::Future.

m_ws = winrt::Windows::Networking::Sockets::MessageWebSocket();

m_wsMessageRevoker = m_ws.MessageReceived(
winrt::auto_revoke, [reloadCallback = settings.liveReloadCallback](auto && /*sender*/, auto &&args) noexcept {
try {
auto reader = args.GetDataReader();

uint32_t len = reader.UnconsumedBufferLength();
if (args.MessageType() == winrt::Windows::Networking::Sockets::SocketMessageType::Utf8) {
reader.UnicodeEncoding(winrt::Windows::Storage::Streams::UnicodeEncoding::Utf8);
std::vector<uint8_t> data(len);
reader.ReadBytes(data);

auto response =
std::string(Microsoft::Common::Utilities::CheckedReinterpretCast<char *>(data.data()), data.size());
auto json =
winrt::Windows::Data::Json::JsonObject::Parse(Microsoft::Common::Unicode::Utf8ToUtf16(response));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use folly::parseJSON here instead but this is fine too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to avoid adding extra folly usage, since long term once we are off the legacy bridge, we might be able to ditch folly altogether.


auto version = (int)json.GetNamedNumber(L"version", 0.0);
if (version != 2) {
return;
}
auto method = std::wstring(json.GetNamedString(L"method", L""));
if (method.empty()) {
return;
}

if (!method.compare(L"reload")) {
reloadCallback();
} else if (!method.compare(L"devMenu")) {
// TODO showDevMenu
} else if (!method.compare(L"captureHeap")) {
// TODO captureHeap
}
}
} catch (winrt::hresult_error const &e) {
}
});

winrt::Windows::Foundation::Uri uri(
Microsoft::Common::Unicode::Utf8ToUtf16("ws://" + settings.debugHost + "/message"));
auto async = m_ws.ConnectAsync(uri);

#ifdef DEFAULT_CPPWINRT_EXCEPTIONS
co_await async;
#else
co_await lessthrow_await_adapter<winrt::Windows::Foundation::IAsyncAction>{async};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. What does this do?
  2. Any reason this is using std::future instead of winrt::fire_and_forget as some other code does?

#endif
}

facebook::react::JSECreator DevSupportManager::LoadJavaScriptInProxyMode(const facebook::react::DevSettings &settings) {
// Reset exception state since client is requesting new service
m_exceptionCaught = false;

try {
LaunchDevTools(settings);
CreatePackagerConnection(settings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreatePackagerConnection [](start = 4, length = 24)

Why do we ignore the result?
Would it be better to have a sequence of async actions uisng .Then or co_await?


return [this, settings](
std::shared_ptr<facebook::react::ExecutorDelegate> delegate,
Expand Down
4 changes: 4 additions & 0 deletions vnext/ReactUWP/Modules/DevSupportManagerUwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <DevServerHelper.h>

#include <winrt/Windows.Networking.Sockets.h>
#include <atomic>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -39,8 +40,11 @@ class DevSupportManager : public facebook::react::IDevSupportManager {

private:
void LaunchDevTools(const facebook::react::DevSettings &settings);
std::future<void> CreatePackagerConnection(const facebook::react::DevSettings &settings);

private:
winrt::Windows::Networking::Sockets::MessageWebSocket m_ws{nullptr};
winrt::Windows::Networking::Sockets::MessageWebSocket::MessageReceived_revoker m_wsMessageRevoker;
bool m_exceptionCaught = false;
std::atomic_bool m_cancellation_token;
};
Expand Down
2 changes: 1 addition & 1 deletion vnext/Shared/OInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ std::vector<std::unique_ptr<NativeModule>> InstanceImpl::GetDefaultNativeModules

void InstanceImpl::RegisterForReloadIfNecessary() noexcept {
// setup polling for live reload
if (!m_devManager->HasException() && m_devSettings->liveReloadCallback != nullptr) {
if (!m_devManager->HasException() && !m_devSettings->useFastRefresh && m_devSettings->liveReloadCallback != nullptr) {
m_devManager->StartPollingLiveReload(m_devSettings->debugHost, m_devSettings->liveReloadCallback);
}
}
Expand Down