Commit c9d23185 authored by rpereira2's avatar rpereira2

Simplify logic to timeout the validation request

The backoff utility which is already being used, supports timing out.
So use that instead of a manual check for max number of requests.
parent 244966bf
...@@ -9,31 +9,25 @@ import statusCodes from '~/lib/utils/http_status'; ...@@ -9,31 +9,25 @@ import statusCodes from '~/lib/utils/http_status';
import { backOff } from '~/lib/utils/common_utils'; import { backOff } from '~/lib/utils/common_utils';
import { queryTypes, formDataValidator } from '../constants'; import { queryTypes, formDataValidator } from '../constants';
const MAX_REQUESTS = 4; const VALIDATION_REQUEST_TIMEOUT = 10000;
const axiosCancelToken = axios.CancelToken; const axiosCancelToken = axios.CancelToken;
let cancelTokenSource; let cancelTokenSource;
function backOffRequest(makeRequestCallback) { function backOffRequest(makeRequestCallback) {
let requestsCount = 0;
return backOff((next, stop) => { return backOff((next, stop) => {
makeRequestCallback() makeRequestCallback()
.then(resp => { .then(resp => {
if (resp.status === statusCodes.OK) { if (resp.status === statusCodes.OK) {
stop(resp); stop(resp);
} else { } else {
requestsCount += 1;
if (requestsCount < MAX_REQUESTS) {
next(); next();
} else {
stop(resp);
}
} }
}) })
// If the request is cancelled by axios // If the request is cancelled by axios
// then consider it as noop so that its not // then consider it as noop so that its not
// caught by subsequent catches // caught by subsequent catches
.catch(thrown => (axios.isCancel(thrown) ? undefined : stop(thrown))); .catch(thrown => (axios.isCancel(thrown) ? undefined : stop(thrown)));
}); }, VALIDATION_REQUEST_TIMEOUT);
} }
export default { export default {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment