Skip to content

Commit

Permalink
Cleans up LongLivedObjectCollection created by RN Windows
Browse files Browse the repository at this point in the history
React Native now intrinsically supports per-VM
LongLivedObjectCollections, so the additional behavior in Windows is
redundant.
  • Loading branch information
rozele committed Apr 9, 2024
1 parent 37c2d99 commit cb39c11
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 192 deletions.
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
4 changes: 1 addition & 3 deletions vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp
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

0 comments on commit cb39c11

Please sign in to comment.