Skip to content

Commit

Permalink
Add callback for Connection/Disconnection to Metro
Browse files Browse the repository at this point in the history
Reviewed By: bnham

Differential Revision: D6447126

fbshipit-source-id: d4c8a4dfb65c2a378f7fe696c8617ff5f3c6cb68
  • Loading branch information
pakoito authored and facebook-github-bot committed Dec 7, 2017
1 parent 8547b7e commit 3d5dc87
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand Down Expand Up @@ -92,6 +93,8 @@ public interface OnServerContentChangeListener {
}

public interface PackagerCommandListener {
void onPackagerConnected();
void onPackagerDisconnected();
void onPackagerReloadCommand();
void onPackagerDevMenuCommand();
void onCaptureHeapCommand(final Responder responder);
Expand Down Expand Up @@ -163,10 +166,24 @@ public void onRequest(@Nullable Object params, Responder responder) {
});
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,16 @@ public void isPackagerRunning(PackagerStatusCallback callback) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.facebook.react.packagerconnection;

import java.util.Map;
import javax.annotation.Nullable;

import android.net.Uri;

Expand Down Expand Up @@ -63,6 +64,13 @@ public void error(Object error) {
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();
Expand All @@ -74,7 +82,7 @@ public JSPackagerClient(String clientId, PackagerConnectionSettings settings, Ma
.appendQueryParameter("clientid", clientId);
String url = builder.build().toString();

mWebSocket = new ReconnectingWebSocket(url, this, null);
mWebSocket = new ReconnectingWebSocket(url, this, connectionCallback);
mRequestHandlers = requestHandlers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,4 +122,20 @@ public void test_onMessage_WrongVersion_ShouldNotTriggerCallback() throws IOExce
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));
}
}

0 comments on commit 3d5dc87

Please sign in to comment.