mirror of
https://github.com/status-im/react-native.git
synced 2025-01-29 18:54:58 +00:00
Add callback for Connection/Disconnection to Metro
Reviewed By: bnham Differential Revision: D6447126 fbshipit-source-id: d4c8a4dfb65c2a378f7fe696c8617ff5f3c6cb68
This commit is contained in:
parent
8547b7e111
commit
3d5dc872a4
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user