Skip to content

Commit

Permalink
Lazy initalize layout aniamtions in the Commit Hook (#6586)
Browse files Browse the repository at this point in the history
## Summary

Since RN 0.76 the first surface is created after our
`initializeTurboModule` method is called on iOS. This change is probably
a result of [this
PR](facebook/react-native@a778979#diff-ebba7a8b74cc36d5a03437c3999863076246c6c4eb5559fd1a58719b720fd755).
This breaks our initialization of `layoutAnimationsProxy_`, so now we
lazy initialize it for every surface in the Commit hook.

## Test plan
Check if layout animations work in the `fabric-example` app.

---------

Co-authored-by: Tomek Zawadzki <[email protected]>
  • Loading branch information
bartlomiejbloniarz and tomekzaw authored Oct 10, 2024
1 parent 8c53bb8 commit 1f5d866
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ namespace reanimated {

ReanimatedCommitHook::ReanimatedCommitHook(
const std::shared_ptr<PropsRegistry> &propsRegistry,
const std::shared_ptr<UIManager> &uiManager)
: propsRegistry_(propsRegistry), uiManager_(uiManager) {
const std::shared_ptr<UIManager> &uiManager,
const std::shared_ptr<LayoutAnimationsProxy> &layoutAnimationsProxy)
: propsRegistry_(propsRegistry),
uiManager_(uiManager),
layoutAnimationsProxy_(layoutAnimationsProxy) {
uiManager_->registerCommitHook(*this);
}

Expand All @@ -28,6 +31,20 @@ RootShadowNode::Unshared ReanimatedCommitHook::shadowTreeWillCommit(
ShadowTree const &,
RootShadowNode::Shared const &,
RootShadowNode::Unshared const &newRootShadowNode) noexcept {
auto surfaceId = newRootShadowNode->getSurfaceId();

{
auto lock = std::unique_lock<std::mutex>(mutex_);
if (surfaceId > currentMaxSurfaceId_) {
uiManager_->getShadowTreeRegistry().enumerate(
[this](const ShadowTree &shadowTree, bool &stop) {
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
layoutAnimationsProxy_);
});
currentMaxSurfaceId_ = surfaceId;
}
}

auto reaShadowNode =
std::reinterpret_pointer_cast<ReanimatedCommitShadowNode>(
newRootShadowNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifdef RCT_NEW_ARCH_ENABLED

#include <reanimated/Fabric/PropsRegistry.h>
#include <reanimated/LayoutAnimations/LayoutAnimationsProxy.h>

#include <react/renderer/uimanager/UIManagerCommitHook.h>

Expand All @@ -15,7 +16,8 @@ class ReanimatedCommitHook : public UIManagerCommitHook {
public:
ReanimatedCommitHook(
const std::shared_ptr<PropsRegistry> &propsRegistry,
const std::shared_ptr<UIManager> &uiManager);
const std::shared_ptr<UIManager> &uiManager,
const std::shared_ptr<LayoutAnimationsProxy> &layoutAnimationsProxy);

~ReanimatedCommitHook() noexcept override;

Expand Down Expand Up @@ -44,6 +46,12 @@ class ReanimatedCommitHook : public UIManagerCommitHook {
std::shared_ptr<PropsRegistry> propsRegistry_;

std::shared_ptr<UIManager> uiManager_;

std::shared_ptr<LayoutAnimationsProxy> layoutAnimationsProxy_;

SurfaceId currentMaxSurfaceId_ = -1;

std::mutex mutex_; // Protects `currentMaxSurfaceId_`.
};

} // namespace reanimated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,15 @@ void NativeReanimatedModule::initializeFabric(
const std::shared_ptr<UIManager> &uiManager) {
uiManager_ = uiManager;

initializeLayoutAnimations();
initializeLayoutAnimationsProxy();

mountHook_ =
std::make_shared<ReanimatedMountHook>(propsRegistry_, uiManager_);
commitHook_ =
std::make_shared<ReanimatedCommitHook>(propsRegistry_, uiManager_);
commitHook_ = std::make_shared<ReanimatedCommitHook>(
propsRegistry_, uiManager_, layoutAnimationsProxy_);
}

void NativeReanimatedModule::initializeLayoutAnimations() {
void NativeReanimatedModule::initializeLayoutAnimationsProxy() {
uiManager_->setAnimationDelegate(nullptr);
auto scheduler = reinterpret_cast<Scheduler *>(uiManager_->getDelegate());
auto componentDescriptorRegistry =
Expand All @@ -876,13 +876,9 @@ void NativeReanimatedModule::initializeLayoutAnimations() {
scheduler->getContextContainer(),
uiWorkletRuntime_->getJSIRuntime(),
uiScheduler_);
uiManager_->getShadowTreeRegistry().enumerate(
[this](const ShadowTree &shadowTree, bool &stop) {
shadowTree.getMountingCoordinator()->setMountingOverrideDelegate(
layoutAnimationsProxy_);
});
}
}

#endif // RCT_NEW_ARCH_ENABLED

jsi::Value NativeReanimatedModule::subscribeForKeyboardEvents(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {

void initializeFabric(const std::shared_ptr<UIManager> &uiManager);

void initializeLayoutAnimations();
void initializeLayoutAnimationsProxy();

std::string obtainPropFromShadowNode(
jsi::Runtime &rt,
Expand Down

0 comments on commit 1f5d866

Please sign in to comment.