Skip to content

Commit

Permalink
fix(android): optimize fetch promise callback impl (#3674)
Browse files Browse the repository at this point in the history
Co-authored-by: maxli <maxli@tencent.com>
  • Loading branch information
siguangli and siguangli2018 authored Dec 21, 2023
1 parent 305cc5a commit b93618e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public HttpTaskCallbackImpl(@NonNull ResourceDataHolder holder,
@Override
public void onTaskSuccess(HippyHttpRequest request, HippyHttpResponse response)
throws Exception {
mDataHolder.resultCode = FetchResultCode.OK.ordinal();
mDataHolder.addResponseHeaderProperty(HTTP_RESPONSE_STATUS_CODE,
response.getStatusCode().toString());
mDataHolder.addResponseHeaderProperty(HTTP_RESPONSE_RESPONSE_MESSAGE,
Expand All @@ -355,7 +356,6 @@ public void onTaskSuccess(HippyHttpRequest request, HippyHttpResponse response)
}
mDataHolder.errorMessage = sb.toString();
}
mDataHolder.resultCode = FetchResultCode.ERR_REMOTE_REQUEST_FAILED.ordinal();
mCallback.onHandleCompleted();
return;
}
Expand All @@ -365,7 +365,6 @@ public void onTaskSuccess(HippyHttpRequest request, HippyHttpResponse response)
}
mDataHolder.readResourceDataFromStream(inputStream);
} catch (IOException e) {
mDataHolder.resultCode = FetchResultCode.ERR_REMOTE_REQUEST_FAILED.ordinal();
mDataHolder.errorMessage = e.getMessage();
mCallback.onHandleCompleted();
return;
Expand Down Expand Up @@ -397,7 +396,6 @@ public void onTaskSuccess(HippyHttpRequest request, HippyHttpResponse response)
cookieManager.flush();
}
}
mDataHolder.resultCode = FetchResultCode.OK.ordinal();
mCallback.onHandleCompleted();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ protected void normalizeRequest(@NonNull HippyMap request,
}
}

protected void handleFetchResponse(@NonNull ResourceDataHolder dataHolder, Promise promise)
@NonNull
protected JSObject handleFetchResponse(@NonNull ResourceDataHolder dataHolder)
throws IllegalStateException {
JSObject responseObject = new JSObject();
int statusCode = 0;
int statusCode = -1;
String responseMessage = null;
JSObject headerObject = new JSObject();
if (dataHolder.responseHeaders != null) {
try {
statusCode = Integer.parseInt(
dataHolder.responseHeaders.get(HTTP_RESPONSE_STATUS_CODE));
} catch (NumberFormatException e) {
throw new IllegalStateException(e.getMessage());
throw new IllegalStateException("parse status code error!");
}
responseMessage = dataHolder.responseHeaders.get(HTTP_RESPONSE_RESPONSE_MESSAGE);
for (Entry<String, String> entry : dataHolder.responseHeaders.entrySet()) {
Expand All @@ -125,8 +126,11 @@ protected void handleFetchResponse(@NonNull ResourceDataHolder dataHolder, Promi
headerObject.set(key, value);
}
}
if (responseMessage == null) {
responseMessage = (dataHolder.errorMessage == null) ? "" : dataHolder.errorMessage;
}
responseObject.set(HTTP_RESPONSE_STATUS_CODE, statusCode);
responseObject.set("statusLine", (responseMessage == null) ? "" : responseMessage);
responseObject.set("statusLine", responseMessage);
responseObject.set("respHeaders", headerObject);
String body = "";
try {
Expand All @@ -138,7 +142,22 @@ protected void handleFetchResponse(@NonNull ResourceDataHolder dataHolder, Promi
throw new IllegalStateException(e.getMessage());
}
responseObject.set("respBody", body);
promise.resolve(responseObject);
return responseObject;
}

protected void handleFetchResult(@NonNull ResourceDataHolder dataHolder, final Promise promise) {
try {
if (dataHolder.resultCode == ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE) {
JSObject responseObject = handleFetchResponse(dataHolder);
promise.resolve(responseObject);
} else {
String errorMessage =
(dataHolder.errorMessage == null) ? "Load remote resource failed!" : dataHolder.errorMessage;
promise.reject(errorMessage);
}
} catch (IllegalStateException e) {
promise.reject("Handle response failed: " + e.getMessage());
}
}

@SuppressWarnings("deprecation")
Expand All @@ -162,19 +181,7 @@ public void fetch(final HippyMap request, final Promise promise) {
new FetchResourceCallback() {
@Override
public void onFetchCompleted(@NonNull ResourceDataHolder dataHolder) {
if (dataHolder.resultCode
== ResourceDataHolder.RESOURCE_LOAD_SUCCESS_CODE) {
try {
handleFetchResponse(dataHolder, promise);
} catch (IllegalStateException e) {
promise.reject(
"Handle response failed: " + e.getMessage());
}
} else {
String error = TextUtils.isEmpty(dataHolder.errorMessage)
? "Load remote resource failed!" : dataHolder.errorMessage;
promise.resolve(error);
}
handleFetchResult(dataHolder, promise);
dataHolder.recycle();
}

Expand Down

0 comments on commit b93618e

Please sign in to comment.