mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-22 19:48:56 +08:00
move page registration logic in to jsinspector
Reviewed By: pakoito Differential Revision: D6531199 fbshipit-source-id: ed1ae9e2f0c19e7656cd022e438693798320e55a
This commit is contained in:
committed by
Facebook Github Bot
parent
48019a0c2a
commit
bef7967f9a
@@ -9,6 +9,9 @@
|
||||
|
||||
#include "InspectorInterfaces.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
@@ -19,5 +22,80 @@ IDestructible::~IDestructible() { }
|
||||
ILocalConnection::~ILocalConnection() { }
|
||||
IRemoteConnection::~IRemoteConnection() { }
|
||||
|
||||
namespace {
|
||||
|
||||
class InspectorImpl : public IInspector {
|
||||
public:
|
||||
int addPage(const std::string& title, ConnectFunc connectFunc) override;
|
||||
void removePage(int pageId) override;
|
||||
|
||||
std::vector<InspectorPage> getPages() const override;
|
||||
std::unique_ptr<ILocalConnection> connect(
|
||||
int pageId,
|
||||
std::unique_ptr<IRemoteConnection> remote) override;
|
||||
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
int nextPageId_{1};
|
||||
std::unordered_map<int, std::string> titles_;
|
||||
std::unordered_map<int, ConnectFunc> connectFuncs_;
|
||||
};
|
||||
|
||||
int InspectorImpl::addPage(const std::string& title, ConnectFunc connectFunc) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
int pageId = nextPageId_++;
|
||||
titles_[pageId] = title;
|
||||
connectFuncs_[pageId] = std::move(connectFunc);
|
||||
|
||||
return pageId;
|
||||
}
|
||||
|
||||
void InspectorImpl::removePage(int pageId) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
titles_.erase(pageId);
|
||||
connectFuncs_.erase(pageId);
|
||||
}
|
||||
|
||||
std::vector<InspectorPage> InspectorImpl::getPages() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
std::vector<InspectorPage> inspectorPages;
|
||||
for (auto& it : titles_) {
|
||||
inspectorPages.push_back(InspectorPage{it.first, it.second});
|
||||
}
|
||||
|
||||
return inspectorPages;
|
||||
}
|
||||
|
||||
std::unique_ptr<ILocalConnection> InspectorImpl::connect(
|
||||
int pageId,
|
||||
std::unique_ptr<IRemoteConnection> remote) {
|
||||
IInspector::ConnectFunc connectFunc;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto it = connectFuncs_.find(pageId);
|
||||
if (it != connectFuncs_.end()) {
|
||||
connectFunc = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
return connectFunc ? connectFunc(std::move(remote)) : nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
IInspector& getInspectorInstance() {
|
||||
static InspectorImpl instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
std::unique_ptr<IInspector> makeTestInspectorInstance() {
|
||||
return std::make_unique<InspectorImpl>();
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
||||
Reference in New Issue
Block a user