Skip to content
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

HttpUtils abortable requests #6388

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ Network.registerResponseHandler((queuedRequest, response) => {
});

Network.registerErrorHandler((queuedRequest, error) => {
if (error.name === 'AbortError') {
Log.info('[API] request aborted', false, queuedRequest);
return;
}
if (queuedRequest.command !== 'Log') {
Log.hmmm('[API] Handled error when making request', error);
} else {
Expand Down
21 changes: 17 additions & 4 deletions src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ Onyx.connect({
callback: val => shouldUseSecureStaging = (val && _.isBoolean(val.shouldUseSecureStaging)) ? val.shouldUseSecureStaging : false,
});

const NON_ABORTABLE_COMMANDS = ['Log', 'DeleteLogin'];
let abortController = new AbortController();

/**
* Send an HTTP request, and attempt to resolve the json response.
* If there is a network error, we'll set the application offline.
*
* @param {String} url
* @param {String} method
* @param {Object} body
* @param {String} [method='get']
* @param {Object} [body=null]
* @param {Boolean} [canAbort=true]
* @returns {Promise}
*/
function processHTTPRequest(url, method = 'get', body = null) {
function processHTTPRequest(url, method = 'get', body = null, canAbort = true) {
return fetch(url, {
method,
body,
signal: canAbort ? abortController.signal : undefined,
})
.then(response => response.json());
}
Expand All @@ -44,7 +49,9 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure =
apiRoot = CONST.STAGING_SECURE_URL;
}

return processHTTPRequest(`${apiRoot}api?command=${command}`, type, formData);
const canAbort = !_.contains(NON_ABORTABLE_COMMANDS, command);

return processHTTPRequest(`${apiRoot}api?command=${command}`, type, formData, canAbort);
}

/**
Expand All @@ -64,7 +71,13 @@ function download(relativePath) {
return processHTTPRequest(`${siteRoot}${strippedRelativePath}`);
}

function abortPendingRequests() {
abortController.abort();
abortController = new AbortController();
}

export default {
download,
xhr,
abortPendingRequests,
};
2 changes: 2 additions & 0 deletions src/libs/actions/SignInRedirect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Onyx from 'react-native-onyx';
import SignoutManager from '../SignoutManager';
import ONYXKEYS from '../../ONYXKEYS';
import HttpUtils from '../HttpUtils';

let currentActiveClients;
Onyx.connect({
Expand All @@ -20,6 +21,7 @@ Onyx.connect({
* @param {String} errorMessage
*/
function clearStorageAndRedirect(errorMessage) {
HttpUtils.abortPendingRequests();
const activeClients = currentActiveClients;
const preferredLocale = currentPreferredLocale;

Expand Down