allow cancelling reload requests
Summary: Make the ProgressDialog cancelable and cancel the network request / websocket connection when the user presses back. public Reviewed By: andreicoman11 Differential Revision: D2764788 fb-gh-sync-id: 0fdb87ba9431be5a3c453422724cd364292eff61
This commit is contained in:
parent
6dc6794881
commit
cb94d997ca
|
@ -91,6 +91,7 @@ public class DevServerHelper {
|
|||
private boolean mOnChangePollingEnabled;
|
||||
private @Nullable OkHttpClient mOnChangePollingClient;
|
||||
private @Nullable OnServerContentChangeListener mOnServerContentChangeListener;
|
||||
private @Nullable Call mDownloadBundleFromURLCall;
|
||||
|
||||
public DevServerHelper(DevInternalSettings settings) {
|
||||
mSettings = settings;
|
||||
|
@ -179,15 +180,29 @@ public class DevServerHelper {
|
|||
Request request = new Request.Builder()
|
||||
.url(bundleURL)
|
||||
.build();
|
||||
Call call = mClient.newCall(request);
|
||||
call.enqueue(new Callback() {
|
||||
mDownloadBundleFromURLCall = Assertions.assertNotNull(mClient.newCall(request));
|
||||
mDownloadBundleFromURLCall.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
// ignore callback if call was cancelled
|
||||
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
|
||||
mDownloadBundleFromURLCall = null;
|
||||
return;
|
||||
}
|
||||
mDownloadBundleFromURLCall = null;
|
||||
|
||||
callback.onFailure(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Response response) throws IOException {
|
||||
// ignore callback if call was cancelled
|
||||
if (mDownloadBundleFromURLCall == null || mDownloadBundleFromURLCall.isCanceled()) {
|
||||
mDownloadBundleFromURLCall = null;
|
||||
return;
|
||||
}
|
||||
mDownloadBundleFromURLCall = null;
|
||||
|
||||
// Check for server errors. If the server error has the expected form, fail with more info.
|
||||
if (!response.isSuccessful()) {
|
||||
String body = response.body().string();
|
||||
|
@ -214,6 +229,13 @@ public class DevServerHelper {
|
|||
});
|
||||
}
|
||||
|
||||
public void cancelDownloadBundleFromURL() {
|
||||
if (mDownloadBundleFromURLCall != null) {
|
||||
mDownloadBundleFromURLCall.cancel();
|
||||
mDownloadBundleFromURLCall = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void isPackagerRunning(final PackagerStatusCallback callback) {
|
||||
String statusURL = createPackagerStatusURL(getDebugServerHost());
|
||||
Request request = new Request.Builder()
|
||||
|
|
|
@ -629,6 +629,13 @@ public class DevSupportManager implements NativeModuleCallExceptionHandler {
|
|||
},
|
||||
Assertions.assertNotNull(mJSAppBundleName),
|
||||
mJSBundleTempFile);
|
||||
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
mDevServerHelper.cancelDownloadBundleFromURL();
|
||||
}
|
||||
});
|
||||
progressDialog.setCancelable(true);
|
||||
}
|
||||
|
||||
private void reload() {
|
||||
|
|
Loading…
Reference in New Issue