mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Refactor InspectorImpl internal page data structure (#42304)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42304 Changelog: [Internal] Light refactor of `InspectorImpl`'s storage from two separate maps (one of them with tuples for values!) to a single map of objects. Reviewed By: hoxyq Differential Revision: D52786335 fbshipit-source-id: a49466ed7189fd032e486319bbdf77097a30885f
This commit is contained in:
committed by
Facebook GitHub Bot
parent
0ea8e1dd8f
commit
f30f867173
@@ -67,7 +67,7 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init)
|
||||
|
||||
+ (NSArray<RCTInspectorPage *> *)pages
|
||||
{
|
||||
std::vector<InspectorPage> pages = getInstance()->getPages();
|
||||
std::vector<InspectorPageDescription> pages = getInstance()->getPages();
|
||||
NSMutableArray<RCTInspectorPage *> *array = [NSMutableArray arrayWithCapacity:pages.size()];
|
||||
for (size_t i = 0; i < pages.size(); i++) {
|
||||
RCTInspectorPage *pageWrapper = [[RCTInspectorPage alloc] initWithId:pages[i].id
|
||||
|
||||
@@ -81,7 +81,8 @@ jni::global_ref<JInspector::javaobject> JInspector::instance(
|
||||
}
|
||||
|
||||
jni::local_ref<jni::JArrayClass<JPage::javaobject>> JInspector::getPages() {
|
||||
std::vector<jsinspector_modern::InspectorPage> pages = inspector_->getPages();
|
||||
std::vector<jsinspector_modern::InspectorPageDescription> pages =
|
||||
inspector_->getPages();
|
||||
auto array = jni::JArrayClass<JPage::javaobject>::newArray(pages.size());
|
||||
for (size_t i = 0; i < pages.size(); i++) {
|
||||
(*array)[i] = JPage::create(pages[i].id, pages[i].title, pages[i].vm);
|
||||
|
||||
+2
-2
@@ -23,13 +23,13 @@ namespace inspector_modern {
|
||||
namespace chrome {
|
||||
|
||||
using ::facebook::react::jsinspector_modern::IInspector;
|
||||
using ::facebook::react::jsinspector_modern::InspectorPage;
|
||||
using ::facebook::react::jsinspector_modern::InspectorPageDescription;
|
||||
using ::facebook::react::jsinspector_modern::IRemoteConnection;
|
||||
|
||||
namespace {
|
||||
|
||||
std::unordered_map<int, std::string> makePageMap(
|
||||
const std::vector<InspectorPage>& pages) {
|
||||
const std::vector<InspectorPageDescription>& pages) {
|
||||
std::unordered_map<int, std::string> pageMap;
|
||||
|
||||
for (auto& page : pages) {
|
||||
|
||||
@@ -31,18 +31,53 @@ class InspectorImpl : public IInspector {
|
||||
ConnectFunc connectFunc) override;
|
||||
void removePage(int pageId) override;
|
||||
|
||||
std::vector<InspectorPage> getPages() const override;
|
||||
std::vector<InspectorPageDescription> getPages() const override;
|
||||
std::unique_ptr<ILocalConnection> connect(
|
||||
int pageId,
|
||||
std::unique_ptr<IRemoteConnection> remote) override;
|
||||
|
||||
private:
|
||||
class Page {
|
||||
public:
|
||||
Page(
|
||||
int id,
|
||||
const std::string& title,
|
||||
const std::string& vm,
|
||||
ConnectFunc connectFunc);
|
||||
operator InspectorPageDescription() const;
|
||||
|
||||
ConnectFunc getConnectFunc() const;
|
||||
|
||||
private:
|
||||
int id_;
|
||||
std::string title_;
|
||||
std::string vm_;
|
||||
ConnectFunc connectFunc_;
|
||||
};
|
||||
mutable std::mutex mutex_;
|
||||
int nextPageId_{1};
|
||||
std::unordered_map<int, std::tuple<std::string, std::string>> titles_;
|
||||
std::unordered_map<int, ConnectFunc> connectFuncs_;
|
||||
std::unordered_map<int, Page> pages_;
|
||||
};
|
||||
|
||||
InspectorImpl::Page::Page(
|
||||
int id,
|
||||
const std::string& title,
|
||||
const std::string& vm,
|
||||
ConnectFunc connectFunc)
|
||||
: id_(id), title_(title), vm_(vm), connectFunc_(std::move(connectFunc)) {}
|
||||
|
||||
InspectorImpl::Page::operator InspectorPageDescription() const {
|
||||
return InspectorPageDescription{
|
||||
.id = id_,
|
||||
.title = title_,
|
||||
.vm = vm_,
|
||||
};
|
||||
}
|
||||
|
||||
InspectorImpl::ConnectFunc InspectorImpl::Page::getConnectFunc() const {
|
||||
return connectFunc_;
|
||||
}
|
||||
|
||||
int InspectorImpl::addPage(
|
||||
const std::string& title,
|
||||
const std::string& vm,
|
||||
@@ -50,8 +85,7 @@ int InspectorImpl::addPage(
|
||||
std::scoped_lock lock(mutex_);
|
||||
|
||||
int pageId = nextPageId_++;
|
||||
titles_[pageId] = std::make_tuple(title, vm);
|
||||
connectFuncs_[pageId] = std::move(connectFunc);
|
||||
pages_.emplace(pageId, Page{pageId, title, vm, std::move(connectFunc)});
|
||||
|
||||
return pageId;
|
||||
}
|
||||
@@ -59,17 +93,15 @@ int InspectorImpl::addPage(
|
||||
void InspectorImpl::removePage(int pageId) {
|
||||
std::scoped_lock lock(mutex_);
|
||||
|
||||
titles_.erase(pageId);
|
||||
connectFuncs_.erase(pageId);
|
||||
pages_.erase(pageId);
|
||||
}
|
||||
|
||||
std::vector<InspectorPage> InspectorImpl::getPages() const {
|
||||
std::vector<InspectorPageDescription> InspectorImpl::getPages() const {
|
||||
std::scoped_lock lock(mutex_);
|
||||
|
||||
std::vector<InspectorPage> inspectorPages;
|
||||
for (auto& it : titles_) {
|
||||
inspectorPages.push_back(InspectorPage{
|
||||
it.first, std::get<0>(it.second), std::get<1>(it.second)});
|
||||
std::vector<InspectorPageDescription> inspectorPages;
|
||||
for (auto& it : pages_) {
|
||||
inspectorPages.push_back(InspectorPageDescription(it.second));
|
||||
}
|
||||
|
||||
return inspectorPages;
|
||||
@@ -83,9 +115,9 @@ std::unique_ptr<ILocalConnection> InspectorImpl::connect(
|
||||
{
|
||||
std::scoped_lock lock(mutex_);
|
||||
|
||||
auto it = connectFuncs_.find(pageId);
|
||||
if (it != connectFuncs_.end()) {
|
||||
connectFunc = it->second;
|
||||
auto it = pages_.find(pageId);
|
||||
if (it != pages_.end()) {
|
||||
connectFunc = it->second.getConnectFunc();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,12 +31,15 @@ class IDestructible {
|
||||
virtual ~IDestructible() = 0;
|
||||
};
|
||||
|
||||
struct InspectorPage {
|
||||
struct InspectorPageDescription {
|
||||
const int id;
|
||||
const std::string title;
|
||||
const std::string vm;
|
||||
};
|
||||
|
||||
// Alias for backwards compatibility.
|
||||
using InspectorPage = InspectorPageDescription;
|
||||
|
||||
/// IRemoteConnection allows the VM to send debugger messages to the client.
|
||||
class JSINSPECTOR_EXPORT IRemoteConnection : public IDestructible {
|
||||
public:
|
||||
@@ -72,7 +75,7 @@ class JSINSPECTOR_EXPORT IInspector : public IDestructible {
|
||||
virtual void removePage(int pageId) = 0;
|
||||
|
||||
/// getPages is called by the client to list all debuggable pages.
|
||||
virtual std::vector<InspectorPage> getPages() const = 0;
|
||||
virtual std::vector<InspectorPageDescription> getPages() const = 0;
|
||||
|
||||
/// connect is called by the client to initiate a debugging session on the
|
||||
/// given page.
|
||||
|
||||
Reference in New Issue
Block a user