mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Prevent double calls to setMounted when merging a JS revision (#56424)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56424 Changelog: [GENERAL][FIXED] Fixed double `setMounted` flag calls with fabric branching enabled This change removes the commit source condition from the call site in `ShadowTree::tryCommit` and moves the logic into updateMountedFlag itself: - Mounted flags are skipped for ReactRevisionMerge commits because they were already set during the React branch commit. Setting them again would double increment the EventEmitter's additive enable counter. - Runtime shadow node references updates condition didn't change, only moved to a single variable. - When neither operation is needed, the function returns early to avoid an unnecessary tree traversal under the DispatchMutex. Reviewed By: lenaic, rubennorte Differential Revision: D100125886 fbshipit-source-id: aceb1ae3609957048b78f615233ed00ce2e04038
This commit is contained in:
1 parent
179e0cdef6
commit
a9a0000bcf
2 files changed
+38
-20
No files matched your search
@@ -422,7 +422,7 @@ CommitStatus ShadowTree::tryCommit(
|
||||
|
||||
auto newRevisionNumber = currentRevision_.number + 1;
|
||||
|
||||
if (!isReactBranch) {
|
||||
{
|
||||
std::scoped_lock dispatchLock(EventEmitter::DispatchMutex());
|
||||
updateMountedFlag(
|
||||
currentRevision_.rootShadowNode->getChildren(),
|
||||
|
||||
@@ -29,6 +29,25 @@ void updateMountedFlag(
|
||||
return;
|
||||
}
|
||||
|
||||
// Mounted flags shouldn't be updated during the React revision merge
|
||||
// because they were already set during the React branch commit. Setting them
|
||||
// again would double-increment the EventEmitter's additive enable counter.
|
||||
bool shouldUpdateMountedFlag =
|
||||
commitSource != ShadowTreeCommitSource::ReactRevisionMerge;
|
||||
|
||||
// Runtime shadow node references are updated during the React revision
|
||||
// commits so that JS can access layout data from the merged tree.
|
||||
bool shouldUpdateRuntimeReference =
|
||||
(commitSource == ShadowTreeCommitSource::React &&
|
||||
ReactNativeFeatureFlags::updateRuntimeShadowNodeReferencesOnCommit()) ||
|
||||
(ReactNativeFeatureFlags::
|
||||
updateRuntimeShadowNodeReferencesOnCommitThread() &&
|
||||
ShadowNode::getUseRuntimeShadowNodeReferenceUpdateOnThread());
|
||||
|
||||
if (!shouldUpdateMountedFlag && !shouldUpdateRuntimeReference) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t index = 0;
|
||||
|
||||
// Stage 1: Mount and unmount "updated" children.
|
||||
@@ -47,15 +66,12 @@ void updateMountedFlag(
|
||||
break;
|
||||
}
|
||||
|
||||
newChild->setMounted(true);
|
||||
oldChild->setMounted(false);
|
||||
if (shouldUpdateMountedFlag) {
|
||||
newChild->setMounted(true);
|
||||
oldChild->setMounted(false);
|
||||
}
|
||||
|
||||
if ((commitSource == ShadowTreeCommitSource::React &&
|
||||
ReactNativeFeatureFlags::
|
||||
updateRuntimeShadowNodeReferencesOnCommit()) ||
|
||||
(ReactNativeFeatureFlags::
|
||||
updateRuntimeShadowNodeReferencesOnCommitThread() &&
|
||||
ShadowNode::getUseRuntimeShadowNodeReferenceUpdateOnThread())) {
|
||||
if (shouldUpdateRuntimeReference) {
|
||||
newChild->updateRuntimeShadowNodeReference(newChild);
|
||||
}
|
||||
|
||||
@@ -68,14 +84,12 @@ void updateMountedFlag(
|
||||
// State 2: Mount new children.
|
||||
for (index = lastIndexAfterFirstStage; index < newChildren.size(); index++) {
|
||||
const auto& newChild = newChildren[index];
|
||||
newChild->setMounted(true);
|
||||
|
||||
if ((commitSource == ShadowTreeCommitSource::React &&
|
||||
ReactNativeFeatureFlags::
|
||||
updateRuntimeShadowNodeReferencesOnCommit()) ||
|
||||
(ReactNativeFeatureFlags::
|
||||
updateRuntimeShadowNodeReferencesOnCommitThread() &&
|
||||
ShadowNode::getUseRuntimeShadowNodeReferenceUpdateOnThread())) {
|
||||
if (shouldUpdateMountedFlag) {
|
||||
newChild->setMounted(true);
|
||||
}
|
||||
|
||||
if (shouldUpdateRuntimeReference) {
|
||||
newChild->updateRuntimeShadowNodeReference(newChild);
|
||||
}
|
||||
|
||||
@@ -83,10 +97,14 @@ void updateMountedFlag(
|
||||
}
|
||||
|
||||
// State 3: Unmount old children.
|
||||
for (index = lastIndexAfterFirstStage; index < oldChildren.size(); index++) {
|
||||
const auto& oldChild = oldChildren[index];
|
||||
oldChild->setMounted(false);
|
||||
updateMountedFlag(oldChild->getChildren(), {}, commitSource);
|
||||
if (shouldUpdateMountedFlag) {
|
||||
for (index = lastIndexAfterFirstStage; index < oldChildren.size();
|
||||
index++) {
|
||||
const auto& oldChild = oldChildren[index];
|
||||
oldChild->setMounted(false);
|
||||
|
||||
updateMountedFlag(oldChild->getChildren(), {}, commitSource);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace facebook::react
|
||||
Reference in new issue
Block a user