Skip to content

Commit

Permalink
Force the debugger to disconnect before a bundle reload
Browse files Browse the repository at this point in the history
Reviewed By: bnham

Differential Revision: D5594238

fbshipit-source-id: feff9f179534c8e617f8fa7c8a7b1bc525c07cae
  • Loading branch information
pakoito authored and facebook-github-bot committed Aug 14, 2017
1 parent f11f001 commit 4150410
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Libraries/WebSocket/RCTReconnectingWebSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

- (instancetype)initWithURL:(NSURL *)url;
@property (nonatomic, weak) id<RCTWebSocketProtocolDelegate> delegate;
/** @brief Must be set before -start to have effect */
@property (nonatomic, strong) dispatch_queue_t delegateDispatchQueue;
- (void)send:(id)data;
- (void)start;
- (void)stop;
Expand Down
4 changes: 3 additions & 1 deletion Libraries/WebSocket/RCTReconnectingWebSocket.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ - (void)start
[self stop];
_socket = [[RCTSRWebSocket alloc] initWithURL:_url];
_socket.delegate = self;

if (_delegateDispatchQueue) {
[_socket setDelegateDispatchQueue:_delegateDispatchQueue];
}
[_socket open];
}

Expand Down
1 change: 1 addition & 0 deletions Libraries/WebSocket/RCTWebSocketObserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@interface RCTWebSocketObserver : NSObject

- (instancetype)initWithURL:(NSURL *)url;
- (void)setDelegateDispatchQueue:(dispatch_queue_t)queue;

@property (nonatomic, weak) id<RCTWebSocketObserverDelegate> delegate;

Expand Down
5 changes: 5 additions & 0 deletions Libraries/WebSocket/RCTWebSocketObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ - (instancetype)initWithURL:(NSURL *)url
return self;
}

- (void)setDelegateDispatchQueue:(dispatch_queue_t)queue
{
[_socket setDelegateDispatchQueue:queue];
}

- (void)start
{
_socket.delegate = self;
Expand Down
6 changes: 6 additions & 0 deletions React/Base/RCTBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTInspectorDevServerHelper.h"
#import "RCTJSEnvironment.h"
#import "RCTLog.h"
#import "RCTModuleData.h"
Expand Down Expand Up @@ -252,6 +253,11 @@ - (void)whitelistedModulesDidChange

- (void)reload
{
#if ENABLE_INSPECTOR
// Disable debugger to resume the JsVM & avoid thread locks while reloading
[RCTInspectorDevServerHelper disableDebugger];
#endif

/**
* Any thread
*/
Expand Down
1 change: 1 addition & 0 deletions React/DevSupport/RCTInspectorDevServerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

+ (void)connectForContext:(JSGlobalContextRef)context
withBundleURL:(NSURL *)bundleURL;
+ (void)disableDebugger;
@end

#endif
17 changes: 16 additions & 1 deletion React/DevSupport/RCTInspectorDevServerHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using namespace facebook::react;

static NSString *const kDebuggerMsgDisable = @"{ \"id\":1,\"method\":\"Debugger.disable\" }";

static NSString *getDebugServerHost(NSURL *bundleURL)
{
NSString *host = [bundleURL host];
Expand Down Expand Up @@ -43,6 +45,20 @@ @implementation RCTInspectorDevServerHelper

RCT_NOT_IMPLEMENTED(- (instancetype)init)

static NSMutableDictionary<NSString *, RCTInspectorPackagerConnection *> *socketConnections = nil;

static void sendEventToAllConnections(NSString *event)
{
for (NSString *socketId in socketConnections) {
[socketConnections[socketId] sendEventToAllConnections:event];
}
}

+ (void)disableDebugger
{
sendEventToAllConnections(kDebuggerMsgDisable);
}

+ (void)connectForContext:(JSGlobalContextRef)context
withBundleURL:(NSURL *)bundleURL
{
Expand All @@ -55,7 +71,6 @@ + (void)connectForContext:(JSGlobalContextRef)context
// Note, using a static dictionary isn't really the greatest design, but
// the packager connection does the same thing, so it's at least consistent.
// This is a static map that holds different inspector clients per the inspectorURL
static NSMutableDictionary<NSString *, RCTInspectorPackagerConnection *> *socketConnections = nil;
if (socketConnections == nil) {
socketConnections = [NSMutableDictionary new];
}
Expand Down
1 change: 1 addition & 0 deletions React/Inspector/RCTInspectorPackagerConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- (instancetype)initWithURL:(NSURL *)url;
- (void)connect;
- (void)closeQuietly;
- (void)sendEventToAllConnections:(NSString *)event;
- (void)sendOpenEvent:(NSString *)pageId;
@end

Expand Down
7 changes: 7 additions & 0 deletions React/Inspector/RCTInspectorPackagerConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ - (void)handleProxyMessage:(NSDictionary<NSString *, id> *)message
}
}

- (void)sendEventToAllConnections:(NSString *)event
{
for (NSString *pageId in _inspectorConnections) {
[_inspectorConnections[pageId] sendMessage:event];
}
}

- (void)closeAllConnections
{
for (NSString *pageId in _inspectorConnections){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class DevServerHelper {
private static final int LONG_POLL_FAILURE_DELAY_MS = 5000;
private static final int HTTP_CONNECT_TIMEOUT_MS = 5000;

private static final String DEBUGGER_MSG_DISABLE = "{ \"id\":1,\"method\":\"Debugger.disable\" }";

public interface OnServerContentChangeListener {
void onServerContentChanged();
}
Expand Down Expand Up @@ -211,6 +213,18 @@ public void openInspector(String id) {
}
}

public void sendEventToAllConnections(String event) {
if (mInspectorPackagerConnection != null) {
mInspectorPackagerConnection.sendEventToAllConnections(event);
}
}

public void disableDebugger() {
if (mInspectorPackagerConnection != null) {
mInspectorPackagerConnection.sendEventToAllConnections(DEBUGGER_MSG_DISABLE);
}
}

public void closeInspectorConnection() {
new AsyncTask<Void, Void, Void>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ public void isPackagerRunning(PackagerStatusCallback callback) {

@Override
public void onPackagerReloadCommand() {
// Disable debugger to resume the JsVM & avoid thread locks while reloading
mDevServerHelper.disableDebugger();
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import android.os.AsyncTask;
Expand Down Expand Up @@ -47,6 +48,14 @@ public void closeQuietly() {
mConnection.close();
}

public void sendEventToAllConnections(String event) {
for (Map.Entry<String, Inspector.LocalConnection> inspectorConnectionEntry :
mInspectorConnections.entrySet()) {
Inspector.LocalConnection inspectorConnection = inspectorConnectionEntry.getValue();
inspectorConnection.sendMessage(event);
}
}

public void sendOpenEvent(String pageId) {
try {
JSONObject payload = makePageIdPayload(pageId);
Expand Down

0 comments on commit 4150410

Please sign in to comment.