Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy initalize layout aniamtions in the Commit Hook #6586

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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::function<void(SurfaceId)> initializeLayoutAnimations)
: propsRegistry_(propsRegistry),
uiManager_(uiManager),
initializeLayoutAnimations_(initializeLayoutAnimations) {
uiManager_->registerCommitHook(*this);
}

Expand All @@ -28,6 +31,16 @@ 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 (!initializedSurfaces_.contains(surfaceId)) {
initializeLayoutAnimations_(surfaceId);
initializedSurfaces_.insert(surfaceId);
}
bartlomiejbloniarz marked this conversation as resolved.
Show resolved Hide resolved
}

auto reaShadowNode =
std::reinterpret_pointer_cast<ReanimatedCommitShadowNode>(
newRootShadowNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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::function<void(SurfaceId)> initializeLayoutAnimations);
bartlomiejbloniarz marked this conversation as resolved.
Show resolved Hide resolved

~ReanimatedCommitHook() noexcept override;

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

std::shared_ptr<UIManager> uiManager_;

std::function<void(SurfaceId)> initializeLayoutAnimations_;

std::unordered_set<SurfaceId> initializedSurfaces_;

std::mutex mutex_;
bartlomiejbloniarz marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace reanimated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,17 @@ 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_, [this](SurfaceId surfaceId) {
initializeLayoutAnimations(surfaceId);
});
}

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

void NativeReanimatedModule::initializeLayoutAnimations(SurfaceId surfaceId) {
uiManager_->getShadowTreeRegistry().visit(
surfaceId, [this](const ShadowTree &shadowTree) {
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,9 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {

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

void initializeLayoutAnimations();
void initializeLayoutAnimationsProxy();

void initializeLayoutAnimations(SurfaceId surfaceId);

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