From d418750359bb545311794f2c35c3aca879c62e3e Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 3 Feb 2020 06:22:51 -0800 Subject: [PATCH] Merge StateCoordinator into ShadowNodeFamily Summary: Changelog: [internal] Merges all of responsibilities of `StateCoordinator` into `ShadowNodeFamily`. Reviewed By: shergin Differential Revision: D19500104 fbshipit-source-id: f31ffded5a840e722fd898eef6a9f52cd2186df7 --- .../componentdescriptor/ComponentDescriptor.h | 2 +- .../ConcreteComponentDescriptor.h | 8 +-- .../core/shadownode/ShadowNodeFamily.cpp | 50 ++++++++++++++++++- .../fabric/core/shadownode/ShadowNodeFamily.h | 20 ++++++++ ReactCommon/fabric/core/state/ConcreteState.h | 17 +++---- ReactCommon/fabric/core/state/State.cpp | 11 ++-- ReactCommon/fabric/core/state/State.h | 6 ++- .../fabric/core/state/StateCoordinator.cpp | 9 ++-- .../core/tests/LayoutableShadowNodeTest.cpp | 3 ++ .../fabric/core/tests/ShadowNodeTest.cpp | 10 +++- .../uimanager/ComponentDescriptorRegistry.cpp | 4 +- ReactCommon/fabric/uimanager/UIManager.cpp | 4 +- 12 files changed, 113 insertions(+), 31 deletions(-) diff --git a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h index 943afb05185..e8b0700e5e0 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h @@ -109,7 +109,7 @@ class ComponentDescriptor { */ virtual State::Shared createInitialState( ShadowNodeFragment const &fragment, - SurfaceId const surfaceId) const = 0; + ShadowNodeFamily::Shared const &family) const = 0; /* * Creates a new State object that represents (and contains) a new version of diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index 04a07efe4a4..ce874a2b87c 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -120,15 +120,16 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { virtual State::Shared createInitialState( ShadowNodeFragment const &fragment, - SurfaceId const surfaceId) const override { + ShadowNodeFamily::Shared const &family) const override { if (std::is_same::value) { // Default case: Returning `null` for nodes that don't use `State`. return nullptr; } return std::make_shared( - ConcreteShadowNode::initialStateData(fragment, surfaceId, *this), - std::make_shared(eventDispatcher_)); + ConcreteShadowNode::initialStateData( + fragment, family->getSurfaceId(), *this), + family); } virtual State::Shared createState( @@ -158,6 +159,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor { return std::make_shared( ShadowNodeFamilyFragment{ fragment.tag, fragment.surfaceId, eventEmitter}, + eventDispatcher_, *this); } diff --git a/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.cpp b/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.cpp index fe5a5b82ec9..485ebd4a4b1 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.cpp +++ b/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.cpp @@ -17,8 +17,10 @@ using AncestorList = ShadowNode::AncestorList; ShadowNodeFamily::ShadowNodeFamily( ShadowNodeFamilyFragment const &fragment, + EventDispatcher::Weak eventDispatcher, ComponentDescriptor const &componentDescriptor) - : tag_(fragment.tag), + : eventDispatcher_(eventDispatcher), + tag_(fragment.tag), surfaceId_(fragment.surfaceId), eventEmitter_(fragment.eventEmitter), componentDescriptor_(componentDescriptor), @@ -87,5 +89,51 @@ AncestorList ShadowNodeFamily::getAncestors( return ancestors; } +const StateTarget &ShadowNodeFamily::getTarget() const { + std::shared_lock lock(mutex_); + return target_; +} + +void ShadowNodeFamily::setTarget(StateTarget &&target) const { + std::unique_lock lock(mutex_); + + assert(target && "`StateTarget` must not be empty."); + + if (target_) { + auto &previousState = target_.getShadowNode().getState(); + auto &nextState = target.getShadowNode().getState(); + + /* + * Checking and setting `isObsolete_` prevents old states to be recommitted + * on top of fresher states. It's okay to commit a tree with "older" Shadow + * Nodes (the evolution of nodes is not linear), however, we never back out + * states (they progress linearly). + */ + if (nextState->isObsolete_) { + return; + } + + previousState->isObsolete_ = true; + } + + target_ = std::move(target); +} + +void ShadowNodeFamily::dispatchRawState( + std::function &&stateData, + EventPriority priority) const { + auto eventDispatcher = eventDispatcher_.lock(); + if (!eventDispatcher || !target_) { // why do we check !target_ here? + return; + } + + eventDispatcher->dispatchStateUpdate( + {[=]() -> std::pair { + auto target = getTarget(); + return {std::move(target), stateData()}; + }}, + priority); +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.h b/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.h index 160188a2894..c18c2f4b1f9 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.h +++ b/ReactCommon/fabric/core/shadownode/ShadowNodeFamily.h @@ -9,6 +9,8 @@ #include +#include + #include #include #include @@ -36,6 +38,7 @@ class ShadowNodeFamily { ShadowNodeFamily( ShadowNodeFamilyFragment const &fragment, + EventDispatcher::Weak eventDispatcher, ComponentDescriptor const &componentDescriptor); /* @@ -64,9 +67,26 @@ class ShadowNodeFamily { SurfaceId getSurfaceId() const; + /* + * Sets and gets a state target. + */ + const StateTarget &getTarget() const; + void setTarget(StateTarget &&target) const; + + /* + * Dispatches a state update with given priority. + */ + void dispatchRawState( + std::function &&stateData, + EventPriority priority) const; + private: friend ShadowNode; + EventDispatcher::Weak eventDispatcher_; + mutable StateTarget target_{}; // Protected by `mutex_`. + mutable better::shared_mutex mutex_; + /* * Deprecated. */ diff --git a/ReactCommon/fabric/core/state/ConcreteState.h b/ReactCommon/fabric/core/state/ConcreteState.h index 8781bc69d55..e5ef7678ca5 100644 --- a/ReactCommon/fabric/core/state/ConcreteState.h +++ b/ReactCommon/fabric/core/state/ConcreteState.h @@ -27,10 +27,8 @@ class ConcreteState : public State { using Shared = std::shared_ptr; using Data = DataT; - explicit ConcreteState( - Data &&data, - StateCoordinator::Shared const &stateCoordinator) - : State(stateCoordinator), data_(std::move(data)) {} + explicit ConcreteState(Data &&data, ShadowNodeFamily::Shared const &family) + : State(family), data_(std::move(data)) {} explicit ConcreteState(Data &&data, State const &other) : State(other), data_(std::move(data)) {} @@ -71,16 +69,15 @@ class ConcreteState : public State { void updateState( std::function callback, EventPriority priority = EventPriority::AsynchronousBatched) const { - stateCoordinator_->dispatchRawState( - {[stateCoordinator = stateCoordinator_, - callback = std::move( - callback)]() -> std::pair { - auto target = stateCoordinator->getTarget(); + family_->dispatchRawState( + {[family = family_, + callback = std::move(callback)]() -> StateData::Shared { + auto target = family->getTarget(); auto oldState = target.getShadowNode().getState(); auto oldData = std::static_pointer_cast(oldState) ->getData(); auto newData = std::make_shared(callback(oldData)); - return {std::move(target), std::move(newData)}; + return newData; }}, priority); } diff --git a/ReactCommon/fabric/core/state/State.cpp b/ReactCommon/fabric/core/state/State.cpp index 1b16e370c2a..e424247d4e6 100644 --- a/ReactCommon/fabric/core/state/State.cpp +++ b/ReactCommon/fabric/core/state/State.cpp @@ -21,18 +21,17 @@ namespace facebook { namespace react { State::State(State const &state) - : stateCoordinator_(state.stateCoordinator_), - revision_(state.revision_ + 1){}; + : family_(state.family_), revision_(state.revision_ + 1){}; -State::State(StateCoordinator::Shared const &stateCoordinator) - : stateCoordinator_(stateCoordinator), revision_{1} {}; +State::State(ShadowNodeFamily::Shared const &family) + : family_(family), revision_{1} {}; void State::commit(std::shared_ptr const &shadowNode) const { - stateCoordinator_->setTarget(StateTarget{shadowNode}); + family_->setTarget(StateTarget{shadowNode}); } State::Shared State::getMostRecentState() const { - auto target = stateCoordinator_->getTarget(); + auto target = family_->getTarget(); return target ? target.getShadowNode().getState() : ShadowNodeFragment::statePlaceholder(); } diff --git a/ReactCommon/fabric/core/state/State.h b/ReactCommon/fabric/core/state/State.h index f0dae7e53b9..ce74a88f7f1 100644 --- a/ReactCommon/fabric/core/state/State.h +++ b/ReactCommon/fabric/core/state/State.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include namespace facebook { @@ -25,7 +26,7 @@ class State { using Shared = std::shared_ptr; explicit State(State const &state); - explicit State(StateCoordinator::Shared const &stateCoordinator); + explicit State(ShadowNodeFamily::Shared const &family); virtual ~State() = default; /* @@ -53,10 +54,11 @@ class State { void commit(std::shared_ptr const &shadowNode) const; protected: - StateCoordinator::Shared stateCoordinator_; + ShadowNodeFamily::Shared family_; private: friend class StateCoordinator; + friend class ShadowNodeFamily; /* * Indicates that the state was committed once and then was replaced by a diff --git a/ReactCommon/fabric/core/state/StateCoordinator.cpp b/ReactCommon/fabric/core/state/StateCoordinator.cpp index 80baccb7a54..7c3a50c0d6b 100644 --- a/ReactCommon/fabric/core/state/StateCoordinator.cpp +++ b/ReactCommon/fabric/core/state/StateCoordinator.cpp @@ -32,9 +32,12 @@ void StateCoordinator::setTarget(StateTarget &&target) const { auto &nextState = target.getShadowNode().getState(); /* - * Checking and setting `isObsolete_` prevents old states to be recommitted - * on top of fresher states. It's okay to commit a tree with "older" Shadow - * Nodes (the evolution of nodes is not linear), however, we never back out + * Checking and setting `isObsolete_` prevents old states to be + recommitted + * on top of fresher states. It's okay to commit a tree with "older" + Shadow + * Nodes (the evolution of nodes is not linear), however, we never back + out * states (they progress linearly). */ if (nextState->isObsolete_) { diff --git a/ReactCommon/fabric/core/tests/LayoutableShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/LayoutableShadowNodeTest.cpp index 689f6d16d91..f58e949952b 100644 --- a/ReactCommon/fabric/core/tests/LayoutableShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/LayoutableShadowNodeTest.cpp @@ -21,6 +21,7 @@ class LayoutableShadowNodeTest : public ::testing::Test { /* .surfaceId = */ 1, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeA_ = std::make_shared( @@ -37,6 +38,7 @@ class LayoutableShadowNodeTest : public ::testing::Test { /* .surfaceId = */ 1, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); auto traits = TestShadowNode::BaseTraits(); @@ -56,6 +58,7 @@ class LayoutableShadowNodeTest : public ::testing::Test { /* .surfaceId = */ 1, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeAAA_ = std::make_shared( diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp index ef367fd9229..0a2e5525bb7 100644 --- a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -41,6 +41,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeAA_ = std::make_shared( ShadowNodeFragment{ @@ -56,6 +57,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeABA_ = std::make_shared( ShadowNodeFragment{ @@ -71,6 +73,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeABB_ = std::make_shared( ShadowNodeFragment{ @@ -89,6 +92,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeAB_ = std::make_shared( ShadowNodeFragment{ @@ -104,6 +108,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeAC_ = std::make_shared( ShadowNodeFragment{ @@ -122,6 +127,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeA_ = std::make_shared( ShadowNodeFragment{ @@ -137,6 +143,7 @@ class ShadowNodeTest : public ::testing::Test { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); nodeZ_ = std::make_shared( ShadowNodeFragment{ @@ -229,6 +236,7 @@ TEST_F(ShadowNodeTest, handleState) { /* .surfaceId = */ surfaceId_, /* .eventEmitter = */ nullptr, }, + eventDispatcher_, componentDescriptor_); auto props = std::make_shared(); @@ -238,7 +246,7 @@ TEST_F(ShadowNodeTest, handleState) { /* .state = */ {}}; auto const initialState = - componentDescriptor_.createInitialState(fragment, surfaceId_); + componentDescriptor_.createInitialState(fragment, family); auto firstNode = std::make_shared( ShadowNodeFragment{ diff --git a/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp index 6faeff8714c..851dfe3dee8 100644 --- a/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp +++ b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp @@ -177,8 +177,8 @@ SharedShadowNode ComponentDescriptorRegistry::createNode( std::move(eventTarget)); auto const props = componentDescriptor.cloneProps(nullptr, RawProps(propsDynamic)); - auto const state = componentDescriptor.createInitialState( - ShadowNodeFragment{props}, surfaceId); + auto const state = + componentDescriptor.createInitialState(ShadowNodeFragment{props}, family); return componentDescriptor.createShadowNode( { diff --git a/ReactCommon/fabric/uimanager/UIManager.cpp b/ReactCommon/fabric/uimanager/UIManager.cpp index 0db6dd51894..37687c721b7 100644 --- a/ReactCommon/fabric/uimanager/UIManager.cpp +++ b/ReactCommon/fabric/uimanager/UIManager.cpp @@ -37,8 +37,8 @@ SharedShadowNode UIManager::createNode( ShadowNodeFamilyFragment{tag, surfaceId, nullptr}, std::move(eventTarget)); auto const props = componentDescriptor.cloneProps(nullptr, rawProps); - auto const state = componentDescriptor.createInitialState( - ShadowNodeFragment{props}, surfaceId); + auto const state = + componentDescriptor.createInitialState(ShadowNodeFragment{props}, family); auto shadowNode = componentDescriptor.createShadowNode( ShadowNodeFragment{