Skip to content

Commit

Permalink
Add sendWrappedEventToPackager to InspectorPackagerConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudo committed Oct 8, 2024
1 parent cbc0978 commit 7afd4e6
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ - (void)sendEventToAllConnections:(NSString *)event
_cxxImpl->sendEventToAllConnections(event.UTF8String);
}

- (void)sendWrappedEventToPackager:(NSString *)event pageId:(NSString *)pageId
{
_cxxImpl->sendWrappedEventToPackager(event.UTF8String, pageId.UTF8String);
}

- (bool)isConnected
{
return _cxxImpl->isConnected();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- (void)connect;
- (void)closeQuietly;
- (void)sendEventToAllConnections:(NSString *)event;
- (void)sendWrappedEventToPackager:(NSString *)event pageId:(NSString *)pageId
@end

@interface RCTInspectorPackagerConnection : NSObject <RCTInspectorPackagerConnectionProtocol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ - (void)sendEventToAllConnections:(NSString *)event
}
}

- (void)sendWrappedEventToPackager:(NSString *)event pageId:(NSString *)pageId
{
[self sendWrappedEvent:pageId message:event];
}

- (void)closeAllConnections
{
for (NSString *pageId in _inspectorConnections) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private static native HybridData initHybrid(

public native void sendEventToAllConnections(String event);

public native void sendWrappedEventToPackager(String event, String pageId);

/** Java wrapper around a C++ IWebSocketDelegate, allowing us to call the interface from Java. */
@DoNotStrip
private static class WebSocketDelegate implements Closeable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
public void closeQuietly();

public void sendEventToAllConnections(String event);

public void sendWrappedEventToPackager(String event, String pageId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void sendEventToAllConnections(String event) {
}
}

public void sendWrappedEventToPackager(String event, String pageId) {
sendWrappedEvent(pageId, event);
}

void handleProxyMessage(JSONObject message) throws JSONException, IOException {
String event = message.getString("event");
switch (event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ void JCxxInspectorPackagerConnection::sendEventToAllConnections(
cxxImpl_.sendEventToAllConnections(event);
}

void JCxxInspectorPackagerConnection::sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId) {
cxxImpl_.sendWrappedEventToPackager(event, pageId);
}

void JCxxInspectorPackagerConnection::registerNatives() {
registerHybrid(
{makeNativeMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class JCxxInspectorPackagerConnection
void connect();
void closeQuietly();
void sendEventToAllConnections(const std::string& event);
void sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId);

private:
friend HybridBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ void InspectorPackagerConnection::Impl::sendEventToAllConnections(
}
}

void InspectorPackagerConnection::Impl::sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId) {
delegate_->scheduleCallback(
[weakSelf = weak_from_this(),
event = std::move(event),
pageId = std::move(pageId)]() {
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return;
}
strongSelf->sendToPackager(folly::dynamic::object(
"event", "wrappedEvent")(
"payload",
folly::dynamic::object("pageId", pageId)("wrappedEvent", event)));
},
0ms);
}

void InspectorPackagerConnection::Impl::closeAllConnections() {
for (auto& connection : inspectorSessions_) {
connection.second.localConnection->disconnect();
Expand Down Expand Up @@ -367,4 +386,10 @@ void InspectorPackagerConnection::sendEventToAllConnections(std::string event) {
impl_->sendEventToAllConnections(event);
}

void InspectorPackagerConnection::sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId) {
impl_->sendWrappedEventToPackager(event, pageId);
}

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class InspectorPackagerConnection {
void connect();
void closeQuietly();
void sendEventToAllConnections(std::string event);
void sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId);

private:
class Impl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class InspectorPackagerConnection::Impl
void connect();
void closeQuietly();
void sendEventToAllConnections(std::string event);
void sendWrappedEventToPackager(
const std::string& event,
const std::string& pageId);
std::unique_ptr<ILocalConnection> removeConnectionForPage(std::string pageId);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,54 @@ TEST_F(InspectorPackagerConnectionTest, TestSendEventToAllConnections) {
getInspectorInstance().removePage(pageId);
}

TEST_F(InspectorPackagerConnectionTest, TestSendWrappedEventToPackager) {
// Configure gmock to expect calls in a specific order.
InSequence mockCallsMustBeInSequence;

packagerConnection_->connect();
auto pageId = getInspectorInstance().addPage(
"mock-description",
"mock-vm",
localConnections_
.lazily_make_unique<std::unique_ptr<IRemoteConnection>>());

// Connect to the page.
webSockets_[0]->getDelegate().didReceiveMessage(sformat(
R"({{
"event": "connect",
"payload": {{
"pageId": {0}
}}
}})",
toJson(std::to_string(pageId))));
ASSERT_TRUE(localConnections_[0]);

// Send an event using sendWrappedEventToPackager and
// observe it being sent via the socket.
EXPECT_CALL(
*webSockets_[0],
send(JsonParsed(AllOf(
AtJsonPtr("/event", Eq("wrappedEvent")),
AtJsonPtr("/payload/pageId", Eq(std::to_string(pageId))),
AtJsonPtr(
"/payload/wrappedEvent",
JsonEq(
R"({{
"method": "FakeDomain.eventTriggered",
"params": ["arg1", "arg2"]
}})"))))))
.RetiresOnSaturation();
packagerConnection_->sendWrappedEventToPackager(
R"({{
"method": "FakeDomain.eventTriggered",
"params": ["arg1", "arg2"]
}})",
pageId);

EXPECT_CALL(*localConnections_[0], disconnect()).RetiresOnSaturation();
getInspectorInstance().removePage(pageId);
}

TEST_F(InspectorPackagerConnectionTest, TestConnectThenDisconnect) {
// Configure gmock to expect calls in a specific order.
InSequence mockCallsMustBeInSequence;
Expand Down

0 comments on commit 7afd4e6

Please sign in to comment.