Add callback for Connection/Disconnection to Metro

Reviewed By: bnham

Differential Revision: D6447126

fbshipit-source-id: d4c8a4dfb65c2a378f7fe696c8617ff5f3c6cb68
This commit is contained in:
Paco Estevez Garcia 2017-12-07 09:31:18 -08:00 committed by Facebook Github Bot
parent 8547b7e111
commit 3d5dc872a4
4 changed files with 55 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import com.facebook.react.modules.systeminfo.AndroidInfoHelpers;
import com.facebook.react.packagerconnection.FileIoHandler;
import com.facebook.react.packagerconnection.JSPackagerClient;
import com.facebook.react.packagerconnection.NotificationOnlyHandler;
import com.facebook.react.packagerconnection.ReconnectingWebSocket.ConnectionCallback;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.packagerconnection.RequestOnlyHandler;
import com.facebook.react.packagerconnection.Responder;
@ -92,6 +93,8 @@ public class DevServerHelper {
}
public interface PackagerCommandListener {
void onPackagerConnected();
void onPackagerDisconnected();
void onPackagerReloadCommand();
void onPackagerDevMenuCommand();
void onCaptureHeapCommand(final Responder responder);
@ -163,10 +166,24 @@ public class DevServerHelper {
});
handlers.putAll(new FileIoHandler().handlers());
ConnectionCallback onPackagerConnectedCallback =
new ConnectionCallback() {
@Override
public void onConnected() {
commandListener.onPackagerConnected();
}
@Override
public void onDisconnected() {
commandListener.onPackagerDisconnected();
}
};
mPackagerClient = new JSPackagerClient(
clientId,
mSettings.getPackagerConnectionSettings(),
handlers);
handlers,
onPackagerConnectedCallback);
mPackagerClient.init();
return null;

View File

@ -723,6 +723,16 @@ public class DevSupportManagerImpl implements
return mLastErrorStack;
}
@Override
public void onPackagerConnected() {
// No-op
}
@Override
public void onPackagerDisconnected() {
// No-op
}
@Override
public void onPackagerReloadCommand() {
// Disable debugger to resume the JsVM & avoid thread locks while reloading

View File

@ -9,6 +9,7 @@
package com.facebook.react.packagerconnection;
import java.util.Map;
import javax.annotation.Nullable;
import android.net.Uri;
@ -63,6 +64,13 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
private Map<String, RequestHandler> mRequestHandlers;
public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Map<String, RequestHandler> requestHandlers) {
this(clientId, settings, requestHandlers, null);
}
public JSPackagerClient(
String clientId, PackagerConnectionSettings settings,
Map<String, RequestHandler> requestHandlers,
@Nullable ReconnectingWebSocket.ConnectionCallback connectionCallback) {
super();
Uri.Builder builder = new Uri.Builder();
@ -74,7 +82,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
.appendQueryParameter("clientid", clientId);
String url = builder.build().toString();
mWebSocket = new ReconnectingWebSocket(url, this, null);
mWebSocket = new ReconnectingWebSocket(url, this, connectionCallback);
mRequestHandlers = requestHandlers;
}

View File

@ -9,6 +9,8 @@
package com.facebook.react.packagerconnection;
import com.facebook.react.packagerconnection.ReconnectingWebSocket.ConnectionCallback;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -120,4 +122,20 @@ public class JSPackagerClientTest {
verify(handler, never()).onNotification(any());
verify(handler, never()).onRequest(any(), any(Responder.class));
}
@Test
public void test_onDisconnection_ShouldTriggerDisconnectionCallback() throws IOException {
ConnectionCallback connectionHandler = mock(ConnectionCallback.class);
RequestHandler handler = mock(RequestHandler.class);
final JSPackagerClient client =
new JSPackagerClient("test_client", mSettings, new HashMap<String,RequestHandler>(), connectionHandler);
client.close();
verify(connectionHandler, never()).onConnected();
verify(connectionHandler, times(1)).onDisconnected();
verify(handler, never()).onNotification(any());
verify(handler, never()).onRequest(any(), any(Responder.class));
}
}