mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-09 22:43:10 +08:00
WebWorkers: Convert NetworkingModule to support web workers
Summary: Converts NetworkingModule to dispatch callbacks to the appropriate ExecutionContext. Reviewed By: lexs Differential Revision: D2932170 fb-gh-sync-id: ac77eec1a176ede4d2257bfd8ddb13153331c8a4 shipit-source-id: ac77eec1a176ede4d2257bfd8ddb13153331c8a4
This commit is contained in:
committed by
Facebook Github Bot 9
parent
532e41105b
commit
9a3f11d3e7
@@ -12,10 +12,10 @@ package com.facebook.react.modules.core;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
import com.facebook.react.bridge.SupportsWebWorkers;
|
||||
import com.facebook.react.bridge.UiThreadUtil;
|
||||
|
||||
/**
|
||||
@@ -23,7 +23,8 @@ import com.facebook.react.bridge.UiThreadUtil;
|
||||
*/
|
||||
public class DeviceEventManagerModule extends ReactContextBaseJavaModule {
|
||||
|
||||
public static interface RCTDeviceEventEmitter extends JavaScriptModule {
|
||||
@SupportsWebWorkers
|
||||
public interface RCTDeviceEventEmitter extends JavaScriptModule {
|
||||
void emit(String eventName, @Nullable Object data);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.io.Reader;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ExecutorToken;
|
||||
import com.facebook.react.bridge.GuardedAsyncTask;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
@@ -116,9 +117,10 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
|
||||
@ReactMethod
|
||||
/**
|
||||
* @param timeout value of 0 results in no timeout
|
||||
*/
|
||||
* @param timeout value of 0 results in no timeout
|
||||
*/
|
||||
public void sendRequest(
|
||||
final ExecutorToken executorToken,
|
||||
String method,
|
||||
String url,
|
||||
final int requestId,
|
||||
@@ -144,7 +146,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
|
||||
Headers requestHeaders = extractHeaders(headers, data);
|
||||
if (requestHeaders == null) {
|
||||
onRequestError(requestId, "Unrecognized headers format");
|
||||
onRequestError(executorToken, requestId, "Unrecognized headers format");
|
||||
return;
|
||||
}
|
||||
String contentType = requestHeaders.get(CONTENT_TYPE_HEADER_NAME);
|
||||
@@ -155,7 +157,10 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method));
|
||||
} else if (data.hasKey(REQUEST_BODY_KEY_STRING)) {
|
||||
if (contentType == null) {
|
||||
onRequestError(requestId, "Payload is set but no content-type header specified");
|
||||
onRequestError(
|
||||
executorToken,
|
||||
requestId,
|
||||
"Payload is set but no content-type header specified");
|
||||
return;
|
||||
}
|
||||
String body = data.getString(REQUEST_BODY_KEY_STRING);
|
||||
@@ -163,7 +168,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
if (RequestBodyUtil.isGzipEncoding(contentEncoding)) {
|
||||
RequestBody requestBody = RequestBodyUtil.createGzip(contentMediaType, body);
|
||||
if (requestBody == null) {
|
||||
onRequestError(requestId, "Failed to gzip request body");
|
||||
onRequestError(executorToken, requestId, "Failed to gzip request body");
|
||||
return;
|
||||
}
|
||||
requestBuilder.method(method, requestBody);
|
||||
@@ -172,14 +177,17 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
} else if (data.hasKey(REQUEST_BODY_KEY_URI)) {
|
||||
if (contentType == null) {
|
||||
onRequestError(requestId, "Payload is set but no content-type header specified");
|
||||
onRequestError(
|
||||
executorToken,
|
||||
requestId,
|
||||
"Payload is set but no content-type header specified");
|
||||
return;
|
||||
}
|
||||
String uri = data.getString(REQUEST_BODY_KEY_URI);
|
||||
InputStream fileInputStream =
|
||||
RequestBodyUtil.getFileInputStream(getReactApplicationContext(), uri);
|
||||
if (fileInputStream == null) {
|
||||
onRequestError(requestId, "Could not retrieve file for uri " + uri);
|
||||
onRequestError(executorToken, requestId, "Could not retrieve file for uri " + uri);
|
||||
return;
|
||||
}
|
||||
requestBuilder.method(
|
||||
@@ -190,7 +198,8 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
contentType = "multipart/form-data";
|
||||
}
|
||||
ReadableArray parts = data.getArray(REQUEST_BODY_KEY_FORMDATA);
|
||||
MultipartBuilder multipartBuilder = constructMultipartBody(parts, contentType, requestId);
|
||||
MultipartBuilder multipartBuilder =
|
||||
constructMultipartBody(executorToken, parts, contentType, requestId);
|
||||
if (multipartBuilder == null) {
|
||||
return;
|
||||
}
|
||||
@@ -207,7 +216,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
if (mShuttingDown) {
|
||||
return;
|
||||
}
|
||||
onRequestError(requestId, e.getMessage());
|
||||
onRequestError(executorToken, requestId, e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -217,25 +226,28 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
// Before we touch the body send headers to JS
|
||||
onResponseReceived(requestId, response);
|
||||
onResponseReceived(executorToken, requestId, response);
|
||||
|
||||
ResponseBody responseBody = response.body();
|
||||
try {
|
||||
if (useIncrementalUpdates) {
|
||||
readWithProgress(requestId, responseBody);
|
||||
onRequestSuccess(requestId);
|
||||
readWithProgress(executorToken, requestId, responseBody);
|
||||
onRequestSuccess(executorToken, requestId);
|
||||
} else {
|
||||
onDataReceived(requestId, responseBody.string());
|
||||
onRequestSuccess(requestId);
|
||||
onDataReceived(executorToken, requestId, responseBody.string());
|
||||
onRequestSuccess(executorToken, requestId);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
onRequestError(requestId, e.getMessage());
|
||||
onRequestError(executorToken, requestId, e.getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void readWithProgress(int requestId, ResponseBody responseBody) throws IOException {
|
||||
private void readWithProgress(
|
||||
ExecutorToken executorToken,
|
||||
int requestId,
|
||||
ResponseBody responseBody) throws IOException {
|
||||
Reader reader = responseBody.charStream();
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder(getBufferSize(responseBody));
|
||||
@@ -246,14 +258,14 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
sb.append(buffer, 0, read);
|
||||
long now = System.nanoTime();
|
||||
if (shouldDispatch(now, last)) {
|
||||
onDataReceived(requestId, sb.toString());
|
||||
onDataReceived(executorToken, requestId, sb.toString());
|
||||
sb.setLength(0);
|
||||
last = now;
|
||||
}
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
onDataReceived(requestId, sb.toString());
|
||||
onDataReceived(executorToken, requestId, sb.toString());
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
@@ -273,31 +285,34 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
}
|
||||
|
||||
private void onDataReceived(int requestId, String data) {
|
||||
private void onDataReceived(ExecutorToken ExecutorToken, int requestId, String data) {
|
||||
WritableArray args = Arguments.createArray();
|
||||
args.pushInt(requestId);
|
||||
args.pushString(data);
|
||||
|
||||
getEventEmitter().emit("didReceiveNetworkData", args);
|
||||
getEventEmitter(ExecutorToken).emit("didReceiveNetworkData", args);
|
||||
}
|
||||
|
||||
private void onRequestError(int requestId, String error) {
|
||||
private void onRequestError(ExecutorToken ExecutorToken, int requestId, String error) {
|
||||
WritableArray args = Arguments.createArray();
|
||||
args.pushInt(requestId);
|
||||
args.pushString(error);
|
||||
|
||||
getEventEmitter().emit("didCompleteNetworkResponse", args);
|
||||
getEventEmitter(ExecutorToken).emit("didCompleteNetworkResponse", args);
|
||||
}
|
||||
|
||||
private void onRequestSuccess(int requestId) {
|
||||
private void onRequestSuccess(ExecutorToken ExecutorToken, int requestId) {
|
||||
WritableArray args = Arguments.createArray();
|
||||
args.pushInt(requestId);
|
||||
args.pushNull();
|
||||
|
||||
getEventEmitter().emit("didCompleteNetworkResponse", args);
|
||||
getEventEmitter(ExecutorToken).emit("didCompleteNetworkResponse", args);
|
||||
}
|
||||
|
||||
private void onResponseReceived(int requestId, Response response) {
|
||||
private void onResponseReceived(
|
||||
ExecutorToken ExecutorToken,
|
||||
int requestId,
|
||||
Response response) {
|
||||
WritableMap headers = translateHeaders(response.headers());
|
||||
|
||||
WritableArray args = Arguments.createArray();
|
||||
@@ -306,7 +321,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
args.pushMap(headers);
|
||||
args.pushString(response.request().urlString());
|
||||
|
||||
getEventEmitter().emit("didReceiveNetworkResponse", args);
|
||||
getEventEmitter(ExecutorToken).emit("didReceiveNetworkResponse", args);
|
||||
}
|
||||
|
||||
private static WritableMap translateHeaders(Headers headers) {
|
||||
@@ -326,7 +341,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void abortRequest(final int requestId) {
|
||||
public void abortRequest(ExecutorToken executorToken, final int requestId) {
|
||||
// We have to use AsyncTask since this might trigger a NetworkOnMainThreadException, this is an
|
||||
// open issue on OkHttp: https://github.com/square/okhttp/issues/869
|
||||
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
|
||||
@@ -338,11 +353,21 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void clearCookies(com.facebook.react.bridge.Callback callback) {
|
||||
public void clearCookies(
|
||||
ExecutorToken executorToken,
|
||||
com.facebook.react.bridge.Callback callback) {
|
||||
mCookieHandler.clearCookies(callback);
|
||||
}
|
||||
|
||||
private @Nullable MultipartBuilder constructMultipartBody(
|
||||
@Override
|
||||
public boolean supportsWebWorkers() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private
|
||||
@Nullable
|
||||
MultipartBuilder constructMultipartBody(
|
||||
ExecutorToken ExecutorToken,
|
||||
ReadableArray body,
|
||||
String contentType,
|
||||
int requestId) {
|
||||
@@ -356,7 +381,10 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
ReadableArray headersArray = bodyPart.getArray("headers");
|
||||
Headers headers = extractHeaders(headersArray, null);
|
||||
if (headers == null) {
|
||||
onRequestError(requestId, "Missing or invalid header format for FormData part.");
|
||||
onRequestError(
|
||||
ExecutorToken,
|
||||
requestId,
|
||||
"Missing or invalid header format for FormData part.");
|
||||
return null;
|
||||
}
|
||||
MediaType partContentType = null;
|
||||
@@ -373,19 +401,25 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
multipartBuilder.addPart(headers, RequestBody.create(partContentType, bodyValue));
|
||||
} else if (bodyPart.hasKey(REQUEST_BODY_KEY_URI)) {
|
||||
if (partContentType == null) {
|
||||
onRequestError(requestId, "Binary FormData part needs a content-type header.");
|
||||
onRequestError(
|
||||
ExecutorToken,
|
||||
requestId,
|
||||
"Binary FormData part needs a content-type header.");
|
||||
return null;
|
||||
}
|
||||
String fileContentUriStr = bodyPart.getString(REQUEST_BODY_KEY_URI);
|
||||
InputStream fileInputStream =
|
||||
RequestBodyUtil.getFileInputStream(getReactApplicationContext(), fileContentUriStr);
|
||||
if (fileInputStream == null) {
|
||||
onRequestError(requestId, "Could not retrieve file for uri " + fileContentUriStr);
|
||||
onRequestError(
|
||||
ExecutorToken,
|
||||
requestId,
|
||||
"Could not retrieve file for uri " + fileContentUriStr);
|
||||
return null;
|
||||
}
|
||||
multipartBuilder.addPart(headers, RequestBodyUtil.create(partContentType, fileInputStream));
|
||||
} else {
|
||||
onRequestError(requestId, "Unrecognized FormData part.");
|
||||
onRequestError(ExecutorToken, requestId, "Unrecognized FormData part.");
|
||||
}
|
||||
}
|
||||
return multipartBuilder;
|
||||
@@ -394,7 +428,9 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
/**
|
||||
* Extracts the headers from the Array. If the format is invalid, this method will return null.
|
||||
*/
|
||||
private @Nullable Headers extractHeaders(
|
||||
private
|
||||
@Nullable
|
||||
Headers extractHeaders(
|
||||
@Nullable ReadableArray headersArray,
|
||||
@Nullable ReadableMap requestData) {
|
||||
if (headersArray == null) {
|
||||
@@ -423,8 +459,8 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
return headersBuilder.build();
|
||||
}
|
||||
|
||||
private DeviceEventManagerModule.RCTDeviceEventEmitter getEventEmitter() {
|
||||
private DeviceEventManagerModule.RCTDeviceEventEmitter getEventEmitter(ExecutorToken ExecutorToken) {
|
||||
return getReactApplicationContext()
|
||||
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
||||
.getJSModule(ExecutorToken, DeviceEventManagerModule.RCTDeviceEventEmitter.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user