Skip to content

Commit cb39c11

Browse files
committed
Cleans up LongLivedObjectCollection created by RN Windows
React Native now intrinsically supports per-VM LongLivedObjectCollections, so the additional behavior in Windows is redundant.
1 parent 37c2d99 commit cb39c11

File tree

8 files changed

+129
-192
lines changed

8 files changed

+129
-192
lines changed

vnext/Microsoft.ReactNative.Cxx/JSI/LongLivedJsiValue.h

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,30 @@ namespace winrt::Microsoft::ReactNative {
1111

1212
// Wrap up JSI Runtime into a LongLivedObject
1313
struct LongLivedJsiRuntime : facebook::react::LongLivedObject {
14-
static std::weak_ptr<LongLivedJsiRuntime> CreateWeak(
15-
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
16-
facebook::jsi::Runtime &runtime) noexcept {
17-
auto value = std::shared_ptr<LongLivedJsiRuntime>(new LongLivedJsiRuntime(longLivedObjectCollection, runtime));
18-
longLivedObjectCollection->add(value);
14+
static std::weak_ptr<LongLivedJsiRuntime> CreateWeak(facebook::jsi::Runtime &runtime) noexcept {
15+
auto value = std::shared_ptr<LongLivedJsiRuntime>(new LongLivedJsiRuntime(runtime));
16+
auto &longLivedObjectCollection = facebook::react::LongLivedObjectCollection::get(runtime);
17+
longLivedObjectCollection.add(value);
1918
return value;
2019
}
2120

2221
facebook::jsi::Runtime &Runtime() {
2322
return runtime_;
2423
}
2524

26-
public: // LongLivedObject overrides
27-
void allowRelease() {
28-
if (auto longLivedObjectCollection = longLivedObjectCollection_.lock()) {
29-
if (longLivedObjectCollection != nullptr) {
30-
longLivedObjectCollection->remove(this);
31-
return;
32-
}
33-
}
34-
LongLivedObject::allowRelease();
35-
}
36-
3725
protected:
38-
LongLivedJsiRuntime(
39-
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
40-
facebook::jsi::Runtime &runtime)
41-
: longLivedObjectCollection_(longLivedObjectCollection), runtime_(runtime) {}
42-
26+
LongLivedJsiRuntime(facebook::jsi::Runtime &runtime) : LongLivedObject(runtime) {}
4327
LongLivedJsiRuntime(LongLivedJsiRuntime const &) = delete;
44-
45-
private:
46-
// Use a weak reference to the collection to avoid reference loops
47-
std::weak_ptr<facebook::react::LongLivedObjectCollection> longLivedObjectCollection_;
48-
facebook::jsi::Runtime &runtime_;
4928
};
5029

5130
// Wrap up a JSI Value into a LongLivedObject.
5231
template <typename TValue>
53-
struct LongLivedJsiValue : LongLivedJsiRuntime {
54-
static std::weak_ptr<LongLivedJsiValue<TValue>> CreateWeak(
55-
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
56-
facebook::jsi::Runtime &runtime,
57-
TValue &&value) noexcept {
58-
auto valueWrapper = std::shared_ptr<LongLivedJsiValue<TValue>>(
59-
new LongLivedJsiValue<TValue>(longLivedObjectCollection, runtime, std::forward<TValue>(value)));
60-
longLivedObjectCollection->add(valueWrapper);
32+
struct LongLivedJsiValue : facebook::react::LongLivedObject {
33+
static std::weak_ptr<LongLivedJsiValue<TValue>> CreateWeak(facebook::jsi::Runtime &runtime, TValue &&value) noexcept {
34+
auto valueWrapper =
35+
std::shared_ptr<LongLivedJsiValue<TValue>>(new LongLivedJsiValue<TValue>(runtime, std::forward<TValue>(value)));
36+
auto &longLivedObjectCollection = facebook::react::LongLivedObjectCollection::get(runtime);
37+
longLivedObjectCollection.add(valueWrapper);
6138
return valueWrapper;
6239
}
6340

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

6845
protected:
6946
template <typename TValue2>
70-
LongLivedJsiValue(
71-
std::shared_ptr<facebook::react::LongLivedObjectCollection> const &longLivedObjectCollection,
72-
facebook::jsi::Runtime &runtime,
73-
TValue2 &&value)
74-
: LongLivedJsiRuntime(longLivedObjectCollection, runtime), value_(std::forward<TValue2>(value)) {}
47+
LongLivedJsiValue(facebook::jsi::Runtime &runtime, TValue2 &&value)
48+
: LongLivedObject(runtime), value_(std::forward<TValue2>(value)) {}
7549

7650
private:
7751
TValue value_;

vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,7 @@ void ReactInstanceWin::InitializeBridgeless() noexcept {
646646
return turboModuleManager->getModule(name);
647647
};
648648

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

652651
auto componentDescriptorRegistry =
653652
Microsoft::ReactNative::WindowsComponentDescriptorRegistry::FromProperties(
@@ -840,7 +839,6 @@ void ReactInstanceWin::InitializeWithBridge() noexcept {
840839
std::move(bundleRootPath), // bundleRootPath
841840
std::move(cxxModules),
842841
m_options.TurboModuleProvider,
843-
m_options.TurboModuleProvider->LongLivedObjectCollection(),
844842
m_reactContext->Properties(),
845843
std::make_unique<BridgeUIBatchInstanceCallback>(weakThis),
846844
m_jsMessageThread.Load(),

0 commit comments

Comments
 (0)