Skip to content

Commit 09bd1f5

Browse files
Fix MountingOverrideDelegate initialization (#6841)
## Summary Currently we use the `setMountingOverrideDelegate` function to pass `LayoutAnimationProxy` to every surface, when we find out that a new surface was created. However, the `setMountingOverrideDelegate` function actually doesn't override the `MountingOverrideDelegate` - it stores all of them. This would lead to us processing the same transaction multiple times, so now we skip each already configured surface. ## Test plan
1 parent f7562d6 commit 09bd1f5

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.cpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,31 @@ ReanimatedCommitHook::~ReanimatedCommitHook() noexcept {
2727
uiManager_->unregisterCommitHook(*this);
2828
}
2929

30+
void ReanimatedCommitHook::maybeInitializeLayoutAnimations(
31+
SurfaceId surfaceId) {
32+
auto lock = std::unique_lock<std::mutex>(mutex_);
33+
if (surfaceId > currentMaxSurfaceId_) {
34+
// when a new surfaceId is observed we call setMountingOverrideDelegate
35+
// for all yet unseen surfaces
36+
uiManager_->getShadowTreeRegistry().enumerate(
37+
[this](const ShadowTree &shadowTree, bool &stop) {
38+
if (shadowTree.getSurfaceId() <= currentMaxSurfaceId_) {
39+
// the set function actually adds our delegate to a list, so we
40+
// shouldn't invoke it twice for the same surface
41+
return;
42+
}
43+
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
44+
layoutAnimationsProxy_);
45+
});
46+
currentMaxSurfaceId_ = surfaceId;
47+
}
48+
}
49+
3050
RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
3151
ShadowTree const &,
3252
RootShadowNode::Shared const &,
3353
RootShadowNode::Unshared const &newRootShadowNode) noexcept {
34-
auto surfaceId = newRootShadowNode->getSurfaceId();
35-
36-
{
37-
auto lock = std::unique_lock<std::mutex>(mutex_);
38-
if (surfaceId > currentMaxSurfaceId_) {
39-
uiManager_->getShadowTreeRegistry().enumerate(
40-
[this](const ShadowTree &shadowTree, bool &stop) {
41-
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
42-
layoutAnimationsProxy_);
43-
});
44-
currentMaxSurfaceId_ = surfaceId;
45-
}
46-
}
54+
maybeInitializeLayoutAnimations(newRootShadowNode->getSurfaceId());
4755

4856
auto reaShadowNode =
4957
std::reinterpret_pointer_cast<ReanimatedCommitShadowNode>(

packages/react-native-reanimated/Common/cpp/reanimated/Fabric/ReanimatedCommitHook.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class ReanimatedCommitHook : public UIManagerCommitHook {
2626

2727
void commitHookWasUnregistered(UIManager const &) noexcept override {}
2828

29+
void maybeInitializeLayoutAnimations(SurfaceId surfaceId);
30+
2931
RootShadowNode::Unshared shadowTreeWillCommit(
3032
ShadowTree const &shadowTree,
3133
RootShadowNode::Shared const &oldRootShadowNode,

0 commit comments

Comments
 (0)