Skip to content

Commit

Permalink
Simplify createInitialState interface (facebook#37069)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#37069

changelog: [internal]

To create initial C++ state, nothing beside props is needed from `ShadowNodeFragment` and this diff removes it.

It makes creation of state easier as we will no longer need to check if props are nullptr.

Reviewed By: mdvacca

Differential Revision: D45183692

fbshipit-source-id: 81ab8eb3c57f6ff64aaed7c5b395555dce6b60b2
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Apr 26, 2023
1 parent a718a88 commit 2cda4f7
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ ShadowNode::Shared ComponentDescriptorRegistry::createNode(
PropsParserContext{surfaceId, *contextContainer_.get()},
nullptr,
RawProps(propsDynamic));
auto const state =
componentDescriptor.createInitialState(ShadowNodeFragment{props}, family);
auto const state = componentDescriptor.createInitialState(props, family);

return componentDescriptor.createShadowNode(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ImageShadowNode final : public ConcreteViewShadowNode<
void setImageManager(const SharedImageManager &imageManager);

static ImageState initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor) {
auto imageSource = ImageSource{ImageSource::Type::Invalid};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,10 @@ void ScrollViewShadowNode::updateScrollContentOffsetIfNeeded() {
}

ScrollViewState ScrollViewShadowNode::initialStateData(
const ShadowNodeFragment &fragment,
Props::Shared const &props,
const ShadowNodeFamilyFragment & /*familyFragment*/,
const ComponentDescriptor & /*componentDescriptor*/) {
if (fragment.props != ShadowNodeFragment::propsPlaceholder()) {
auto const &scrollViewProps =
static_cast<ScrollViewProps const &>(*fragment.props);
return {scrollViewProps.contentOffset, {}, 0};
} else {
return ScrollViewState{};
}
return {static_cast<ScrollViewProps const &>(*props).contentOffset, {}, 0};
}

#pragma mark - LayoutableShadowNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ScrollViewShadowNode final : public ConcreteViewShadowNode<
using ConcreteViewShadowNode::ConcreteViewShadowNode;

static ScrollViewState initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AndroidTextInputComponentDescriptor final
}

virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const override {
int surfaceId = family->getSurfaceId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ComponentDescriptor {
* State's data which can be constructed based on initial Props.
*/
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const = 0;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
};

virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
Props::Shared const &props,
ShadowNodeFamily::Shared const &family) const override {
if (std::is_same<ConcreteStateData, StateData>::value) {
// Default case: Returning `null` for nodes that don't use `State`.
Expand All @@ -161,7 +161,7 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
return std::make_shared<ConcreteState>(
std::make_shared<ConcreteStateData const>(
ConcreteShadowNode::initialStateData(
fragment, ShadowNodeFamilyFragment::build(*family), *this)),
props, ShadowNodeFamilyFragment::build(*family), *this)),
family);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ConcreteShadowNode : public BaseShadowNodeT {
}

static ConcreteStateData initialStateData(
ShadowNodeFragment const &fragment,
Props::Shared const & /*props*/,
ShadowNodeFamilyFragment const &familyFragment,
ComponentDescriptor const &componentDescriptor) {
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ShadowNode::Unshared ComponentBuilder::build(
elementFragment.tag, elementFragment.surfaceId, nullptr},
nullptr);

auto initialState = componentDescriptor.createInitialState(
ShadowNodeFragment{elementFragment.props}, family);
auto initialState =
componentDescriptor.createInitialState(elementFragment.props, family);

auto constShadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ ShadowNode::Shared UIManager::createNode(
componentDescriptor.createFamily(fragment, std::move(eventTarget));
auto const props =
componentDescriptor.cloneProps(propsParserContext, nullptr, rawProps);
auto const state =
componentDescriptor.createInitialState(ShadowNodeFragment{props}, family);
auto const state = componentDescriptor.createInitialState(props, family);

auto shadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{
Expand Down

0 comments on commit 2cda4f7

Please sign in to comment.