|
| 1 | +// Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | + |
| 3 | +// This source code is licensed under the MIT license found in the |
| 4 | +// LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +#include "StubViewTree.h" |
| 7 | + |
| 8 | +namespace facebook { |
| 9 | +namespace react { |
| 10 | + |
| 11 | +StubViewTree::StubViewTree(ShadowView const &shadowView) { |
| 12 | + auto view = std::make_shared<StubView>(); |
| 13 | + view->update(shadowView); |
| 14 | + registry[shadowView.tag] = view; |
| 15 | +} |
| 16 | + |
| 17 | +void StubViewTree::mutate(ShadowViewMutationList const &mutations) { |
| 18 | + for (auto const &mutation : mutations) { |
| 19 | + switch (mutation.type) { |
| 20 | + case ShadowViewMutation::Create: { |
| 21 | + assert(mutation.parentShadowView == ShadowView{}); |
| 22 | + assert(mutation.oldChildShadowView == ShadowView{}); |
| 23 | + auto stubView = std::make_shared<StubView>(); |
| 24 | + auto tag = mutation.newChildShadowView.tag; |
| 25 | + assert(registry.find(tag) == registry.end()); |
| 26 | + registry[tag] = stubView; |
| 27 | + break; |
| 28 | + } |
| 29 | + |
| 30 | + case ShadowViewMutation::Delete: { |
| 31 | + assert(mutation.parentShadowView == ShadowView{}); |
| 32 | + assert(mutation.newChildShadowView == ShadowView{}); |
| 33 | + auto tag = mutation.oldChildShadowView.tag; |
| 34 | + assert(registry.find(tag) != registry.end()); |
| 35 | + registry.erase(tag); |
| 36 | + break; |
| 37 | + } |
| 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 | + |
| 53 | + case ShadowViewMutation::Remove: { |
| 54 | + assert(mutation.newChildShadowView == ShadowView{}); |
| 55 | + auto parentTag = mutation.parentShadowView.tag; |
| 56 | + assert(registry.find(parentTag) != registry.end()); |
| 57 | + auto parentStubView = registry[parentTag]; |
| 58 | + auto childTag = mutation.oldChildShadowView.tag; |
| 59 | + assert(registry.find(childTag) != registry.end()); |
| 60 | + auto childStubView = registry[childTag]; |
| 61 | + assert( |
| 62 | + parentStubView->children[mutation.index]->tag == |
| 63 | + childStubView->tag); |
| 64 | + parentStubView->children.erase( |
| 65 | + parentStubView->children.begin() + mutation.index); |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + case ShadowViewMutation::Update: { |
| 70 | + assert( |
| 71 | + mutation.newChildShadowView.tag == mutation.oldChildShadowView.tag); |
| 72 | + assert( |
| 73 | + registry.find(mutation.newChildShadowView.tag) != registry.end()); |
| 74 | + auto stubView = registry[mutation.newChildShadowView.tag]; |
| 75 | + stubView->update(mutation.newChildShadowView); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +bool operator==(StubViewTree const &lhs, StubViewTree const &rhs) { |
| 83 | + if (lhs.registry.size() != rhs.registry.size()) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + for (auto const &pair : lhs.registry) { |
| 88 | + auto &lhsStubView = *lhs.registry.at(pair.first); |
| 89 | + auto &rhsStubView = *rhs.registry.at(pair.first); |
| 90 | + |
| 91 | + if (lhsStubView != rhsStubView) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | +} |
| 98 | + |
| 99 | +bool operator!=(StubViewTree const &lhs, StubViewTree const &rhs) { |
| 100 | + return !(lhs == rhs); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace react |
| 104 | +} // namespace facebook |
0 commit comments