all files / src/ retryLink.js

90.91% Statements 40/44
76.92% Branches 20/26
85.71% Functions 12/14
95% Lines 38/40
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          10×         11× 11× 10× 10× 10×                          
"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var Observable = require("zen-observable");
var link_1 = require("./link");
var RetryLink = (function (_super) {
    __extends(RetryLink, _super);
    function RetryLink(params) {
        var _this = _super.call(this) || this;
        _this.count = 0;
        _this.defaultInterval = function (delay) { return delay; };
        _this.max = params && params.max || 10;
        _this.delay = params && params.delay || 300;
        _this.interval = params && params.interval || _this.defaultInterval;
        return _this;
    }
    RetryLink.prototype.request = function (operation, forward) {
        var _this = this;
        return new Observable(function (observer) {
            var subscriber = {
                next: function (data) {
                    _this.count = 0;
                    observer.next(data);
                },
                error: function (error) {
                    _this.count++;
                    if (_this.count < _this.max) {
                        _this.timer = setTimeout(function () {
                            var observable = forward(operation);
                            _this.subscription = observable.subscribe(subscriber);
                        }, _this.interval(_this.delay, _this.count));
                    }
                    else {
                        observer.error(error);
                    }
                },
                complete: observer.complete.bind(observer),
            };
            _this.subscription = forward(operation).subscribe(subscriber);
            return function () {
                _this.subscription.unsubscribe();
                if (_this.timer) {
                    clearTimeout(_this.timer);
                }
            };
        });
    };
    return RetryLink;
}(link_1.ApolloLink));
exports.default = RetryLink;
//# sourceMappingURL=retryLink.js.map