mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Layout event emitter (#57722)
Summary: > Stack 2/3 — parent: `feat/shadowTreeDidCommit`. Review that first. `onLayout` events are emitted inline from `ShadowTree::emitLayoutEvents`, which forces `ShadowTree` to depend on `ViewProps` and `BaseViewEventEmitter`. This moves that logic into a standalone `LayoutEventEmitter` that consumes the `shadowTreeDidCommit` hook (added in the parent PR) and is registered as a commit hook by `Scheduler`. Same filter (nodes with an `onLayout` prop), same timing (during commit), same `BaseViewEventEmitter::onLayout` call — behavior is unchanged. After this, `ShadowTree` no longer references view props or event emitters, and the per-commit layout-change signal is shared with `ResizeObserver` instead of being duplicated. ## Changelog: [INTERNAL] [CHANGED] - Emit `onLayout` from a `LayoutEventEmitter` commit hook instead of inline in `ShadowTree` Pull Request resolved: https://github.com/react/react-native/pull/57722 Test Plan: Behavior-preserving refactor. Existing `onLayout` tests pass and `ShadowTree` no longer includes `ViewShadowNode`/`ViewProps`. Verified in rn-tester that `onLayout` still fires on mount and on size changes. Reviewed By: christophpurrer Differential Revision: D114045065 Pulled By: javache fbshipit-source-id: 5b887537ad3e947f3a352fb9dd10b7d124f669df
This commit is contained in:
1 parent
3365667479
commit
55195ca43f
14 files changed
+129
-24
No files matched your search
@@ -11,7 +11,6 @@
|
||||
#include <jsinspector-modern/tracing/PerformanceTracerSection.h>
|
||||
#include <react/debug/react_native_assert.h>
|
||||
#include <react/renderer/components/root/RootComponentDescriptor.h>
|
||||
#include <react/renderer/components/view/ViewShadowNode.h>
|
||||
#include <react/renderer/core/LayoutContext.h>
|
||||
#include <react/renderer/core/LayoutPrimitives.h>
|
||||
#include <react/renderer/mounting/ShadowTreeRevision.h>
|
||||
@@ -466,8 +465,6 @@ CommitStatus ShadowTree::tryCommit(
|
||||
delegate_.shadowTreeDidCommit(
|
||||
*this, newRevision.rootShadowNode, affectedLayoutableNodes);
|
||||
|
||||
emitLayoutEvents(affectedLayoutableNodes);
|
||||
|
||||
if (isReactBranch) {
|
||||
scheduleReactRevisionPromotion();
|
||||
} else if (commitMode == CommitMode::Normal) {
|
||||
@@ -637,25 +634,6 @@ void ShadowTree::commitEmptyTree() const {
|
||||
{/* default commit options */});
|
||||
}
|
||||
|
||||
void ShadowTree::emitLayoutEvents(
|
||||
std::vector<const LayoutableShadowNode*>& affectedLayoutableNodes) const {
|
||||
TraceSection s(
|
||||
"ShadowTree::emitLayoutEvents",
|
||||
"affectedLayoutableNodes",
|
||||
affectedLayoutableNodes.size());
|
||||
|
||||
for (const auto* layoutableNode : affectedLayoutableNodes) {
|
||||
if (auto viewProps =
|
||||
dynamic_cast<const ViewProps*>(layoutableNode->getProps().get())) {
|
||||
if (viewProps->onLayout) {
|
||||
static_cast<const BaseViewEventEmitter&>(
|
||||
*layoutableNode->getEventEmitter())
|
||||
.onLayout(layoutableNode->getLayoutMetrics());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ShadowTree::notifyDelegatesOfUpdates() const {
|
||||
delegate_.shadowTreeDidFinishTransaction(mountingCoordinator_, true);
|
||||
}
|
||||
|
||||
@@ -161,8 +161,6 @@ class ShadowTree final {
|
||||
|
||||
void mount(ShadowTreeRevision revision, bool mountSynchronously) const;
|
||||
|
||||
void emitLayoutEvents(std::vector<const LayoutableShadowNode *> &affectedLayoutableNodes) const;
|
||||
|
||||
void scheduleReactRevisionPromotion() const;
|
||||
|
||||
const SurfaceId surfaceId_;
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include <react/renderer/mounting/MountingOverrideDelegate.h>
|
||||
#include <react/renderer/mounting/ShadowViewMutation.h>
|
||||
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
|
||||
#include <react/renderer/uimanager/LayoutEventEmitter.h>
|
||||
#include <react/renderer/uimanager/UIManager.h>
|
||||
#include <react/renderer/uimanager/UIManagerBinding.h>
|
||||
#include <mutex>
|
||||
@@ -148,6 +149,11 @@ Scheduler::Scheduler(
|
||||
|
||||
delegate_ = delegate;
|
||||
commitHooks_ = schedulerToolbox.commitHooks;
|
||||
|
||||
// Layout events (`onLayout`) are emitted as a standalone consumer of the
|
||||
// `shadowTreeDidCommit` commit hook.
|
||||
commitHooks_.push_back(std::make_shared<LayoutEventEmitter>());
|
||||
|
||||
uiManager_ = uiManager;
|
||||
|
||||
for (auto& commitHook : commitHooks_) {
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#include "LayoutEventEmitter.h"
|
||||
|
||||
#include <cxxreact/TraceSection.h>
|
||||
#include <react/renderer/components/view/ViewShadowNode.h>
|
||||
#include <react/renderer/core/LayoutableShadowNode.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
void LayoutEventEmitter::shadowTreeDidCommit(
|
||||
const ShadowTree& /*shadowTree*/,
|
||||
const RootShadowNode::Shared& /*rootShadowNode*/,
|
||||
const std::vector<const LayoutableShadowNode*>&
|
||||
affectedLayoutableNodes) noexcept {
|
||||
TraceSection s(
|
||||
"LayoutEventEmitter::shadowTreeDidCommit",
|
||||
"affectedLayoutableNodes",
|
||||
affectedLayoutableNodes.size());
|
||||
|
||||
for (const auto* layoutableNode : affectedLayoutableNodes) {
|
||||
if (auto viewProps =
|
||||
dynamic_cast<const ViewProps*>(layoutableNode->getProps().get())) {
|
||||
if (viewProps->onLayout) {
|
||||
static_cast<const BaseViewEventEmitter&>(
|
||||
*layoutableNode->getEventEmitter())
|
||||
.onLayout(layoutableNode->getLayoutMetrics());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace facebook::react
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <react/renderer/uimanager/UIManagerCommitHook.h>
|
||||
|
||||
namespace facebook::react {
|
||||
|
||||
/*
|
||||
* Emits layout events (`onLayout`) for the nodes whose layout changed in
|
||||
* each commit, using the `shadowTreeDidCommit` commit hook as the source of
|
||||
* layout changes (this logic used to be implemented directly in
|
||||
* `ShadowTree`).
|
||||
*/
|
||||
class LayoutEventEmitter final : public UIManagerCommitHook {
|
||||
public:
|
||||
void commitHookWasRegistered(const UIManager & /*uiManager*/) noexcept override {}
|
||||
void commitHookWasUnregistered(const UIManager & /*uiManager*/) noexcept override {}
|
||||
|
||||
void shadowTreeDidCommit(
|
||||
const ShadowTree &shadowTree,
|
||||
const RootShadowNode::Shared &rootShadowNode,
|
||||
const std::vector<const LayoutableShadowNode *> &affectedLayoutableNodes) noexcept override;
|
||||
};
|
||||
|
||||
} // namespace facebook::react
|
||||
@@ -3166,6 +3166,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -3075,6 +3075,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -3163,6 +3163,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -5397,6 +5397,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -5321,6 +5321,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -5394,6 +5394,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -2086,6 +2086,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -2022,6 +2022,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
@@ -2083,6 +2083,12 @@ class facebook::react::LayoutAnimationStatusDelegate {
|
||||
class facebook::react::LayoutConformanceShadowNode : public facebook::react::ConcreteShadowNode<facebook::react::LayoutConformanceShadowNodeComponentName, facebook::react::YogaLayoutableShadowNode, facebook::react::LayoutConformanceProps> {
|
||||
}
|
||||
|
||||
class facebook::react::LayoutEventEmitter : public facebook::react::UIManagerCommitHook {
|
||||
public virtual void commitHookWasRegistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void commitHookWasUnregistered(const facebook::react::UIManager&) noexcept override;
|
||||
public virtual void shadowTreeDidCommit(const facebook::react::ShadowTree& shadowTree, const facebook::react::RootShadowNode::Shared& rootShadowNode, const std::vector<const facebook::react::LayoutableShadowNode*>& affectedLayoutableNodes) noexcept override;
|
||||
}
|
||||
|
||||
class facebook::react::LayoutableShadowNode : public facebook::react::ShadowNode {
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNode& sourceShadowNode, const facebook::react::ShadowNodeFragment& fragment);
|
||||
public LayoutableShadowNode(const facebook::react::ShadowNodeFragment& fragment, const facebook::react::ShadowNodeFamily::Shared& family, facebook::react::ShadowNodeTraits traits);
|
||||
|
||||
Reference in new issue
Block a user