StubViewTree.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "StubViewTree.h"
  8. namespace facebook {
  9. namespace react {
  10. StubViewTree::StubViewTree(ShadowView const &shadowView) {
  11. auto view = std::make_shared<StubView>();
  12. view->update(shadowView);
  13. rootTag = shadowView.tag;
  14. registry[shadowView.tag] = view;
  15. }
  16. StubView const &StubViewTree::getRootStubView() const {
  17. return *registry.at(rootTag);
  18. }
  19. void StubViewTree::mutate(ShadowViewMutationList const &mutations) {
  20. for (auto const &mutation : mutations) {
  21. switch (mutation.type) {
  22. case ShadowViewMutation::Create: {
  23. assert(mutation.parentShadowView == ShadowView{});
  24. assert(mutation.oldChildShadowView == ShadowView{});
  25. auto stubView = std::make_shared<StubView>();
  26. auto tag = mutation.newChildShadowView.tag;
  27. assert(registry.find(tag) == registry.end());
  28. registry[tag] = stubView;
  29. break;
  30. }
  31. case ShadowViewMutation::Delete: {
  32. assert(mutation.parentShadowView == ShadowView{});
  33. assert(mutation.newChildShadowView == ShadowView{});
  34. auto tag = mutation.oldChildShadowView.tag;
  35. assert(registry.find(tag) != registry.end());
  36. registry.erase(tag);
  37. break;
  38. }
  39. case ShadowViewMutation::Insert: {
  40. assert(mutation.oldChildShadowView == ShadowView{});
  41. auto parentTag = mutation.parentShadowView.tag;
  42. assert(registry.find(parentTag) != registry.end());
  43. auto parentStubView = registry[parentTag];
  44. auto childTag = mutation.newChildShadowView.tag;
  45. assert(registry.find(childTag) != registry.end());
  46. auto childStubView = registry[childTag];
  47. childStubView->update(mutation.newChildShadowView);
  48. parentStubView->children.insert(
  49. parentStubView->children.begin() + mutation.index, childStubView);
  50. break;
  51. }
  52. case ShadowViewMutation::Remove: {
  53. assert(mutation.newChildShadowView == ShadowView{});
  54. auto parentTag = mutation.parentShadowView.tag;
  55. assert(registry.find(parentTag) != registry.end());
  56. auto parentStubView = registry[parentTag];
  57. auto childTag = mutation.oldChildShadowView.tag;
  58. assert(registry.find(childTag) != registry.end());
  59. auto childStubView = registry[childTag];
  60. assert(
  61. parentStubView->children[mutation.index]->tag ==
  62. childStubView->tag);
  63. parentStubView->children.erase(
  64. parentStubView->children.begin() + mutation.index);
  65. break;
  66. }
  67. case ShadowViewMutation::Update: {
  68. assert(
  69. mutation.newChildShadowView.tag == mutation.oldChildShadowView.tag);
  70. assert(
  71. registry.find(mutation.newChildShadowView.tag) != registry.end());
  72. auto stubView = registry[mutation.newChildShadowView.tag];
  73. stubView->update(mutation.newChildShadowView);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. bool operator==(StubViewTree const &lhs, StubViewTree const &rhs) {
  80. if (lhs.registry.size() != rhs.registry.size()) {
  81. return false;
  82. }
  83. for (auto const &pair : lhs.registry) {
  84. auto &lhsStubView = *lhs.registry.at(pair.first);
  85. auto &rhsStubView = *rhs.registry.at(pair.first);
  86. if (lhsStubView != rhsStubView) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. bool operator!=(StubViewTree const &lhs, StubViewTree const &rhs) {
  93. return !(lhs == rhs);
  94. }
  95. } // namespace react
  96. } // namespace facebook