all files / src/datasourceApp/ datasourceScheduler.ts

50% Statements 29/58
25% Branches 4/16
50% Functions 7/14
50% Lines 29/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                          20× 20× 20×               21×                                                                          
/* 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;