Skip to content
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

Cleans up LongLivedObjectCollection created by RN Windows #12934

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Cleans up LongLivedObjectCollection created by RN Windows",
"packageName": "react-native-windows",
"email": "erozell@outlook.com",
"dependentChangeType": "patch"
}
52 changes: 13 additions & 39 deletions vnext/Microsoft.ReactNative.Cxx/JSI/LongLivedJsiValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,30 @@ namespace winrt::Microsoft::ReactNative {

// Wrap up JSI Runtime into a LongLivedObject
struct LongLivedJsiRuntime : facebook::react::LongLivedObject {
static std::weak_ptr<LongLivedJsiRuntime> CreateWeak(
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
facebook::jsi::Runtime &runtime) noexcept {
auto value = std::shared_ptr<LongLivedJsiRuntime>(new LongLivedJsiRuntime(longLivedObjectCollection, runtime));
longLivedObjectCollection->add(value);
static std::weak_ptr<LongLivedJsiRuntime> CreateWeak(facebook::jsi::Runtime &runtime) noexcept {
auto value = std::shared_ptr<LongLivedJsiRuntime>(new LongLivedJsiRuntime(runtime));
auto &longLivedObjectCollection = facebook::react::LongLivedObjectCollection::get(runtime);
longLivedObjectCollection.add(value);
return value;
}

facebook::jsi::Runtime &Runtime() {
return runtime_;
}

public: // LongLivedObject overrides
void allowRelease() {
if (auto longLivedObjectCollection = longLivedObjectCollection_.lock()) {
if (longLivedObjectCollection != nullptr) {
longLivedObjectCollection->remove(this);
return;
}
}
LongLivedObject::allowRelease();
}

protected:
LongLivedJsiRuntime(
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
facebook::jsi::Runtime &runtime)
: longLivedObjectCollection_(longLivedObjectCollection), runtime_(runtime) {}

LongLivedJsiRuntime(facebook::jsi::Runtime &runtime) : LongLivedObject(runtime) {}
LongLivedJsiRuntime(LongLivedJsiRuntime const &) = delete;

private:
// Use a weak reference to the collection to avoid reference loops
std::weak_ptr<facebook::react::LongLivedObjectCollection> longLivedObjectCollection_;
facebook::jsi::Runtime &runtime_;
};

// Wrap up a JSI Value into a LongLivedObject.
template <typename TValue>
struct LongLivedJsiValue : LongLivedJsiRuntime {
static std::weak_ptr<LongLivedJsiValue<TValue>> CreateWeak(
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
facebook::jsi::Runtime &runtime,
TValue &&value) noexcept {
auto valueWrapper = std::shared_ptr<LongLivedJsiValue<TValue>>(
new LongLivedJsiValue<TValue>(longLivedObjectCollection, runtime, std::forward<TValue>(value)));
longLivedObjectCollection->add(valueWrapper);
struct LongLivedJsiValue : facebook::react::LongLivedObject {
static std::weak_ptr<LongLivedJsiValue<TValue>> CreateWeak(facebook::jsi::Runtime &runtime, TValue &&value) noexcept {
auto valueWrapper =
std::shared_ptr<LongLivedJsiValue<TValue>>(new LongLivedJsiValue<TValue>(runtime, std::forward<TValue>(value)));
auto &longLivedObjectCollection = facebook::react::LongLivedObjectCollection::get(runtime);
longLivedObjectCollection.add(valueWrapper);
return valueWrapper;
}

Expand All @@ -67,11 +44,8 @@ struct LongLivedJsiValue : LongLivedJsiRuntime {

protected:
template <typename TValue2>
LongLivedJsiValue(
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
facebook::jsi::Runtime &runtime,
TValue2 &&value)
: LongLivedJsiRuntime(longLivedObjectCollection, runtime), value_(std::forward<TValue2>(value)) {}
LongLivedJsiValue(facebook::jsi::Runtime &runtime, TValue2 &&value)
: LongLivedObject(runtime), value_(std::forward<TValue2>(value)) {}

private:
TValue value_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,7 @@ void ReactInstanceWin::InitializeBridgeless() noexcept {
return turboModuleManager->getModule(name);
};

facebook::react::TurboModuleBinding::install(
runtime, std::function(binding), nullptr, m_options.TurboModuleProvider->LongLivedObjectCollection());
facebook::react::TurboModuleBinding::install(runtime, std::function(binding));

auto componentDescriptorRegistry =
Microsoft::ReactNative::WindowsComponentDescriptorRegistry::FromProperties(
Expand Down Expand Up @@ -840,7 +839,6 @@ void ReactInstanceWin::InitializeWithBridge() noexcept {
std::move(bundleRootPath), // bundleRootPath
std::move(cxxModules),
m_options.TurboModuleProvider,
m_options.TurboModuleProvider->LongLivedObjectCollection(),
m_reactContext->Properties(),
std::make_unique<BridgeUIBatchInstanceCallback>(weakThis),
m_jsMessageThread.Load(),
Expand Down
Loading
Loading