Skip to content

Commit

Permalink
[0.71] Implement BlobCollector (#12276)
Browse files Browse the repository at this point in the history
* Implement BlobCollector (#12269)

* Implement BlobCollector

* Change files

* clang format

* Remove change files

* Change files
  • Loading branch information
JunielKatarn authored Oct 21, 2023
1 parent 7f2e4a1 commit ce7c217
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Implement BlobCollector (#12269)",
"packageName": "react-native-windows",
"email": "julio.rocha@microsoft.com",
"dependentChangeType": "patch"
}
21 changes: 21 additions & 0 deletions vnext/Shared/Modules/BlobCollector.cpp
Original file line number Diff line number Diff line change
@@ -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<IBlobResource> resource) noexcept
: m_blobId{blobId}, m_weakResource{std::weak_ptr<IBlobResource>(resource)} {}

BlobCollector::~BlobCollector() noexcept {
if (auto rc = m_weakResource.lock()) {
rc->Release(std::move(m_blobId));
}
}

} // namespace Microsoft::React
23 changes: 23 additions & 0 deletions vnext/Shared/Modules/BlobCollector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <Networking/IBlobResource.h>

// JSI
#include <jsi/jsi.h>

namespace Microsoft::React {

class JSI_EXPORT BlobCollector : public facebook::jsi::HostObject {
std::string m_blobId;
std::weak_ptr<Networking::IBlobResource> m_weakResource;

public:
BlobCollector(std::string blobId, std::shared_ptr<Networking::IBlobResource> resource) noexcept;

~BlobCollector() noexcept;
};

} // namespace Microsoft::React
19 changes: 19 additions & 0 deletions vnext/Shared/Modules/BlobModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "BlobModule.h"

#include <CreateModules.h>
#include <JSI/JsiApiContext.h>
#include <Modules/CxxModuleUtilities.h>
#include "BlobCollector.h"

// React Native
#include <cxxreact/JsArgumentHelpers.h>
Expand Down Expand Up @@ -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<BlobCollector>(blobId, resource);

return jsi::Object::createFromHostObject(rt, collector);
}));
});
}

ReactNativeSpecs::BlobModuleSpec_Constants BlobTurboModule::GetConstants() noexcept {
Expand Down
2 changes: 2 additions & 0 deletions vnext/Shared/Shared.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\AsyncStorageModuleWin32.cpp">
<ExcludedFromBuild Condition="'$(ApplicationType)' == ''">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobModule.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\CxxModuleUtilities.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\ExceptionsManagerModule.cpp" />
Expand Down Expand Up @@ -99,6 +100,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\RuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\ScriptStore.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobModule.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\CxxModuleUtilities.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\FileReaderModule.h" />
Expand Down
6 changes: 6 additions & 0 deletions vnext/Shared/Shared.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
<ClCompile Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.cpp">
<Filter>Source Files\Modules</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
Expand Down Expand Up @@ -487,6 +490,9 @@
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.h">
<Filter>Header Files\Modules</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)tracing\rnw.wprp">
Expand Down

0 comments on commit ce7c217

Please sign in to comment.