This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,85 @@
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_preprocessor_flags_for_build_mode")
load(
"//tools/build_defs/oss:rn_defs.bzl",
"ANDROID",
"APPLE",
"CXX",
"fb_xplat_cxx_test",
"get_apple_compiler_flags",
"get_apple_inspector_flags",
"react_native_xplat_target",
"rn_xplat_cxx_library",
"subdir_glob",
)
APPLE_COMPILER_FLAGS = get_apple_compiler_flags()
rn_xplat_cxx_library(
name = "element",
srcs = glob(
["**/*.cpp"],
exclude = glob(["tests/**/*.cpp"]),
),
headers = glob(
["**/*.h"],
exclude = glob(["tests/**/*.h"]),
),
header_namespace = "",
exported_headers = subdir_glob(
[
("", "*.h"),
],
prefix = "react/element",
),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_labels = ["supermodule:ios/default/public.react_native.infra"],
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),
force_static = True,
macosx_tests_override = [],
platforms = (ANDROID, APPLE, CXX),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
tests = [":tests"],
visibility = ["PUBLIC"],
deps = [
"//xplat/fbsystrace:fbsystrace",
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/components/root:root"),
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/components/scrollview:scrollview"),
react_native_xplat_target("fabric/components/text:text"),
react_native_xplat_target("fabric/uimanager:uimanager"),
react_native_xplat_target("fabric/core:core"),
react_native_xplat_target("fabric/debug:debug"),
react_native_xplat_target("utils:utils"),
],
)
fb_xplat_cxx_test(
name = "tests",
srcs = glob(["tests/**/*.cpp"]),
headers = glob(["tests/**/*.h"]),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
contacts = ["oncall+react_native@xmail.facebook.com"],
platforms = (ANDROID, APPLE, CXX),
deps = [
":element",
"//xplat/third-party/gmock:gtest",
react_native_xplat_target("fabric/components/root:root"),
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/components/scrollview:scrollview"),
],
)

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ComponentBuilder.h"
namespace facebook {
namespace react {
ComponentBuilder::ComponentBuilder(
ComponentDescriptorRegistry::Shared const &componentDescriptorRegistry)
: componentDescriptorRegistry_(componentDescriptorRegistry){};
ShadowNode::Unshared ComponentBuilder::build(
ElementFragment const &elementFragment) const {
auto &componentDescriptor =
componentDescriptorRegistry_->at(elementFragment.componentHandle);
auto children = ShadowNode::ListOfShared{};
children.reserve(elementFragment.children.size());
for (auto const &childFragment : elementFragment.children) {
children.push_back(build(childFragment));
}
auto family = componentDescriptor.createFamily(
ShadowNodeFamilyFragment{
elementFragment.tag, elementFragment.surfaceId, nullptr},
nullptr);
auto state = elementFragment.state
? elementFragment.state
: componentDescriptor.createInitialState(
ShadowNodeFragment{elementFragment.props}, family);
auto constShadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{
elementFragment.props,
std::make_shared<ShadowNode::ListOfShared const>(children),
state},
family);
auto shadowNode = std::const_pointer_cast<ShadowNode>(constShadowNode);
if (elementFragment.referenceCallback) {
elementFragment.referenceCallback(shadowNode);
}
if (elementFragment.finalizeCallback) {
elementFragment.finalizeCallback(*shadowNode);
}
return shadowNode;
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <memory>
#include <react/core/ComponentDescriptor.h>
#include <react/core/ShadowNode.h>
#include <react/core/ShadowNodeFamilyFragment.h>
#include <react/core/ShadowNodeFragment.h>
#include <react/uimanager/ComponentDescriptorRegistry.h>
#include <react/element/Element.h>
#include <react/element/ElementFragment.h>
namespace facebook {
namespace react {
/*
* Build `ShadowNode` trees with a given given `Element` trees.
*/
class ComponentBuilder final {
public:
ComponentBuilder(
ComponentDescriptorRegistry::Shared const &componentDescriptorRegistry);
/*
* Copyable and movable.
*/
ComponentBuilder(ComponentBuilder const &componentBuilder) = default;
ComponentBuilder(ComponentBuilder &&componentBuilder) noexcept = default;
ComponentBuilder &operator=(ComponentBuilder const &other) = default;
ComponentBuilder &operator=(ComponentBuilder &&other) = default;
/*
* Builds a `ShadowNode` tree with given `Element` tree using stored
* `ComponentDescriptorRegistry`.
*/
template <typename ShadowNodeT>
std::shared_ptr<ShadowNodeT> build(Element<ShadowNodeT> element) const {
return std::static_pointer_cast<ShadowNodeT>(build(element.fragment_));
}
private:
/*
* Internal, type-erased version of `build`.
*/
ShadowNode::Unshared build(ElementFragment const &elementFragment) const;
ComponentDescriptorRegistry::Shared componentDescriptorRegistry_;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,10 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "Element.h"
// Intentionally empty.

View File

@ -0,0 +1,160 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
#include <react/core/ShadowNode.h>
#include <react/element/ElementFragment.h>
namespace facebook {
namespace react {
/*
* `Element<>` is an abstraction layer that allows describing component
* hierarchy in a declarative way. Creating `Element`s themself does not create
* a component tree (aka `ShadowNode` tree) but describes some hierarchical
* structure that might be used to build an actual component tree (similar to
* XML Elements).
* `Element` provides some basic type-safety guarantees: all modifications
* of element objects require using objects (such as Props or State) of
* compatible type.
*/
template <typename ShadowNodeT>
class Element final {
public:
using ConcreteProps = typename ShadowNodeT::ConcreteProps;
using SharedConcreteProps = std::shared_ptr<ConcreteProps const>;
using ConcreteState = typename ShadowNodeT::ConcreteState;
using SharedConcreteState = std::shared_ptr<ConcreteState const>;
using ConcreteShadowNode = ShadowNodeT;
using ConcreteUnsharedShadowNode = std::shared_ptr<ConcreteShadowNode>;
using ConcreteReferenceCallback =
std::function<void(std::shared_ptr<ShadowNodeT const> const &shadowNode)>;
/*
* Constructs an `Element`.
*/
Element() {
fragment_.componentHandle = ShadowNodeT::Handle();
fragment_.componentName = ShadowNodeT::Name();
fragment_.props = ShadowNodeT::defaultSharedProps();
}
/*
* Converts to `ElementFragment` object.
*/
operator ElementFragment() {
return fragment_;
}
/*
* Sets `tag`.
*/
Element &tag(Tag tag) {
fragment_.tag = tag;
return *this;
}
/*
* Sets `surfaceId`.
*/
Element &surfaceId(SurfaceId surfaceId) {
fragment_.surfaceId = surfaceId;
return *this;
}
/*
* Sets `props`.
*/
Element &props(SharedConcreteProps props) {
fragment_.props = props;
return *this;
}
/*
* Sets `props` using callback.
*/
Element &props(std::function<SharedConcreteProps()> callback) {
fragment_.props = callback();
return *this;
}
/*
* Sets `state`.
*/
Element &state(SharedConcreteState state) {
fragment_.state = state;
return *this;
}
/*
* Sets `state` using callback.
*/
Element &state(std::function<SharedConcreteState()> callback) {
fragment_.state = state();
return *this;
}
/*
* Sets children.
*/
Element &children(std::vector<ElementFragment> children) {
auto fragments = ElementFragment::List{};
fragments.reserve(children.size());
for (auto const &child : children) {
fragments.push_back(child);
}
fragment_.children = fragments;
return *this;
}
/*
* Calls the callback during component construction with a pointer to the
* component which is being constructed.
*/
Element &reference(
std::function<void(ConcreteUnsharedShadowNode const &shadowNode)>
callback) {
fragment_.referenceCallback = callback;
return *this;
}
/*
* During component construction, assigns a given pointer to a component
* that is being constructed.
*/
Element &reference(ConcreteUnsharedShadowNode &outShadowNode) {
fragment_.referenceCallback = [&](ShadowNode::Shared const &shadowNode) {
outShadowNode = std::const_pointer_cast<ConcreteShadowNode>(
std::static_pointer_cast<ConcreteShadowNode const>(shadowNode));
};
return *this;
}
/*
* Calls the callback with a reference to a just constructed component.
*/
Element &finalize(
std::function<void(ConcreteShadowNode &shadowNode)> finalizeCallback) {
fragment_.finalizeCallback = [=](ShadowNode &shadowNode) {
return finalizeCallback(static_cast<ConcreteShadowNode &>(shadowNode));
};
return *this;
}
private:
friend class ComponentBuilder;
ElementFragment fragment_;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,10 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ElementFragment.h"
// Intentionally empty.

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
#include <vector>
#include <react/core/ShadowNode.h>
namespace facebook {
namespace react {
/*
* This is an implementation detail, do not use it directly.
* A type-erased version of `Element<>`.
* `ElementFragment` carries all information that is stored inside `Element<>`
* in some generalized, type-erased manner.
*/
class ElementFragment final {
public:
using Shared = std::shared_ptr<ElementFragment>;
using List = std::vector<ElementFragment>;
using ListOfShared = std::vector<Shared>;
using ReferenceCallback =
std::function<void(ShadowNode::Unshared const &shadowNode)>;
using FinalizeCallback = std::function<void(ShadowNode &shadowNode)>;
/*
* ComponentDescriptor part (describes the type)
*/
ComponentHandle componentHandle;
ComponentName componentName;
/*
* ShadowNodeFamily part (describes the family)
*/
Tag tag;
SurfaceId surfaceId;
/*
* ShadowNode part (describes the instance)
*/
Props::Shared props;
State::Shared state;
List children;
/*
* Other
*/
ReferenceCallback referenceCallback;
FinalizeCallback finalizeCallback;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/components/root/RootComponentDescriptor.h>
#include <react/components/scrollview/ScrollViewComponentDescriptor.h>
#include <react/components/text/ParagraphComponentDescriptor.h>
#include <react/components/text/RawTextComponentDescriptor.h>
#include <react/components/text/TextComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/element/ComponentBuilder.h>
#include <react/uimanager/ComponentDescriptorProviderRegistry.h>
namespace facebook {
namespace react {
inline ComponentBuilder simpleComponentBuilder() {
ComponentDescriptorProviderRegistry componentDescriptorProviderRegistry{};
auto eventDispatcher = EventDispatcher::Shared{};
auto componentDescriptorRegistry =
componentDescriptorProviderRegistry.createComponentDescriptorRegistry(
ComponentDescriptorParameters{eventDispatcher, nullptr, nullptr});
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<RootComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ViewComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ScrollViewComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ParagraphComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<TextComponentDescriptor>());
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<RawTextComponentDescriptor>());
return ComponentBuilder{componentDescriptorRegistry};
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,102 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <memory>
#include <gtest/gtest.h>
#include <react/components/root/RootComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/element/ComponentBuilder.h>
#include <react/element/Element.h>
#include <react/element/testUtils.h>
#include <react/uimanager/ComponentDescriptorProviderRegistry.h>
using namespace facebook::react;
TEST(ElementTest, testNormalCases) {
auto builder = simpleComponentBuilder();
auto shadowNodeA = std::shared_ptr<RootShadowNode>{};
auto shadowNodeAA = std::shared_ptr<ViewShadowNode>{};
auto shadowNodeAB = std::shared_ptr<ViewShadowNode>{};
auto shadowNodeABA = std::shared_ptr<ViewShadowNode>{};
auto propsAA = std::make_shared<ViewProps>();
propsAA->nativeId = "node AA";
// clang-format off
auto element =
Element<RootShadowNode>()
.reference(shadowNodeA)
.tag(1)
.props([]() {
auto props = std::make_shared<RootProps>();
props->nativeId = "node A";
return props;
})
.finalize([](RootShadowNode &shadowNode){
shadowNode.sealRecursive();
})
.children({
Element<ViewShadowNode>()
.reference(shadowNodeAA)
.tag(2)
.props(propsAA),
Element<ViewShadowNode>()
.reference(shadowNodeAB)
.tag(3)
.props([]() {
auto props = std::make_shared<ViewProps>();
props->nativeId = "node AB";
return props;
})
.children({
Element<ViewShadowNode>()
.reference(shadowNodeABA)
.tag(4)
.props([]() {
auto props = std::make_shared<ViewProps>();
props->nativeId = "node ABA";
return props;
})
})
});
// clang-format on
auto shadowNode = builder.build(element);
EXPECT_EQ(shadowNode, shadowNodeA);
// Tags
EXPECT_EQ(shadowNodeA->getTag(), 1);
EXPECT_EQ(shadowNodeAA->getTag(), 2);
EXPECT_EQ(shadowNodeAB->getTag(), 3);
EXPECT_EQ(shadowNodeABA->getTag(), 4);
// Children
EXPECT_EQ(shadowNodeA->getChildren().size(), 2);
EXPECT_EQ(shadowNodeAA->getChildren().size(), 0);
EXPECT_EQ(shadowNodeAB->getChildren().size(), 1);
EXPECT_EQ(shadowNodeABA->getChildren().size(), 0);
EXPECT_EQ(
shadowNodeA->getChildren(),
(ShadowNode::ListOfShared{shadowNodeAA, shadowNodeAB}));
EXPECT_EQ(
shadowNodeAB->getChildren(), (ShadowNode::ListOfShared{shadowNodeABA}));
// Props
EXPECT_EQ(shadowNodeA->getProps()->nativeId, "node A");
EXPECT_EQ(shadowNodeABA->getProps()->nativeId, "node ABA");
EXPECT_EQ(shadowNodeAA->getProps(), propsAA);
// Finalize
EXPECT_TRUE(shadowNodeA->getSealed());
EXPECT_TRUE(shadowNodeAA->getSealed());
EXPECT_TRUE(shadowNodeAB->getSealed());
EXPECT_TRUE(shadowNodeABA->getSealed());
}