From 7b0e1663fc8183ae74f22bd895aab76a527e4f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar=20Rocha?= Date: Wed, 11 Oct 2023 18:21:10 -0700 Subject: [PATCH 1/3] Implement WebSocketTurboModuleProxy (#12214) * Implement WebSocketTurboModuleProxy * Change files * Define common constant for proxy ID name * Define common constant for shared state ID name * Define common constant for content handler ID name * clang format --- ...-c9696c65-0592-42c1-bd7a-8a215b34cd90.json | 7 +++ vnext/Shared/Modules/WebSocketModule.cpp | 45 +++++++++++++++---- vnext/Shared/Modules/WebSocketTurboModule.h | 19 ++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json diff --git a/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json b/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json new file mode 100644 index 00000000000..b5ad674bc1d --- /dev/null +++ b/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Implement WebSocketTurboModuleProxy", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Shared/Modules/WebSocketModule.cpp b/vnext/Shared/Modules/WebSocketModule.cpp index 6d5aa9414bc..214dcbcf575 100644 --- a/vnext/Shared/Modules/WebSocketModule.cpp +++ b/vnext/Shared/Modules/WebSocketModule.cpp @@ -52,6 +52,9 @@ using Microsoft::React::Networking::IWebSocketResource; constexpr char s_moduleName[] = "WebSocketModule"; constexpr wchar_t s_moduleNameW[] = L"WebSocketModule"; +constexpr wchar_t s_proxyNameW[] = L"WebSocketModule.Proxy"; +constexpr wchar_t s_sharedStateNameW[] = L"WebSocketModule.SharedState"; +constexpr wchar_t s_contentHandlerNameW[] = L"BlobModule.ContentHandler"; msrn::ReactModuleProvider s_moduleProvider = msrn::MakeTurboModuleProvider(); @@ -119,7 +122,7 @@ GetOrCreateWebSocket(int64_t id, string &&url, weak_ptr contentHandler; auto propId = ReactPropertyId>>{ - L"BlobModule.ContentHandler"}; + s_contentHandlerNameW}; if (auto prop = propBag.Get(propId)) contentHandler = prop.Value().lock(); @@ -171,11 +174,11 @@ WebSocketModule::WebSocketModule(winrt::Windows::Foundation::IInspectable const auto propBag = ReactPropertyBag{m_sharedState->InspectableProps.try_as()}; - auto proxyPropId = ReactPropertyId>>{L"WebSocketModule.Proxy"}; + auto proxyPropId = ReactPropertyId>>{s_proxyNameW}; auto proxy = weak_ptr{m_proxy}; propBag.Set(proxyPropId, std::move(proxy)); - auto statePropId = ReactPropertyId>>{L"WebSocketModule.SharedState"}; + auto statePropId = ReactPropertyId>>{s_sharedStateNameW}; auto state = weak_ptr{m_sharedState}; propBag.Set(statePropId, std::move(state)); } @@ -309,10 +312,9 @@ vector WebSocketModule::getMethods() WebSocketModuleProxy::WebSocketModuleProxy(IInspectable const &inspectableProperties) noexcept : m_inspectableProps{inspectableProperties} {} -void WebSocketModuleProxy::SendBinary(std::string &&base64String, int64_t id) noexcept /*override*/ { +void WebSocketModuleProxy::SendBinary(string &&base64String, int64_t id) noexcept /*override*/ { auto propBag = ReactPropertyBag{m_inspectableProps.try_as()}; - auto sharedPropId = - ReactPropertyId>>{L"WebSocketModule.SharedState"}; + auto sharedPropId = ReactPropertyId>>{s_sharedStateNameW}; auto state = propBag.Get(sharedPropId).Value(); weak_ptr weakWs = GetOrCreateWebSocket(id, {}, std::move(state)); @@ -353,8 +355,7 @@ shared_ptr WebSocketTurboModule::CreateResource(int64_t id, rc->SetOnMessage([id, context = m_context](size_t length, const string &message, bool isBinary) { auto args = msrn::JSValueObject{{"id", id}, {"type", isBinary ? "binary" : "text"}}; shared_ptr contentHandler; - auto propId = - ReactPropertyId>>{L"BlobModule.ContentHandler"}; + auto propId = ReactPropertyId>>{s_contentHandlerNameW}; auto propBag = context.Properties(); if (auto prop = propBag.Get(propId)) contentHandler = prop.Value().lock(); @@ -396,6 +397,11 @@ shared_ptr WebSocketTurboModule::CreateResource(int64_t id, void WebSocketTurboModule::Initialize(msrn::ReactContext const &reactContext) noexcept { m_context = reactContext.Handle(); + m_proxy = std::make_shared(m_resourceMap); + + auto proxyPropId = ReactPropertyId>>{s_proxyNameW}; + auto proxy = weak_ptr{m_proxy}; + m_context.Properties().Set(proxyPropId, std::move(proxy)); } void WebSocketTurboModule::Connect( @@ -487,6 +493,29 @@ void WebSocketTurboModule::RemoveListeners(double /*count*/) noexcept {} #pragma endregion WebSocketTurboModule +#pragma region WebSocketTurboModuleProxy + +WebSocketTurboModuleProxy::WebSocketTurboModuleProxy( + std::unordered_map> &resourceMap) noexcept + : m_resourceMap{resourceMap} {} + +#pragma endregion WebSocketTurboModuleProxy + +void WebSocketTurboModuleProxy::SendBinary(string &&base64String, int64_t id) noexcept /*override*/ +{ + auto rcItr = m_resourceMap.find(static_cast(id)); + if (rcItr == m_resourceMap.cend()) { + return; + } + + weak_ptr weakRc = (*rcItr).second; + if (auto rc = weakRc.lock()) { + rc->SendBinary(std::move(base64String)); + } +} + +#pragma region WebSocketTurboModule + /*extern*/ const char *GetWebSocketModuleName() noexcept { return s_moduleName; } diff --git a/vnext/Shared/Modules/WebSocketTurboModule.h b/vnext/Shared/Modules/WebSocketTurboModule.h index 030df965c93..2b4ec3137cb 100644 --- a/vnext/Shared/Modules/WebSocketTurboModule.h +++ b/vnext/Shared/Modules/WebSocketTurboModule.h @@ -10,6 +10,20 @@ namespace Microsoft::React { +class WebSocketTurboModuleProxy final : public IWebSocketModuleProxy { + std::unordered_map> &m_resourceMap; + + public: + WebSocketTurboModuleProxy( + std::unordered_map> &resourceMap) noexcept; + +#pragma region IWebSocketModuleProxy + + void SendBinary(std::string &&base64String, int64_t id) noexcept override; + +#pragma endregion +}; + REACT_MODULE(WebSocketTurboModule, L"WebSocketModule") struct WebSocketTurboModule { using ModuleSpec = ReactNativeSpecs::WebSocketModuleSpec; @@ -47,6 +61,11 @@ struct WebSocketTurboModule { winrt::Microsoft::ReactNative::ReactContext m_context; std::unordered_map> m_resourceMap; + + /// + /// Exposes a subset of the module's methods. + /// + std::shared_ptr m_proxy; }; } // namespace Microsoft::React From d1a264a5a88190a516eac44e1311bd029f0a4a7d Mon Sep 17 00:00:00 2001 From: "Julio C. Rocha" Date: Wed, 11 Oct 2023 18:51:41 -0700 Subject: [PATCH 2/3] Remove change file --- ...ative-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json diff --git a/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json b/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json deleted file mode 100644 index b5ad674bc1d..00000000000 --- a/change/react-native-windows-c9696c65-0592-42c1-bd7a-8a215b34cd90.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "prerelease", - "comment": "Implement WebSocketTurboModuleProxy", - "packageName": "react-native-windows", - "email": "julio.rocha@microsoft.com", - "dependentChangeType": "patch" -} From 2af56706f5e5a4959ca69daa8eb5a25744b8302d Mon Sep 17 00:00:00 2001 From: "Julio C. Rocha" Date: Wed, 11 Oct 2023 18:56:33 -0700 Subject: [PATCH 3/3] Change files --- ...ative-windows-4772174e-f940-4806-9e77-a99bfc82135c.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 change/react-native-windows-4772174e-f940-4806-9e77-a99bfc82135c.json diff --git a/change/react-native-windows-4772174e-f940-4806-9e77-a99bfc82135c.json b/change/react-native-windows-4772174e-f940-4806-9e77-a99bfc82135c.json new file mode 100644 index 00000000000..b256a11d875 --- /dev/null +++ b/change/react-native-windows-4772174e-f940-4806-9e77-a99bfc82135c.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "Implement WebSocketTurboModuleProxy (#12214)", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +}