Commit 1653d570 authored by Dhiraj Bodicherla's avatar Dhiraj Bodicherla

Cancelling previous backoff reqs

parent c6974623
...@@ -17,6 +17,8 @@ import { backOff } from '~/lib/utils/common_utils'; ...@@ -17,6 +17,8 @@ import { backOff } from '~/lib/utils/common_utils';
import { queryTypes, formDataValidator } from '../constants'; import { queryTypes, formDataValidator } from '../constants';
const MAX_REQUESTS = 4; const MAX_REQUESTS = 4;
const axiosCancelToken = axios.CancelToken;
let cancelTokenSource;
function backOffRequest(makeRequestCallback) { function backOffRequest(makeRequestCallback) {
let requestsCount = 0; let requestsCount = 0;
...@@ -34,7 +36,10 @@ function backOffRequest(makeRequestCallback) { ...@@ -34,7 +36,10 @@ function backOffRequest(makeRequestCallback) {
} }
} }
}) })
.catch(stop); // If the request is cancelled by axios
// then consider it as noop so that its not
// caught by subsequent catches
.catch(thrown => (axios.isCancel(thrown) ? undefined : stop(thrown)));
}); });
} }
...@@ -114,11 +119,17 @@ export default { ...@@ -114,11 +119,17 @@ export default {
} }
}, },
methods: { methods: {
requestValidation() { requestValidation(query, cancelToken) {
return backOffRequest(() => return backOffRequest(() =>
axios.post(this.validateQueryPath, { axios.post(
query: this.query, this.validateQueryPath,
}), {
query,
},
{
cancelToken,
},
),
); );
}, },
setFormState(isValid, inFlight, message) { setFormState(isValid, inFlight, message) {
...@@ -132,7 +143,15 @@ export default { ...@@ -132,7 +143,15 @@ export default {
return; return;
} }
this.setFormState(null, true, ''); this.setFormState(null, true, '');
this.requestValidation() // cancel previously dispatched backoff request
if (cancelTokenSource) {
cancelTokenSource.cancel();
}
// Creating a new token for each request because
// if a single token is used it can cancel existing requests
// as well.
cancelTokenSource = axiosCancelToken.source();
this.requestValidation(this.query, cancelTokenSource.token)
.then(res => { .then(res => {
const response = res.data; const response = res.data;
const { valid, error } = response.query; const { valid, error } = response.query;
......
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