Pass WebSocketSender wrapper instead of WebSocket to callback handler

Reviewed By: cwdick

Differential Revision: D4488064

fbshipit-source-id: eb7113f5f4ba65546c0bd0336dc65b6fae610a53
This commit is contained in:
Lukas Piatkowski 2017-02-10 10:34:33 -08:00 committed by Facebook Github Bot
parent 4388783a21
commit 3329969b63
4 changed files with 61 additions and 15 deletions

View File

@ -35,7 +35,6 @@ import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
import okio.Okio;
import okio.Sink;
@ -85,7 +84,7 @@ public class DevServerHelper {
public interface PackagerCommandListener {
void onPackagerReloadCommand();
void onCaptureHeapCommand();
void onPokeSamplingProfilerCommand(@Nullable final WebSocket webSocket);
void onPokeSamplingProfilerCommand(@Nullable final JSPackagerWebSocketClient.WebSocketSender webSocket);
}
public interface PackagerStatusCallback {
@ -125,7 +124,10 @@ public class DevServerHelper {
mPackagerConnection = new JSPackagerWebSocketClient(getPackagerConnectionURL(),
new JSPackagerWebSocketClient.JSPackagerCallback() {
@Override
public void onMessage(@Nullable WebSocket webSocket, String target, String action) {
public void onMessage(
@Nullable JSPackagerWebSocketClient.WebSocketSender webSocket,
String target,
String action) {
if (commandListener != null && "bridge".equals(target)) {
if ("reload".equals(action)) {
commandListener.onPackagerReloadCommand();

View File

@ -689,7 +689,8 @@ public class DevSupportManagerImpl implements
}
@Override
public void onPokeSamplingProfilerCommand(@Nullable final WebSocket webSocket) {
public void onPokeSamplingProfilerCommand(
@Nullable final JSPackagerWebSocketClient.WebSocketSender webSocket) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
@ -704,7 +705,8 @@ public class DevSupportManagerImpl implements
JSCHeapUpload.captureCallback(mDevServerHelper.getHeapCaptureUploadUrl()));
}
private void handlePokeSamplingProfiler(@Nullable WebSocket webSocket) {
private void handlePokeSamplingProfiler(
@Nullable JSPackagerWebSocketClient.WebSocketSender webSocket) {
try {
List<String> pokeResults = JSCSamplingProfiler.poke(60000);
for (String result : pokeResults) {
@ -716,7 +718,12 @@ public class DevSupportManagerImpl implements
Toast.LENGTH_LONG).show();
if (webSocket != null) {
// WebSocket is provided, so there is a client waiting our response
webSocket.sendMessage(RequestBody.create(WebSocket.TEXT, result == null ? "" : result));
webSocket.sendMessage(
RequestBody.create(
WebSocket.TEXT,
result == null
? "{\"target\":\"profiler\", \"action\":\"started\"}"
: result));
} else if (result != null) {
// The profile was not initiated by external client, so process the
// profile if there is one in the result

View File

@ -22,6 +22,7 @@ import com.facebook.common.logging.FLog;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.ws.WebSocket;
@ -42,8 +43,20 @@ public class JSPackagerWebSocketClient implements WebSocketListener {
private boolean mClosed = false;
private boolean mSuppressConnectionErrors;
final public class WebSocketSender {
private WebSocket mWebSocket;
public WebSocketSender(WebSocket webSocket) {
mWebSocket = webSocket;
}
public void sendMessage(RequestBody requestBody) throws IOException {
mWebSocket.sendMessage(requestBody);
}
}
public interface JSPackagerCallback {
void onMessage(@Nullable WebSocket webSocket, String target, String action);
void onMessage(@Nullable WebSocketSender webSocket, String target, String action);
}
private @Nullable WebSocket mWebSocket;
@ -110,7 +123,10 @@ public class JSPackagerWebSocketClient implements WebSocketListener {
private void triggerMessageCallback(String target, String action) {
if (mCallback != null) {
mCallback.onMessage(mWebSocket, target, action);
WebSocketSender webSocketSender = mWebSocket == null
? null
: new WebSocketSender(mWebSocket);
mCallback.onMessage(webSocketSender, target, action);
}
}

View File

@ -34,7 +34,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 1, \"target\": \"targetValue\", \"action\": \"actionValue\"}"));
verify(callback).onMessage(any(WebSocket.class), eq("targetValue"), eq("actionValue"));
verify(callback).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
eq("targetValue"),
eq("actionValue"));
}
@Test
@ -44,7 +47,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.BINARY,
"{\"version\": 1, \"target\": \"targetValue\", \"action\": \"actionValue\"}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
@Test
@ -54,7 +60,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 1, \"action\": \"actionValue\"}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
@Test
@ -64,7 +73,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 1, \"target\": null, \"action\": \"actionValue\"}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
@Test
@ -74,7 +86,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 1, \"target\": \"targetValue\"}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
@Test
@ -84,7 +99,10 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 1, \"target\": \"targetValue\", \"action\": null}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
@Test
@ -94,6 +112,9 @@ public class JSPackagerWebSocketClientTest {
final JSPackagerWebSocketClient client = new JSPackagerWebSocketClient("ws://not_needed", callback);
client.onMessage(ResponseBody.create(WebSocket.TEXT,
"{\"version\": 2, \"target\": \"targetValue\", \"action\": \"actionValue\"}"));
verify(callback, never()).onMessage(any(WebSocket.class), anyString(), anyString());
verify(callback, never()).onMessage(
any(JSPackagerWebSocketClient.WebSocketSender.class),
anyString(),
anyString());
}
}