ComponentBuilder.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #include "ComponentBuilder.h"
  8. namespace facebook {
  9. namespace react {
  10. ComponentBuilder::ComponentBuilder(
  11. ComponentDescriptorRegistry::Shared const &componentDescriptorRegistry)
  12. : componentDescriptorRegistry_(componentDescriptorRegistry){};
  13. ShadowNode::Unshared ComponentBuilder::build(
  14. ElementFragment const &elementFragment) const {
  15. auto &componentDescriptor =
  16. componentDescriptorRegistry_->at(elementFragment.componentHandle);
  17. auto children = ShadowNode::ListOfShared{};
  18. children.reserve(elementFragment.children.size());
  19. for (auto const &childFragment : elementFragment.children) {
  20. children.push_back(build(childFragment));
  21. }
  22. auto family = componentDescriptor.createFamily(
  23. ShadowNodeFamilyFragment{
  24. elementFragment.tag, elementFragment.surfaceId, nullptr},
  25. nullptr);
  26. auto state = elementFragment.state
  27. ? elementFragment.state
  28. : componentDescriptor.createInitialState(
  29. ShadowNodeFragment{elementFragment.props}, family);
  30. auto constShadowNode = componentDescriptor.createShadowNode(
  31. ShadowNodeFragment{
  32. elementFragment.props,
  33. std::make_shared<ShadowNode::ListOfShared const>(children),
  34. state},
  35. family);
  36. auto shadowNode = std::const_pointer_cast<ShadowNode>(constShadowNode);
  37. if (elementFragment.referenceCallback) {
  38. elementFragment.referenceCallback(shadowNode);
  39. }
  40. if (elementFragment.finalizeCallback) {
  41. elementFragment.finalizeCallback(*shadowNode);
  42. }
  43. return shadowNode;
  44. }
  45. } // namespace react
  46. } // namespace facebook