Skip to content

Commit

Permalink
Propose to connect metro server programmatically (#31828)
Browse files Browse the repository at this point in the history
Summary:
nowadays, we could only specify metro server by either dev settings or building time ip.txt.
this pr adds a new way to specify metro server programmatically which makes rn launcher or testing more feasible.

## Changelog

[Internal] [iOS] [Added] - Propose to connect metro server programmatically

Pull Request resolved: #31828

Test Plan:
just exposing a public interface for RCTPackagerConnection without much code change.
test to call this interface success locally.

Reviewed By: sammy-SC

Differential Revision: D30878774

Pulled By: yungsters

fbshipit-source-id: 5f1d6a4835a983abde7e095d20153e4ba2146a61
  • Loading branch information
Kudo authored and facebook-github-bot committed Sep 22, 2021
1 parent 398595e commit e2b5b65
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions React/DevSupport/RCTPackagerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ typedef void (^RCTConnectedHandler)(void);
/** Disconnects and removes all handlers. */
- (void)stop;

/** Reconnect with given packager server. */
- (void)reconnect:(NSString *)packagerServerHostPort;

/**
* Historically no distinction was made between notification and request
* handlers. If you use this method, it will be registered as *both* a
Expand Down
14 changes: 9 additions & 5 deletions React/DevSupport/RCTPackagerConnection.mm
Original file line number Diff line number Diff line change
Expand Up @@ -125,28 +125,32 @@ - (void)stop
_requestRegistrations.clear();
}

- (void)bundleURLSettingsChanged
- (void)reconnect:(NSString *)packagerServerHostPort
{
std::lock_guard<std::mutex> l(_mutex);
if (_socket == nil) {
return; // already stopped
}

NSString *const serverHostPort = [[RCTBundleURLProvider sharedSettings] packagerServerHostPort];
NSString *const serverScheme = [[RCTBundleURLProvider sharedSettings] packagerScheme];
if ([serverHostPort isEqual:_serverHostPortForSocket] && [serverScheme isEqual:_serverSchemeForSocket]) {
if ([packagerServerHostPort isEqual:_serverHostPortForSocket] && [serverScheme isEqual:_serverSchemeForSocket]) {
return; // unchanged
}

_socket.delegate = nil;
[_socket stop];
_serverHostPortForSocket = serverHostPort;
_serverHostPortForSocket = packagerServerHostPort;
_serverSchemeForSocket = serverScheme;
_socket = socketForLocation(serverHostPort, serverScheme);
_socket = socketForLocation(packagerServerHostPort, serverScheme);
_socket.delegate = self;
[_socket start];
}

- (void)bundleURLSettingsChanged
{
[self reconnect:[[RCTBundleURLProvider sharedSettings] packagerServerHostPort]];
}

- (RCTHandlerToken)addNotificationHandler:(RCTNotificationHandler)handler
queue:(dispatch_queue_t)queue
forMethod:(NSString *)method
Expand Down

0 comments on commit e2b5b65

Please sign in to comment.