Skip to content

update:Improve Timeout Detection in Network Requests #43548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
package com.facebook.react.modules.network;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;

import java.net.SocketTimeoutException;

/** Util methods to send network responses to JS. */
/**
* Util methods to send network responses to JS.
*/
public class ResponseUtil {
public static void onDataSend(
@Nullable ReactApplicationContext reactContext, int requestId, long progress, long total) {
@Nullable ReactApplicationContext reactContext, int requestId, long progress, long total) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushInt((int) progress);
Expand All @@ -28,11 +32,11 @@ public static void onDataSend(
}

public static void onIncrementalDataReceived(
@Nullable ReactApplicationContext reactContext,
int requestId,
String data,
long progress,
long total) {
@Nullable ReactApplicationContext reactContext,
int requestId,
String data,
long progress,
long total) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushString(data);
Expand All @@ -45,7 +49,7 @@ public static void onIncrementalDataReceived(
}

public static void onDataReceivedProgress(
@Nullable ReactApplicationContext reactContext, int requestId, long progress, long total) {
@Nullable ReactApplicationContext reactContext, int requestId, long progress, long total) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushInt((int) progress);
Expand All @@ -57,7 +61,7 @@ public static void onDataReceivedProgress(
}

public static void onDataReceived(
@Nullable ReactApplicationContext reactContext, int requestId, String data) {
@Nullable ReactApplicationContext reactContext, int requestId, String data) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushString(data);
Expand All @@ -68,7 +72,7 @@ public static void onDataReceived(
}

public static void onDataReceived(
@Nullable ReactApplicationContext reactContext, int requestId, WritableMap data) {
@Nullable ReactApplicationContext reactContext, int requestId, WritableMap data) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushMap(data);
Expand All @@ -79,22 +83,23 @@ public static void onDataReceived(
}

public static void onRequestError(
@Nullable ReactApplicationContext reactContext, int requestId, String error, Throwable e) {
@Nullable ReactApplicationContext reactContext, int requestId, String error, Throwable e) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushString(error);

if ((e != null) && (e.getClass() == SocketTimeoutException.class)) {
args.pushBoolean(true); // last argument is a time out boolean
if (e instanceof SocketTimeoutException || (e instanceof InterruptedIOException && "timeout".equalsIgnoreCase(error))) {
args.pushBoolean(true);
}


if (reactContext != null) {
reactContext.emitDeviceEvent("didCompleteNetworkResponse", args);
}
}

public static void onRequestSuccess(
@Nullable ReactApplicationContext reactContext, int requestId) {
@Nullable ReactApplicationContext reactContext, int requestId) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushNull();
Expand All @@ -105,11 +110,11 @@ public static void onRequestSuccess(
}

public static void onResponseReceived(
@Nullable ReactApplicationContext reactContext,
int requestId,
int statusCode,
WritableMap headers,
String url) {
@Nullable ReactApplicationContext reactContext,
int requestId,
int statusCode,
WritableMap headers,
String url) {
WritableArray args = Arguments.createArray();
args.pushInt(requestId);
args.pushInt(statusCode);
Expand Down