Commit 9e7df648 by jifwin Committed by Torkel Ödegaard

Fix requests cancelling (#7457)

* fix backendSrv request cancelling

* revert imports

* formatting
parent a36b1d9d
...@@ -94,6 +94,21 @@ export class BackendSrv { ...@@ -94,6 +94,21 @@ export class BackendSrv {
}); });
}; };
addCanceler(requestId, canceler) {
if (requestId in this.inFlightRequests) {
this.inFlightRequests[requestId].push(canceler);
} else {
this.inFlightRequests[requestId] = [canceler];
}
}
resolveCancelerIfExists(requestId) {
var cancelers = this.inFlightRequests[requestId];
if (!_.isUndefined(cancelers) && cancelers.length) {
cancelers[0].resolve();
}
}
datasourceRequest(options) { datasourceRequest(options) {
options.retry = options.retry || 0; options.retry = options.retry || 0;
...@@ -101,16 +116,13 @@ export class BackendSrv { ...@@ -101,16 +116,13 @@ export class BackendSrv {
// particular query. If the requestID exists, the promise it is keyed to // particular query. If the requestID exists, the promise it is keyed to
// is canceled, canceling the previous datasource request if it is still // is canceled, canceling the previous datasource request if it is still
// in-flight. // in-flight.
var canceler; var requestId = options.requestId;
if (options.requestId) { if (requestId) {
canceler = this.inFlightRequests[options.requestId]; this.resolveCancelerIfExists(requestId);
if (canceler) {
canceler.resolve();
}
// create new canceler // create new canceler
canceler = this.$q.defer(); var canceler = this.$q.defer();
options.timeout = canceler.promise; options.timeout = canceler.promise;
this.inFlightRequests[options.requestId] = canceler; this.addCanceler(requestId, canceler);
} }
var requestIsLocal = options.url.indexOf('/') === 0; var requestIsLocal = options.url.indexOf('/') === 0;
...@@ -158,7 +170,7 @@ export class BackendSrv { ...@@ -158,7 +170,7 @@ export class BackendSrv {
}).finally(() => { }).finally(() => {
// clean up // clean up
if (options.requestId) { if (options.requestId) {
delete this.inFlightRequests[options.requestId]; this.inFlightRequests[options.requestId].shift();
} }
}); });
}; };
......
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