diff --git a/change/react-native-windows-5d9952b2-0b6d-4246-a1ec-994f54168545.json b/change/react-native-windows-5d9952b2-0b6d-4246-a1ec-994f54168545.json new file mode 100644 index 00000000000..50e0a89c002 --- /dev/null +++ b/change/react-native-windows-5d9952b2-0b6d-4246-a1ec-994f54168545.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Implement BlobCollector", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Shared/Modules/BlobCollector.cpp b/vnext/Shared/Modules/BlobCollector.cpp new file mode 100644 index 00000000000..131f4c6fb78 --- /dev/null +++ b/vnext/Shared/Modules/BlobCollector.cpp @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#include "BlobCollector.h" + +using std::string; + +namespace Microsoft::React { + +using Networking::IBlobResource; + +BlobCollector::BlobCollector(string blobId, std::shared_ptr resource) noexcept + : m_blobId{blobId}, m_weakResource{std::weak_ptr(resource)} {} + +BlobCollector::~BlobCollector() noexcept { + if (auto rc = m_weakResource.lock()) { + rc->Release(std::move(m_blobId)); + } +} + +} // namespace Microsoft::React diff --git a/vnext/Shared/Modules/BlobCollector.h b/vnext/Shared/Modules/BlobCollector.h new file mode 100644 index 00000000000..f8fc27e8ca1 --- /dev/null +++ b/vnext/Shared/Modules/BlobCollector.h @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#pragma once + +#include + +// JSI +#include + +namespace Microsoft::React { + +class JSI_EXPORT BlobCollector : public facebook::jsi::HostObject { + std::string m_blobId; + std::weak_ptr m_weakResource; + + public: + BlobCollector(std::string blobId, std::shared_ptr resource) noexcept; + + ~BlobCollector() noexcept; +}; + +} // namespace Microsoft::React diff --git a/vnext/Shared/Modules/BlobModule.cpp b/vnext/Shared/Modules/BlobModule.cpp index 4750d3e58a1..d54220cdb61 100644 --- a/vnext/Shared/Modules/BlobModule.cpp +++ b/vnext/Shared/Modules/BlobModule.cpp @@ -4,7 +4,9 @@ #include "BlobModule.h" #include +#include #include +#include "BlobCollector.h" // React Native #include @@ -38,6 +40,23 @@ void BlobTurboModule::Initialize(msrn::ReactContext const &reactContext) noexcep m_resource->Callbacks().OnError = [&reactContext](string &&errorText) { Modules::SendEvent(reactContext, L"blobFailed", {errorText}); }; + + namespace jsi = facebook::jsi; + msrn::ExecuteJsi(reactContext, [resource = m_resource](jsi::Runtime &runtime) { + runtime.global().setProperty( + runtime, + "__blobCollectorProvider", + jsi::Function::createFromHostFunction( + runtime, + jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"), + 1, + [resource](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, size_t count) { + auto blobId = args[0].asString(rt).utf8(rt); + auto collector = std::make_shared(blobId, resource); + + return jsi::Object::createFromHostObject(rt, collector); + })); + }); } ReactNativeSpecs::BlobModuleSpec_Constants BlobTurboModule::GetConstants() noexcept { diff --git a/vnext/Shared/Shared.vcxitems b/vnext/Shared/Shared.vcxitems index f93700f2604..965607c7369 100644 --- a/vnext/Shared/Shared.vcxitems +++ b/vnext/Shared/Shared.vcxitems @@ -193,6 +193,7 @@ + @@ -327,6 +328,7 @@ + @@ -565,4 +567,4 @@ NotUsing - + \ No newline at end of file diff --git a/vnext/Shared/Shared.vcxitems.filters b/vnext/Shared/Shared.vcxitems.filters index 13e60ac551a..5ce92221d85 100644 --- a/vnext/Shared/Shared.vcxitems.filters +++ b/vnext/Shared/Shared.vcxitems.filters @@ -275,6 +275,9 @@ + + Source Files\Modules + @@ -750,6 +753,9 @@ + + Header Files\Modules +