Skip to content

Commit 1c51d66

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Deprecate ShadowNode::ListOfShared and migrate to std::vector<std::shared_ptr<const ShadowNode>> (facebook#52402)
Summary: Pull Request resolved: facebook#52402 changelog: [internal] Mark ShadowNode::ListOfShared as deprecated and replace most usages throughout the React Native renderer codebase with the explicit std::vector<std::shared_ptr<const ShadowNode>> type. This improves code clarity by making the container type explicit rather than relying on a type alias. Reviewed By: christophpurrer Differential Revision: D77651676 fbshipit-source-id: 8c4bd9b8cbbe467384b947ef9e7a4524f2053e36
1 parent 9d63098 commit 1c51d66

File tree

19 files changed

+96
-59
lines changed

19 files changed

+96
-59
lines changed

packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ getPointerEventsProcessorFromRuntime(facebook::jsi::Runtime& runtime) {
6666

6767
static std::vector<facebook::jsi::Value>
6868
getArrayOfInstanceHandlesFromShadowNodes(
69-
const ShadowNode::ListOfShared& nodes,
69+
const std::vector<std::shared_ptr<const ShadowNode>>& nodes,
7070
facebook::jsi::Runtime& runtime) {
7171
// JSI doesn't support adding elements to an array after creation,
7272
// so we need to accumulate the values in a vector and then create

packages/react-native/ReactCommon/react/nativemodule/fantomtestspecificmethods/internal/FantomForcedCloneCommitHook.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ std::shared_ptr<const ShadowNode> findAndClone(
2727
children[i] = maybeClone;
2828
return node->clone(
2929
{ShadowNodeFragment::propsPlaceholder(),
30-
std::make_shared<ShadowNode::ListOfShared>(children)});
30+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
31+
children)});
3132
}
3233
}
3334

packages/react-native/ReactCommon/react/renderer/animations/tests/LayoutAnimationTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ static void testShadowNodeTreeLifeCycleLayoutAnimations(
117117
auto currentRootNode = std::static_pointer_cast<const RootShadowNode>(
118118
emptyRootNode->ShadowNode::clone(ShadowNodeFragment{
119119
ShadowNodeFragment::propsPlaceholder(),
120-
std::make_shared<ShadowNode::ListOfShared>(
121-
ShadowNode::ListOfShared{singleRootChildNode})}));
120+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
121+
std::vector<std::shared_ptr<const ShadowNode>>{
122+
singleRootChildNode})}));
122123

123124
// Building an initial view hierarchy.
124125
auto viewTree = StubViewTree(ShadowView(*emptyRootNode));

packages/react-native/ReactCommon/react/renderer/components/view/tests/ViewTest.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ TEST_F(YogaDirtyFlagTest, removingLastChildMustDirtyYogaNode) {
174174

175175
return oldShadowNode.clone(
176176
{ShadowNodeFragment::propsPlaceholder(),
177-
std::make_shared<const ShadowNode::ListOfShared>(children)});
177+
std::make_shared<
178+
const std::vector<std::shared_ptr<const ShadowNode>>>(
179+
children)});
178180
});
179181

180182
EXPECT_TRUE(
@@ -193,7 +195,9 @@ TEST_F(YogaDirtyFlagTest, reversingListOfChildrenMustDirtyYogaNode) {
193195

194196
return oldShadowNode.clone(
195197
{ShadowNodeFragment::propsPlaceholder(),
196-
std::make_shared<const ShadowNode::ListOfShared>(children)});
198+
std::make_shared<
199+
const std::vector<std::shared_ptr<const ShadowNode>>>(
200+
children)});
197201
});
198202

199203
EXPECT_TRUE(

packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ thread_local bool useRuntimeShadowNodeReferenceUpdateOnThread{false}; // NOLINT
3535

3636
ShadowNode::SharedListOfShared ShadowNode::emptySharedShadowNodeSharedList() {
3737
static const auto emptySharedShadowNodeSharedList =
38-
std::make_shared<ShadowNode::ListOfShared>();
38+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>();
3939
return emptySharedShadowNodeSharedList;
4040
}
4141

@@ -177,7 +177,8 @@ ComponentHandle ShadowNode::getComponentHandle() const {
177177
return family_->getComponentHandle();
178178
}
179179

180-
const ShadowNode::ListOfShared& ShadowNode::getChildren() const {
180+
const std::vector<std::shared_ptr<const ShadowNode>>& ShadowNode::getChildren()
181+
const {
181182
return *children_;
182183
}
183184

@@ -241,7 +242,8 @@ void ShadowNode::appendChild(const std::shared_ptr<const ShadowNode>& child) {
241242
ensureUnsealed();
242243

243244
cloneChildrenIfShared();
244-
auto& children = const_cast<ShadowNode::ListOfShared&>(*children_);
245+
auto& children =
246+
const_cast<std::vector<std::shared_ptr<const ShadowNode>>&>(*children_);
245247
children.push_back(child);
246248

247249
child->family_->setParent(family_);
@@ -257,7 +259,8 @@ void ShadowNode::replaceChild(
257259
cloneChildrenIfShared();
258260
newChild->family_->setParent(family_);
259261

260-
auto& children = const_cast<ShadowNode::ListOfShared&>(*children_);
262+
auto& children =
263+
const_cast<std::vector<std::shared_ptr<const ShadowNode>>&>(*children_);
261264
auto size = children.size();
262265

263266
if (suggestedIndex != std::numeric_limits<size_t>::max() &&
@@ -287,7 +290,8 @@ void ShadowNode::cloneChildrenIfShared() {
287290
}
288291

289292
traits_.unset(ShadowNodeTraits::Trait::ChildrenAreShared);
290-
children_ = std::make_shared<ShadowNode::ListOfShared>(*children_);
293+
children_ = std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
294+
*children_);
291295
}
292296

293297
void ShadowNode::updateTraitsIfNeccessary() {
@@ -395,7 +399,9 @@ std::shared_ptr<ShadowNode> ShadowNode::cloneTree(
395399
children[childIndex] = childNode;
396400

397401
childNode = parentNode.clone(
398-
{.children = std::make_shared<ShadowNode::ListOfShared>(children)});
402+
{.children =
403+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
404+
children)});
399405
}
400406

401407
return std::const_pointer_cast<ShadowNode>(childNode);
@@ -411,15 +417,17 @@ std::shared_ptr<ShadowNode> cloneMultipleRecursive(
411417
ShadowNode>(const ShadowNode&, const ShadowNodeFragment&)>& callback) {
412418
const auto* family = &shadowNode.getFamily();
413419
auto& children = shadowNode.getChildren();
414-
std::shared_ptr<ShadowNode::ListOfShared> newChildren;
420+
std::shared_ptr<std::vector<std::shared_ptr<const ShadowNode>>> newChildren;
415421
auto count = childrenCount.at(family);
416422

417423
for (int i = 0; count > 0 && i < children.size(); i++) {
418424
const auto childFamily = &children[i]->getFamily();
419425
if (childrenCount.contains(childFamily)) {
420426
count--;
421427
if (!newChildren) {
422-
newChildren = std::make_shared<ShadowNode::ListOfShared>(children);
428+
newChildren =
429+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
430+
children);
423431
}
424432
(*newChildren)[i] = cloneMultipleRecursive(
425433
*children[i], familiesToUpdate, childrenCount, callback);

packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ class ShadowNode : public Sealable,
4141
// TODO(T223558094): delete this in the next version.
4242
using Unshared [[deprecated("Use std::shared_ptr<ShadowNode> instead")]] =
4343
std::shared_ptr<ShadowNode>;
44-
using ListOfShared = std::vector<std::shared_ptr<const ShadowNode>>;
44+
// TODO(T223558094): delete this in the next version.
45+
using ListOfShared [[deprecated(
46+
"Use std::vector<std::shared_ptr<const ShadowNode>> instead")]] =
47+
std::vector<std::shared_ptr<const ShadowNode>>;
4548
using ListOfWeak = std::vector<std::weak_ptr<const ShadowNode>>;
46-
using SharedListOfShared = std::shared_ptr<const ListOfShared>;
47-
using UnsharedListOfShared = std::shared_ptr<ListOfShared>;
49+
using SharedListOfShared =
50+
std::shared_ptr<const std::vector<std::shared_ptr<const ShadowNode>>>;
51+
using UnsharedListOfShared =
52+
std::shared_ptr<std::vector<std::shared_ptr<const ShadowNode>>>;
4853
using UnsharedListOfWeak = std::shared_ptr<ListOfWeak>;
4954

5055
using AncestorList = std::vector<std::pair<
@@ -145,7 +150,7 @@ class ShadowNode : public Sealable,
145150
ShadowNodeTraits getTraits() const;
146151

147152
const Props::Shared& getProps() const;
148-
const ListOfShared& getChildren() const;
153+
const std::vector<std::shared_ptr<const ShadowNode>>& getChildren() const;
149154
const SharedEventEmitter& getEventEmitter() const;
150155
jsi::Value getInstanceHandle(jsi::Runtime& runtime) const;
151156
Tag getTag() const;

packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class ShadowNodeTest : public testing::Test {
7878
familyABB,
7979
traits);
8080

81-
auto nodeABChildren = std::make_shared<ShadowNode::ListOfShared>(
82-
ShadowNode::ListOfShared{nodeABA_, nodeABB_});
81+
auto nodeABChildren =
82+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
83+
std::vector<std::shared_ptr<const ShadowNode>>{nodeABA_, nodeABB_});
8384

8485
auto familyAB = componentDescriptor_.createFamily(ShadowNodeFamilyFragment{
8586
/* .tag = */ 15,
@@ -107,8 +108,10 @@ class ShadowNodeTest : public testing::Test {
107108
familyAC,
108109
traits);
109110

110-
auto nodeAChildren = std::make_shared<ShadowNode::ListOfShared>(
111-
ShadowNode::ListOfShared{nodeAA_, nodeAB_, nodeAC_});
111+
auto nodeAChildren =
112+
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
113+
std::vector<std::shared_ptr<const ShadowNode>>{
114+
nodeAA_, nodeAB_, nodeAC_});
112115

113116
auto familyA = componentDescriptor_.createFamily(ShadowNodeFamilyFragment{
114117
/* .tag = */ 17,

packages/react-native/ReactCommon/react/renderer/element/ComponentBuilder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ std::shared_ptr<ShadowNode> ComponentBuilder::build(
2020
auto& componentDescriptor =
2121
componentDescriptorRegistry_->at(elementFragment.componentHandle);
2222

23-
auto children = ShadowNode::ListOfShared{};
23+
auto children = std::vector<std::shared_ptr<const ShadowNode>>{};
2424
children.reserve(elementFragment.children.size());
2525
for (const auto& childFragment : elementFragment.children) {
2626
children.push_back(build(childFragment));
@@ -35,7 +35,8 @@ std::shared_ptr<ShadowNode> ComponentBuilder::build(
3535
auto constShadowNode = componentDescriptor.createShadowNode(
3636
ShadowNodeFragment{
3737
elementFragment.props,
38-
std::make_shared<const ShadowNode::ListOfShared>(children),
38+
std::make_shared<
39+
const std::vector<std::shared_ptr<const ShadowNode>>>(children),
3940
initialState},
4041
family);
4142

packages/react-native/ReactCommon/react/renderer/element/tests/ElementTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ TEST(ElementTest, testNormalCases) {
8585
EXPECT_EQ(shadowNodeABA->getChildren().size(), 0);
8686
EXPECT_EQ(
8787
shadowNodeA->getChildren(),
88-
(ShadowNode::ListOfShared{shadowNodeAA, shadowNodeAB}));
88+
(std::vector<std::shared_ptr<const ShadowNode>>{
89+
shadowNodeAA, shadowNodeAB}));
8990
EXPECT_EQ(
90-
shadowNodeAB->getChildren(), (ShadowNode::ListOfShared{shadowNodeABA}));
91+
shadowNodeAB->getChildren(),
92+
(std::vector<std::shared_ptr<const ShadowNode>>{shadowNodeABA}));
9193

9294
// Props
9395
EXPECT_EQ(shadowNodeA->getProps()->nativeId, "node A");

packages/react-native/ReactCommon/react/renderer/mounting/ShadowTree.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static std::shared_ptr<ShadowNode> progressState(const ShadowNode& shadowNode) {
4343
}
4444
}
4545

46-
auto newChildren = ShadowNode::ListOfShared{};
46+
auto newChildren = std::vector<std::shared_ptr<const ShadowNode>>{};
4747
if (!shadowNode.getChildren().empty()) {
4848
auto index = size_t{0};
4949
for (const auto& childNode : shadowNode.getChildren()) {
@@ -66,9 +66,11 @@ static std::shared_ptr<ShadowNode> progressState(const ShadowNode& shadowNode) {
6666

6767
return shadowNode.clone({
6868
ShadowNodeFragment::propsPlaceholder(),
69-
areChildrenChanged ? std::make_shared<const ShadowNode::ListOfShared>(
70-
std::move(newChildren))
71-
: ShadowNodeFragment::childrenPlaceholder(),
69+
areChildrenChanged
70+
? std::make_shared<
71+
const std::vector<std::shared_ptr<const ShadowNode>>>(
72+
std::move(newChildren))
73+
: ShadowNodeFragment::childrenPlaceholder(),
7274
isStateChanged ? newState : ShadowNodeFragment::statePlaceholder(),
7375
});
7476
}
@@ -102,7 +104,7 @@ static std::shared_ptr<ShadowNode> progressState(
102104

103105
auto& children = shadowNode.getChildren();
104106
auto& baseChildren = baseShadowNode.getChildren();
105-
auto newChildren = ShadowNode::ListOfShared{};
107+
auto newChildren = std::vector<std::shared_ptr<const ShadowNode>>{};
106108

107109
auto childrenSize = children.size();
108110
auto baseChildrenSize = baseChildren.size();
@@ -153,9 +155,11 @@ static std::shared_ptr<ShadowNode> progressState(
153155

154156
return shadowNode.clone({
155157
ShadowNodeFragment::propsPlaceholder(),
156-
areChildrenChanged ? std::make_shared<const ShadowNode::ListOfShared>(
157-
std::move(newChildren))
158-
: ShadowNodeFragment::childrenPlaceholder(),
158+
areChildrenChanged
159+
? std::make_shared<
160+
const std::vector<std::shared_ptr<const ShadowNode>>>(
161+
std::move(newChildren))
162+
: ShadowNodeFragment::childrenPlaceholder(),
159163
isStateChanged ? newState : ShadowNodeFragment::statePlaceholder(),
160164
});
161165
}

0 commit comments

Comments
 (0)