9
9
10
10
#include " InspectorInterfaces.h"
11
11
12
+ #include < mutex>
13
+ #include < unordered_map>
14
+
12
15
namespace facebook {
13
16
namespace react {
14
17
@@ -19,5 +22,80 @@ IDestructible::~IDestructible() { }
19
22
ILocalConnection::~ILocalConnection () { }
20
23
IRemoteConnection::~IRemoteConnection () { }
21
24
25
+ namespace {
26
+
27
+ class InspectorImpl : public IInspector {
28
+ public:
29
+ int addPage (const std::string& title, ConnectFunc connectFunc) override ;
30
+ void removePage (int pageId) override ;
31
+
32
+ std::vector<InspectorPage> getPages () const override ;
33
+ std::unique_ptr<ILocalConnection> connect (
34
+ int pageId,
35
+ std::unique_ptr<IRemoteConnection> remote) override ;
36
+
37
+ private:
38
+ mutable std::mutex mutex_;
39
+ int nextPageId_{1 };
40
+ std::unordered_map<int , std::string> titles_;
41
+ std::unordered_map<int , ConnectFunc> connectFuncs_;
42
+ };
43
+
44
+ int InspectorImpl::addPage (const std::string& title, ConnectFunc connectFunc) {
45
+ std::lock_guard<std::mutex> lock (mutex_);
46
+
47
+ int pageId = nextPageId_++;
48
+ titles_[pageId] = title;
49
+ connectFuncs_[pageId] = std::move (connectFunc);
50
+
51
+ return pageId;
52
+ }
53
+
54
+ void InspectorImpl::removePage (int pageId) {
55
+ std::lock_guard<std::mutex> lock (mutex_);
56
+
57
+ titles_.erase (pageId);
58
+ connectFuncs_.erase (pageId);
59
+ }
60
+
61
+ std::vector<InspectorPage> InspectorImpl::getPages () const {
62
+ std::lock_guard<std::mutex> lock (mutex_);
63
+
64
+ std::vector<InspectorPage> inspectorPages;
65
+ for (auto & it : titles_) {
66
+ inspectorPages.push_back (InspectorPage{it.first , it.second });
67
+ }
68
+
69
+ return inspectorPages;
70
+ }
71
+
72
+ std::unique_ptr<ILocalConnection> InspectorImpl::connect (
73
+ int pageId,
74
+ std::unique_ptr<IRemoteConnection> remote) {
75
+ IInspector::ConnectFunc connectFunc;
76
+
77
+ {
78
+ std::lock_guard<std::mutex> lock (mutex_);
79
+
80
+ auto it = connectFuncs_.find (pageId);
81
+ if (it != connectFuncs_.end ()) {
82
+ connectFunc = it->second ;
83
+ }
84
+ }
85
+
86
+ return connectFunc ? connectFunc (std::move (remote)) : nullptr ;
87
+ }
88
+
89
+ } // namespace
90
+
91
+ IInspector& getInspectorInstance () {
92
+ static InspectorImpl instance;
93
+ return instance;
22
94
}
95
+
96
+ std::unique_ptr<IInspector> makeTestInspectorInstance () {
97
+ return std::make_unique<InspectorImpl>();
23
98
}
99
+
100
+ } // namespace react
101
+ } // namespace facebook
0 commit comments