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
This commit is contained in:
Samuel Susla authored and Facebook Github Bot committed 2020-02-03 06:25:25 -08:00
1 parent ee01be3462
commit d418750359
12 files changed
+113 -31

No files matched your search

@@ -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
@@ -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<ConcreteStateData, StateData>::value) {
// Default case: Returning `null` for nodes that don't use `State`.
return nullptr;
}
return std::make_shared<ConcreteState>(
ConcreteShadowNode::initialStateData(fragment, surfaceId, *this),
std::make_shared<StateCoordinator>(eventDispatcher_));
ConcreteShadowNode::initialStateData(
fragment, family->getSurfaceId(), *this),
family);
}
virtual State::Shared createState(
@@ -158,6 +159,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
return std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{
fragment.tag, fragment.surfaceId, eventEmitter},
eventDispatcher_,
*this);
}
@@ -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<better::shared_mutex> lock(mutex_);
return target_;
}
void ShadowNodeFamily::setTarget(StateTarget &&target) const {
std::unique_lock<better::shared_mutex> 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::Shared()> &&stateData,
EventPriority priority) const {
auto eventDispatcher = eventDispatcher_.lock();
if (!eventDispatcher || !target_) { // why do we check !target_ here?
return;
}
eventDispatcher->dispatchStateUpdate(
{[=]() -> std::pair<StateTarget, StateData::Shared> {
auto target = getTarget();
return {std::move(target), stateData()};
}},
priority);
}
} // namespace react
} // namespace facebook
@@ -9,6 +9,8 @@
#include <memory>
#include <better/mutex.h>
#include <react/core/EventEmitter.h>
#include <react/core/ReactPrimitives.h>
#include <react/core/ShadowNodeFamilyFragment.h>
@@ -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::Shared()> &&stateData,
EventPriority priority) const;
private:
friend ShadowNode;
EventDispatcher::Weak eventDispatcher_;
mutable StateTarget target_{}; // Protected by `mutex_`.
mutable better::shared_mutex mutex_;
/*
* Deprecated.
*/
+7 -10
View File
@@ -27,10 +27,8 @@ class ConcreteState : public State {
using Shared = std::shared_ptr<const ConcreteState>;
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<Data(const Data &oldData)> callback,
EventPriority priority = EventPriority::AsynchronousBatched) const {
stateCoordinator_->dispatchRawState(
{[stateCoordinator = stateCoordinator_,
callback = std::move(
callback)]() -> std::pair<StateTarget, StateData::Shared> {
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<const ConcreteState>(oldState)
->getData();
auto newData = std::make_shared<Data>(callback(oldData));
return {std::move(target), std::move(newData)};
return newData;
}},
priority);
}
+5 -6
View File
@@ -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<ShadowNode const> 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();
}
+4 -2
View File
@@ -8,6 +8,7 @@
#pragma once
#include <folly/dynamic.h>
#include <react/core/ShadowNodeFamily.h>
#include <react/core/StateCoordinator.h>
namespace facebook {
@@ -25,7 +26,7 @@ class State {
using Shared = std::shared_ptr<const State>;
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<ShadowNode const> 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
@@ -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_) {
@@ -21,6 +21,7 @@ class LayoutableShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeA_ = std::make_shared<TestShadowNode>(
@@ -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<TestShadowNode>(
@@ -41,6 +41,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeAA_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -56,6 +57,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeABA_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -71,6 +73,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeABB_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -89,6 +92,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeAB_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -104,6 +108,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeAC_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -122,6 +127,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeA_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -137,6 +143,7 @@ class ShadowNodeTest : public ::testing::Test {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
nodeZ_ = std::make_shared<TestShadowNode>(
ShadowNodeFragment{
@@ -229,6 +236,7 @@ TEST_F(ShadowNodeTest, handleState) {
/* .surfaceId = */ surfaceId_,
/* .eventEmitter = */ nullptr,
},
eventDispatcher_,
componentDescriptor_);
auto props = std::make_shared<const TestProps>();
@@ -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<TestShadowNode>(
ShadowNodeFragment{
@@ -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(
{
+2 -2
View File
@@ -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{