Fix requests cancelling (#7457)

* fix backendSrv request cancelling

* revert imports

* formatting
This commit is contained in:
jifwin 2017-02-06 06:42:26 +01:00 committed by Torkel Ödegaard
parent a36b1d9dce
commit 9e7df648b5

View File

@ -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 {
// 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 {
}).finally(() => { }).finally(() => {
// clean up // clean up
if (options.requestId) { if (options.requestId) {
delete this.inFlightRequests[options.requestId]; this.inFlightRequests[options.requestId].shift();
} }
}); });
}; };