break dependency between JSPackagerClient.RequestHandler and WebSocketListener

Reviewed By: amnn

Differential Revision: D4810406

fbshipit-source-id: a447bc15c6619921edd7adf0b3d1d93ae04e2e43
This commit is contained in:
Charles Dick
2017-03-31 10:59:34 -07:00
committed by Facebook Github Bot
parent ec68c97d72
commit 175e77d004
9 changed files with 125 additions and 69 deletions

View File

@@ -62,17 +62,17 @@ public class FileIoHandler implements Runnable {
private int mNextHandle;
private final Handler mHandler;
private final Map<Integer, TtlFileInputStream> mOpenFiles;
private final Map<String, JSPackagerClient.RequestHandler> mRequestHandlers;
private final Map<String, RequestHandler> mRequestHandlers;
public FileIoHandler() {
mNextHandle = 1;
mHandler = new Handler(Looper.getMainLooper());
mOpenFiles = new HashMap<>();
mRequestHandlers = new HashMap<>();
mRequestHandlers.put("fopen", new JSPackagerClient.RequestOnlyHandler() {
mRequestHandlers.put("fopen", new RequestOnlyHandler() {
@Override
public void onRequest(
@Nullable Object params, JSPackagerClient.Responder responder) {
@Nullable Object params, Responder responder) {
synchronized (mOpenFiles) {
try {
JSONObject paramsObj = (JSONObject)params;
@@ -98,10 +98,10 @@ public class FileIoHandler implements Runnable {
}
}
});
mRequestHandlers.put("fclose", new JSPackagerClient.RequestOnlyHandler() {
mRequestHandlers.put("fclose", new RequestOnlyHandler() {
@Override
public void onRequest(
@Nullable Object params, JSPackagerClient.Responder responder) {
@Nullable Object params, Responder responder) {
synchronized (mOpenFiles) {
try {
if (!(params instanceof Number)) {
@@ -121,10 +121,10 @@ public class FileIoHandler implements Runnable {
}
}
});
mRequestHandlers.put("fread", new JSPackagerClient.RequestOnlyHandler() {
mRequestHandlers.put("fread", new RequestOnlyHandler() {
@Override
public void onRequest(
@Nullable Object params, JSPackagerClient.Responder responder) {
@Nullable Object params, Responder responder) {
synchronized (mOpenFiles) {
try {
JSONObject paramsObj = (JSONObject)params;
@@ -153,7 +153,7 @@ public class FileIoHandler implements Runnable {
});
}
public Map<String, JSPackagerClient.RequestHandler> handlers() {
public Map<String, RequestHandler> handlers() {
return mRequestHandlers;
}

View File

@@ -8,9 +8,6 @@
package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import android.net.Uri;
@@ -32,10 +29,10 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
private static final String PACKAGER_CONNECTION_URL_FORMAT = "ws://%s/message?device=%s&app=%s&context=%s";
private static final int PROTOCOL_VERSION = 2;
public class Responder {
private class ResponderImpl implements Responder {
private Object mId;
public Responder(Object id) {
public ResponderImpl(Object id) {
mId = id;
}
@@ -64,26 +61,6 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
}
}
public interface RequestHandler {
public void onRequest(@Nullable Object params, Responder responder);
public void onNotification(@Nullable Object params);
}
public static abstract class NotificationOnlyHandler implements RequestHandler {
final public void onRequest(@Nullable Object params, Responder responder) {
responder.error("Request is not supported");
FLog.e(TAG, "Request is not supported");
}
abstract public void onNotification(@Nullable Object params);
}
public static abstract class RequestOnlyHandler implements RequestHandler {
abstract public void onRequest(@Nullable Object params, Responder responder);
final public void onNotification(@Nullable Object params) {
FLog.e(TAG, "Notification is not supported");
}
}
private ReconnectingWebSocket mWebSocket;
private Map<String, RequestHandler> mRequestHandlers;
@@ -149,7 +126,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
if (id == null) {
handler.onNotification(params);
} else {
handler.onRequest(params, new Responder(id));
handler.onRequest(params, new ResponderImpl(id));
}
} catch (Exception e) {
FLog.e(TAG, "Handling the message failed", e);
@@ -160,7 +137,7 @@ final public class JSPackagerClient implements ReconnectingWebSocket.MessageCall
private void abortOnMessage(Object id, String reason) {
if (id != null) {
(new Responder(id)).error(reason);
(new ResponderImpl(id)).error(reason);
}
FLog.e(TAG, "Handling the message failed with reason: " + reason);

View File

@@ -0,0 +1,23 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
* file in the same directory.
*/
package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
import com.facebook.common.logging.FLog;
public abstract class NotificationOnlyHandler implements RequestHandler {
private static final String TAG = JSPackagerClient.class.getSimpleName();
final public void onRequest(@Nullable Object params, Responder responder) {
responder.error("Request is not supported");
FLog.e(TAG, "Request is not supported");
}
abstract public void onNotification(@Nullable Object params);
}

View File

@@ -0,0 +1,16 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
* file in the same directory.
*/
package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
public interface RequestHandler {
void onRequest(@Nullable Object params, Responder responder);
void onNotification(@Nullable Object params);
}

View File

@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
* file in the same directory.
*/
package com.facebook.react.packagerconnection;
import javax.annotation.Nullable;
import com.facebook.common.logging.FLog;
public abstract class RequestOnlyHandler implements RequestHandler {
private static final String TAG = JSPackagerClient.class.getSimpleName();
abstract public void onRequest(@Nullable Object params, Responder responder);
final public void onNotification(@Nullable Object params) {
FLog.e(TAG, "Notification is not supported");
}
}

View File

@@ -0,0 +1,14 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the LICENSE file in the root
* directory of this source tree. An additional grant of patent rights can be found in the PATENTS
* file in the same directory.
*/
package com.facebook.react.packagerconnection;
public interface Responder {
void respond(Object result);
void error(Object error);
}