RCTInspector.mm
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#import "RCTInspector.h"
#if RCT_DEV
#include <jschelpers/JavaScriptCore.h>
#include <jsinspector/InspectorInterfaces.h>
#import "RCTDefines.h"
#import "RCTInspectorPackagerConnection.h"
#import "RCTLog.h"
#import "RCTSRWebSocket.h"
#import "RCTUtils.h"
using namespace facebook::react;
// This is a port of the Android impl, at
// react-native-github/ReactAndroid/src/main/java/com/facebook/react/bridge/Inspector.java
// react-native-github/ReactAndroid/src/main/jni/react/jni/JInspector.cpp
// please keep consistent :)
class RemoteConnection : public IRemoteConnection {
public:
RemoteConnection(RCTInspectorRemoteConnection *connection) :
_connection(connection) {}
virtual void onMessage(std::string message) override {
[_connection onMessage:@(message.c_str())];
}
virtual void onDisconnect() override {
[_connection onDisconnect];
}
private:
const RCTInspectorRemoteConnection *_connection;
};
@interface RCTInspectorPage () {
NSInteger _id;
NSString *_title;
}
- (instancetype)initWithId:(NSInteger)id
title:(NSString *)title;
@end
@interface RCTInspectorLocalConnection () {
std::unique_ptr<ILocalConnection> _connection;
}
- (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection;
@end
static IInspector *getInstance()
{
return &facebook::react::getInspectorInstance();
}
@implementation RCTInspector
RCT_NOT_IMPLEMENTED(- (instancetype)init)
+ (NSArray<RCTInspectorPage *> *)pages
{
std::vector<InspectorPage> 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
title:@(pages[i].title.c_str())];
[array addObject:pageWrapper];
}
return array;
}
+ (RCTInspectorLocalConnection *)connectPage:(NSInteger)pageId
forRemoteConnection:(RCTInspectorRemoteConnection *)remote
{
auto localConnection = getInstance()->connect(pageId, std::make_unique<RemoteConnection>(remote));
return [[RCTInspectorLocalConnection alloc] initWithConnection:std::move(localConnection)];
}
@end
@implementation RCTInspectorPage
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (instancetype)initWithId:(NSInteger)id
title:(NSString *)title
{
if (self = [super init]) {
_id = id;
_title = title;
}
return self;
}
@end
@implementation RCTInspectorLocalConnection
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (instancetype)initWithConnection:(std::unique_ptr<ILocalConnection>)connection
{
if (self = [super init]) {
_connection = std::move(connection);
}
return self;
}
- (void)sendMessage:(NSString *)message
{
_connection->sendMessage([message UTF8String]);
}
- (void)disconnect
{
_connection->disconnect();
}
@end
#endif