diff --git a/ReactCommon/fabric/uimanager/ShadowTree.cpp b/ReactCommon/fabric/uimanager/ShadowTree.cpp index dd8c1e16e..f74c079e3 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.cpp +++ b/ReactCommon/fabric/uimanager/ShadowTree.cpp @@ -38,6 +38,11 @@ Tag ShadowTree::getRootTag() const { return rootTag_; } +SharedRootShadowNode ShadowTree::getRootShadowNode() const { + std::lock_guard lock(commitMutex_); + return rootShadowNode_; +} + #pragma mark - Layout Size ShadowTree::measure(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const { @@ -46,7 +51,7 @@ Size ShadowTree::measure(const LayoutConstraints &layoutConstraints, const Layou return newRootShadowNode->getLayoutMetrics().frame.size; } -void ShadowTree::constraintLayout(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) { +void ShadowTree::constraintLayout(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const { auto newRootShadowNode = cloneRootShadowNode(layoutConstraints, layoutContext); complete(newRootShadowNode); } @@ -54,15 +59,15 @@ void ShadowTree::constraintLayout(const LayoutConstraints &layoutConstraints, co #pragma mark - Commiting UnsharedRootShadowNode ShadowTree::cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const { - auto oldRootShadowNode = rootShadowNode_; + auto oldRootShadowNode = getRootShadowNode(); const auto &props = std::make_shared(*oldRootShadowNode->getProps(), layoutConstraints, layoutContext); auto newRootShadowNode = std::make_shared(*oldRootShadowNode, ShadowNodeFragment {.props = props}); return newRootShadowNode; } -void ShadowTree::complete(const SharedShadowNodeUnsharedList &rootChildNodes) { - auto oldRootShadowNode = rootShadowNode_; +void ShadowTree::complete(const SharedShadowNodeUnsharedList &rootChildNodes) const { + auto oldRootShadowNode = getRootShadowNode(); auto newRootShadowNode = std::make_shared( *oldRootShadowNode, @@ -74,8 +79,8 @@ void ShadowTree::complete(const SharedShadowNodeUnsharedList &rootChildNodes) { complete(newRootShadowNode); } -void ShadowTree::complete(UnsharedRootShadowNode newRootShadowNode) { - SharedRootShadowNode oldRootShadowNode = rootShadowNode_; +void ShadowTree::complete(UnsharedRootShadowNode newRootShadowNode) const { + SharedRootShadowNode oldRootShadowNode = getRootShadowNode(); newRootShadowNode->layout(); @@ -99,7 +104,7 @@ bool ShadowTree::commit( const SharedRootShadowNode &oldRootShadowNode, const SharedRootShadowNode &newRootShadowNode, const ShadowViewMutationList &mutations -) { +) const { std::lock_guard lock(commitMutex_); if (oldRootShadowNode != rootShadowNode_) { @@ -109,10 +114,11 @@ bool ShadowTree::commit( rootShadowNode_ = newRootShadowNode; toggleEventEmitters(mutations); + return true; } -void ShadowTree::emitLayoutEvents(const ShadowViewMutationList &mutations) { +void ShadowTree::emitLayoutEvents(const ShadowViewMutationList &mutations) const { for (const auto &mutation : mutations) { // Only `Insert` and `Update` mutations can affect layout metrics. if ( @@ -147,7 +153,7 @@ void ShadowTree::emitLayoutEvents(const ShadowViewMutationList &mutations) { } } -void ShadowTree::toggleEventEmitters(const ShadowViewMutationList &mutations) { +void ShadowTree::toggleEventEmitters(const ShadowViewMutationList &mutations) const { std::lock_guard lock(EventEmitter::DispatchMutex()); for (const auto &mutation : mutations) { diff --git a/ReactCommon/fabric/uimanager/ShadowTree.h b/ReactCommon/fabric/uimanager/ShadowTree.h index d7add5ccf..7ee60c82c 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.h +++ b/ReactCommon/fabric/uimanager/ShadowTree.h @@ -49,17 +49,19 @@ public: /* * Applies given `layoutConstraints` and `layoutContext` and commit * the new shadow tree. + * Returns `true` if the operation is finished successfully. * Can be called from any thread. */ - void constraintLayout(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext); + void constraintLayout(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const; #pragma mark - Application /* * Create a new shadow tree with given `rootChildNodes` and commit. * Can be called from any thread. + * Returns `true` if the operation is finished successfully. */ - void complete(const SharedShadowNodeUnsharedList &rootChildNodes); + void complete(const SharedShadowNodeUnsharedList &rootChildNodes) const; #pragma mark - Delegate @@ -74,17 +76,22 @@ public: private: UnsharedRootShadowNode cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const; - void complete(UnsharedRootShadowNode newRootShadowNode); + void complete(UnsharedRootShadowNode newRootShadowNode) const; bool commit( const SharedRootShadowNode &oldRootShadowNode, const SharedRootShadowNode &newRootShadowNode, const ShadowViewMutationList &mutations - ); - void toggleEventEmitters(const ShadowViewMutationList &mutations); - void emitLayoutEvents(const ShadowViewMutationList &mutations); + ) const; + void toggleEventEmitters(const ShadowViewMutationList &mutations) const; + void emitLayoutEvents(const ShadowViewMutationList &mutations) const; + + /* + * Return `rootShadowNodeMutex_` protected by `commitMutex_`. + */ + SharedRootShadowNode getRootShadowNode() const; const Tag rootTag_; - SharedRootShadowNode rootShadowNode_; + mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`. ShadowTreeDelegate const *delegate_; mutable std::mutex commitMutex_; };