1 #ifdef RCT_NEW_ARCH_ENABLED
2 
3 #include "NewestShadowNodesRegistry.h"
4 
5 #include <react/renderer/core/ComponentDescriptor.h>
6 #include <react/renderer/core/ShadowNodeFragment.h>
7 
8 using namespace facebook::react;
9 
10 namespace reanimated {
11 
set(ShadowNode::Shared shadowNode,Tag parentTag)12 void NewestShadowNodesRegistry::set(
13     ShadowNode::Shared shadowNode,
14     Tag parentTag) {
15   map_[shadowNode->getTag()] = std::make_pair(shadowNode, parentTag);
16 }
17 
has(const ShadowNode::Shared & shadowNode) const18 bool NewestShadowNodesRegistry::has(
19     const ShadowNode::Shared &shadowNode) const {
20   return map_.find(shadowNode->getTag()) != map_.cend();
21 }
22 
get(Tag tag) const23 ShadowNode::Shared NewestShadowNodesRegistry::get(Tag tag) const {
24   const auto it = map_.find(tag);
25   return it != map_.cend() ? it->second.first : nullptr;
26 }
27 
update(ShadowNode::Shared shadowNode)28 void NewestShadowNodesRegistry::update(ShadowNode::Shared shadowNode) {
29   const auto it = map_.find(shadowNode->getTag());
30   react_native_assert(it != map_.cend());
31   it->second.first = shadowNode;
32 }
33 
remove(Tag tag)34 void NewestShadowNodesRegistry::remove(Tag tag) {
35   if (map_.find(tag) == map_.cend()) {
36     return;
37   }
38 
39   auto shadowNode = map_[tag].first;
40 
41   while (shadowNode != nullptr) {
42     bool hasAnyChildInMap = false;
43     for (const auto &child : shadowNode->getChildren()) {
44       if (has(child)) {
45         hasAnyChildInMap = true;
46         break;
47       }
48     }
49 
50     if (hasAnyChildInMap) {
51       break;
52     }
53 
54     auto it = map_.find(shadowNode->getTag());
55     Tag parentTag = it->second.second;
56     map_.erase(it);
57 
58     shadowNode = map_[parentTag].first;
59   }
60 }
61 
createLock() const62 std::lock_guard<std::mutex> NewestShadowNodesRegistry::createLock() const {
63   return std::lock_guard<std::mutex>(mutex_);
64 }
65 
66 } // namespace reanimated
67 
68 #endif // RCT_NEW_ARCH_ENABLED
69