Skip to content

Commit a07b3b5

Browse files
Commit every tree when the media features change
1 parent 60ff87a commit a07b3b5

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerMediaFeatures.cpp‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,12 @@ void UIManager::setMediaFeatures(MediaFeatures mediaFeatures) const {
9595
}
9696
(*store)->set(mediaFeatures);
9797

98-
// Each commit resolves the outdated conditional values in
99-
// `shadowTreeWillCommit`, so a commit without changes is enough. A tree
100-
// without conditional values has nothing to update.
98+
// `shadowTreeWillCommit` resolves the outdated values, so an empty commit is
99+
// enough. Commit every tree: a React commit in progress can add new ones.
101100
shadowTreeRegistry_.enumerate(
102101
[&](const ShadowTree& shadowTree, bool& /*stop*/) {
103102
shadowTree.commit(
104-
[](const RootShadowNode& oldRootShadowNode)
105-
-> RootShadowNode::Unshared {
106-
if (!oldRootShadowNode.subtreeHasConditionalValues()) {
107-
return nullptr;
108-
}
103+
[](const RootShadowNode& oldRootShadowNode) {
109104
return std::make_shared<RootShadowNode>(
110105
oldRootShadowNode, ShadowNodeFragment{});
111106
},

‎packages/react-native/ReactCommon/react/renderer/uimanager/tests/UIManagerMediaFeaturesTest.cpp‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,20 @@ TEST_F(UIManagerMediaFeaturesTest, reactCommitResolvesAChangeDuringIt) {
182182
EXPECT_FLOAT_EQ(committedOpacity(), 0.5);
183183
}
184184

185+
TEST_F(
186+
UIManagerMediaFeaturesTest,
187+
firstConditionalViewResolvesAChangeDuringItsCommit) {
188+
// The committed tree has no conditional values yet, only the React commit.
189+
SetMediaFeaturesDuringCommitHook commitHook;
190+
commitHook.mediaFeatures = MediaFeatures{.colorScheme = ColorScheme::Dark};
191+
uiManager_->registerCommitHook(commitHook);
192+
reactCommit(createView());
193+
uiManager_->unregisterCommitHook(commitHook);
194+
195+
EXPECT_FALSE(commitHook.mediaFeatures.has_value());
196+
EXPECT_FLOAT_EQ(committedOpacity(), 0.5);
197+
}
198+
185199
TEST_F(UIManagerMediaFeaturesTest, cancelledReactCommitDoesNotSaveTheChange) {
186200
auto view = createView();
187201
reactCommit(view);
@@ -254,17 +268,22 @@ TEST_F(UIManagerMediaFeaturesTest, aCommitOfAnOlderTreeResolvesAChange) {
254268
EXPECT_FLOAT_EQ(committedOpacity(), 0.5);
255269
}
256270

257-
TEST_F(UIManagerMediaFeaturesTest, aChangeDoesNotCommitATreeWithoutThem) {
271+
TEST_F(UIManagerMediaFeaturesTest, aChangeDoesNotCloneViewsOfATreeWithoutThem) {
258272
reactCommit(uiManager_->createNode(
259273
viewTag_,
260274
"View",
261275
surfaceId_,
262276
RawProps(folly::dynamic::object("opacity", 1)),
263277
nullptr));
264-
auto revisionNumber = shadowTree_->getCurrentRevision().number;
278+
auto revisionBeforeChange = shadowTree_->getCurrentRevision();
265279

280+
// The change commits only a new root.
266281
uiManager_->setMediaFeatures({.colorScheme = ColorScheme::Dark});
267-
EXPECT_EQ(shadowTree_->getCurrentRevision().number, revisionNumber);
282+
auto revision = shadowTree_->getCurrentRevision();
283+
EXPECT_EQ(revision.number, revisionBeforeChange.number + 1);
284+
EXPECT_EQ(
285+
revision.rootShadowNode->getChildren().front(),
286+
revisionBeforeChange.rootShadowNode->getChildren().front());
268287
}
269288

270289
TEST_F(UIManagerMediaFeaturesTest, aChangeDoesNotCloneViewsThatDoNotReadIt) {

0 commit comments

Comments
 (0)