stubs.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 "stubs.h"
  8. #include <react/core/LayoutableShadowNode.h>
  9. #include <react/core/ShadowNodeFragment.h>
  10. #include <react/mounting/Differentiator.h>
  11. namespace facebook {
  12. namespace react {
  13. /*
  14. * Generates `create` and `insert` instructions recursively traversing a shadow
  15. * tree.
  16. * This is a trivial implementation of diffing algorithm that can only "diff"
  17. * an empty tree with some other one.
  18. */
  19. static void calculateShadowViewMutationsForNewTree(
  20. ShadowViewMutation::List &mutations,
  21. ShadowView const &parentShadowView,
  22. ShadowViewNodePair::List const &newChildPairs) {
  23. for (auto index = 0; index < newChildPairs.size(); index++) {
  24. auto const &newChildPair = newChildPairs[index];
  25. mutations.push_back(
  26. ShadowViewMutation::CreateMutation(newChildPair.shadowView));
  27. mutations.push_back(ShadowViewMutation::InsertMutation(
  28. parentShadowView, newChildPair.shadowView, index));
  29. auto const newGrandChildPairs =
  30. sliceChildShadowNodeViewPairs(*newChildPair.shadowNode);
  31. calculateShadowViewMutationsForNewTree(
  32. mutations, newChildPair.shadowView, newGrandChildPairs);
  33. }
  34. }
  35. StubViewTree stubViewTreeFromShadowNode(ShadowNode const &rootShadowNode) {
  36. auto mutations = ShadowViewMutation::List{};
  37. mutations.reserve(256);
  38. calculateShadowViewMutationsForNewTree(
  39. mutations,
  40. ShadowView(rootShadowNode),
  41. sliceChildShadowNodeViewPairs(rootShadowNode));
  42. auto emptyRootShadowNode = rootShadowNode.clone(
  43. ShadowNodeFragment{ShadowNodeFragment::propsPlaceholder(),
  44. ShadowNode::emptySharedShadowNodeSharedList()});
  45. auto stubViewTree = StubViewTree(ShadowView(*emptyRootShadowNode));
  46. stubViewTree.mutate(mutations);
  47. return stubViewTree;
  48. }
  49. } // namespace react
  50. } // namespace facebook