From 9349ee26e77f073bf737e26b21bd1f5a7a7fd774 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 18 Nov 2019 14:22:53 -0800 Subject: [PATCH] Fabric: Clarifying life-time concerns of `ShadowTree::delegate_` Summary: This diff makes it clear from the code that ShadowTree delegate must be around for the whole life-time of a ShadowTree instance and there is no way to revoke/reset the delegate. This makes reasoning about the lifetime much simpler. We didn't call `setDelegate(nullptr)` before, so nothing really changes here. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D18542502 fbshipit-source-id: f57ee21e0bb533fb82cb6f8ba7723e40ffb25a38 --- ReactCommon/fabric/mounting/ShadowTree.cpp | 23 ++++++---------------- ReactCommon/fabric/mounting/ShadowTree.h | 15 +++----------- ReactCommon/fabric/uimanager/Scheduler.cpp | 7 +++++-- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/ReactCommon/fabric/mounting/ShadowTree.cpp b/ReactCommon/fabric/mounting/ShadowTree.cpp index ce1689d7a87..72bb0313b50 100644 --- a/ReactCommon/fabric/mounting/ShadowTree.cpp +++ b/ReactCommon/fabric/mounting/ShadowTree.cpp @@ -91,10 +91,11 @@ static void updateMountedFlag( ShadowTree::ShadowTree( SurfaceId surfaceId, - const LayoutConstraints &layoutConstraints, - const LayoutContext &layoutContext, - const RootComponentDescriptor &rootComponentDescriptor) - : surfaceId_(surfaceId) { + LayoutConstraints const &layoutConstraints, + LayoutContext const &layoutContext, + RootComponentDescriptor const &rootComponentDescriptor, + ShadowTreeDelegate const &delegate) + : surfaceId_(surfaceId), delegate_(delegate) { const auto noopEventEmitter = std::make_shared( nullptr, -1, std::shared_ptr()); @@ -197,9 +198,7 @@ bool ShadowTree::tryCommit(ShadowTreeCommitTransaction transaction) const { mountingCoordinator_->push( ShadowTreeRevision{newRootShadowNode, revisionNumber, telemetry}); - if (delegate_) { - delegate_->shadowTreeDidFinishTransaction(*this, mountingCoordinator_); - } + delegate_.shadowTreeDidFinishTransaction(*this, mountingCoordinator_); return true; } @@ -244,15 +243,5 @@ void ShadowTree::emitLayoutEvents( } } -#pragma mark - Delegate - -void ShadowTree::setDelegate(ShadowTreeDelegate const *delegate) { - delegate_ = delegate; -} - -ShadowTreeDelegate const *ShadowTree::getDelegate() const { - return delegate_; -} - } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/mounting/ShadowTree.h b/ReactCommon/fabric/mounting/ShadowTree.h index 95dc67ac1ff..4653bebefaa 100644 --- a/ReactCommon/fabric/mounting/ShadowTree.h +++ b/ReactCommon/fabric/mounting/ShadowTree.h @@ -38,7 +38,8 @@ class ShadowTree final { SurfaceId surfaceId, LayoutConstraints const &layoutConstraints, LayoutContext const &layoutContext, - RootComponentDescriptor const &rootComponentDescriptor); + RootComponentDescriptor const &rootComponentDescriptor, + ShadowTreeDelegate const &delegate); ~ShadowTree(); @@ -65,16 +66,6 @@ class ShadowTree final { */ void commitEmptyTree() const; -#pragma mark - Delegate - - /* - * Sets and gets the delegate. - * The delegate is stored as a raw pointer, so the owner must null - * the pointer before being destroyed. - */ - void setDelegate(ShadowTreeDelegate const *delegate); - ShadowTreeDelegate const *getDelegate() const; - private: RootShadowNode::Unshared cloneRootShadowNode( RootShadowNode::Shared const &oldRootShadowNode, @@ -85,12 +76,12 @@ class ShadowTree final { std::vector &affectedLayoutableNodes) const; SurfaceId const surfaceId_; + ShadowTreeDelegate const &delegate_; mutable better::shared_mutex commitMutex_; mutable RootShadowNode::Shared rootShadowNode_; // Protected by `commitMutex_`. mutable ShadowTreeRevision::Number revisionNumber_{ 0}; // Protected by `commitMutex_`. - ShadowTreeDelegate const *delegate_; MountingCoordinator::Shared mountingCoordinator_; }; diff --git a/ReactCommon/fabric/uimanager/Scheduler.cpp b/ReactCommon/fabric/uimanager/Scheduler.cpp index f84e292df04..bcf88f10074 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.cpp +++ b/ReactCommon/fabric/uimanager/Scheduler.cpp @@ -134,8 +134,11 @@ void Scheduler::startSurface( SystraceSection s("Scheduler::startSurface"); auto shadowTree = std::make_unique( - surfaceId, layoutConstraints, layoutContext, *rootComponentDescriptor_); - shadowTree->setDelegate(uiManager_.get()); + surfaceId, + layoutConstraints, + layoutContext, + *rootComponentDescriptor_, + *uiManager_); auto uiManager = uiManager_;