-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Hitting 'r' in the packager should reload the instance #4665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
|
@@ -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) { | ||
| 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)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could use folly::parseJSON here instead but this is fine too
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| #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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why do we ignore the result? |
||
|
|
||
| return [this, settings]( | ||
| std::shared_ptr<facebook::react::ExecutorDelegate> delegate, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.