/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var DatasourceScheduler = (function () {
function DatasourceScheduler(dsInstance) {
this.dsInstance = dsInstance;
this._fetchInterval = 1000;
this.disposed = false;
this.running = false;
}
Object.defineProperty(DatasourceScheduler.prototype, "fetchInterval", {
set: function (ms) {
this._fetchInterval = ms;
if (this._fetchInterval < 1000) {
this.fetchInterval = 1000;
console.warn("Datasource has fetch interval below 1000ms, it was forced to 1000ms\n" +
"Please do not set intervals shorter than 1000ms. If you really need this, file a ticket with explanation!");
}
this.scheduleFetch(this._fetchInterval);
},
enumerable: true,
configurable: true
});
DatasourceScheduler.prototype.start = function () {
this.running = true;
// Fetch once as soon as possible
this.scheduleFetch(0);
};
DatasourceScheduler.prototype.forceUpdate = function () {
this.scheduleFetch(0);
};
DatasourceScheduler.prototype.dispose = function () {
this.clearFetchTimeout();
this.disposed = true;
this.running = false;
};
DatasourceScheduler.prototype.scheduleFetch = function (ms) {
var _this = this;
this.clearFetchTimeout();
Iif (ms === Infinity) {
return;
}
Iif (!this.running) {
return;
}
this.fetchTimeoutRef = setTimeout(function () {
_this.doFetchData();
}, ms);
};
DatasourceScheduler.prototype.clearFetchTimeout = function () {
if (this.fetchTimeoutRef) {
clearTimeout(this.fetchTimeoutRef);
this.fetchTimeoutRef = null;
}
};
DatasourceScheduler.prototype.doFetchData = function () {
var _this = this;
if (this.fetchPromise) {
console.warn("Do not fetch data because a fetch is currently running on Datasource", this.dsInstance.id);
return;
}
var fetchPromise = new Promise(function (resolve, reject) {
_this.dsInstance.fetchData(resolve, reject);
setTimeout(function () {
if (_this.fetchPromise === fetchPromise) {
reject(new Error("Timeout! Datasource fetchData() took longer than 5 seconds."));
}
}, 5000);
});
this.fetchPromise = fetchPromise;
fetchPromise.then(function (result) {
_this.fetchPromise = null;
if (!_this.disposed) {
if (result !== undefined) {
_this.dsInstance.fetchedDatasourceData(result);
}
_this.scheduleFetch(_this._fetchInterval);
}
else {
console.error("fetchData of disposed plugin finished - result discarded", _this.dsInstance.id, result);
}
}).catch(function (error) {
console.warn("Failed to fetch data for Datasource of type " + _this.dsInstance.type + " with id " + _this.dsInstance.id);
console.error(error);
_this.fetchPromise = null;
_this.scheduleFetch(_this._fetchInterval);
});
};
return DatasourceScheduler;
}());
exports.DatasourceScheduler = DatasourceScheduler;
|