1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var apollo_link_1 = require("apollo-link");
|
5 | var delayFunction_1 = require("./delayFunction");
|
6 | var retryFunction_1 = require("./retryFunction");
|
7 | var RetryableOperation = (function () {
|
8 | function RetryableOperation(operation, nextLink, delayFor, retryIf) {
|
9 | var _this = this;
|
10 | this.operation = operation;
|
11 | this.nextLink = nextLink;
|
12 | this.delayFor = delayFor;
|
13 | this.retryIf = retryIf;
|
14 | this.retryCount = 0;
|
15 | this.values = [];
|
16 | this.complete = false;
|
17 | this.canceled = false;
|
18 | this.observers = [];
|
19 | this.currentSubscription = null;
|
20 | this.onNext = function (value) {
|
21 | _this.values.push(value);
|
22 | for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) {
|
23 | var observer = _a[_i];
|
24 | if (!observer)
|
25 | continue;
|
26 | observer.next(value);
|
27 | }
|
28 | };
|
29 | this.onComplete = function () {
|
30 | _this.complete = true;
|
31 | for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) {
|
32 | var observer = _a[_i];
|
33 | if (!observer)
|
34 | continue;
|
35 | observer.complete();
|
36 | }
|
37 | };
|
38 | this.onError = function (error) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
39 | var shouldRetry, _i, _a, observer;
|
40 | return tslib_1.__generator(this, function (_b) {
|
41 | switch (_b.label) {
|
42 | case 0:
|
43 | this.retryCount += 1;
|
44 | return [4, this.retryIf(this.retryCount, this.operation, error)];
|
45 | case 1:
|
46 | shouldRetry = _b.sent();
|
47 | if (shouldRetry) {
|
48 | this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error));
|
49 | return [2];
|
50 | }
|
51 | this.error = error;
|
52 | for (_i = 0, _a = this.observers; _i < _a.length; _i++) {
|
53 | observer = _a[_i];
|
54 | if (!observer)
|
55 | continue;
|
56 | observer.error(error);
|
57 | }
|
58 | return [2];
|
59 | }
|
60 | });
|
61 | }); };
|
62 | }
|
63 | RetryableOperation.prototype.subscribe = function (observer) {
|
64 | if (this.canceled) {
|
65 | throw new Error("Subscribing to a retryable link that was canceled is not supported");
|
66 | }
|
67 | this.observers.push(observer);
|
68 | for (var _i = 0, _a = this.values; _i < _a.length; _i++) {
|
69 | var value = _a[_i];
|
70 | observer.next(value);
|
71 | }
|
72 | if (this.complete) {
|
73 | observer.complete();
|
74 | }
|
75 | else if (this.error) {
|
76 | observer.error(this.error);
|
77 | }
|
78 | };
|
79 | RetryableOperation.prototype.unsubscribe = function (observer) {
|
80 | var index = this.observers.indexOf(observer);
|
81 | if (index < 0) {
|
82 | throw new Error("RetryLink BUG! Attempting to unsubscribe unknown observer!");
|
83 | }
|
84 | this.observers[index] = null;
|
85 | if (this.observers.every(function (o) { return o === null; })) {
|
86 | this.cancel();
|
87 | }
|
88 | };
|
89 | RetryableOperation.prototype.start = function () {
|
90 | if (this.currentSubscription)
|
91 | return;
|
92 | this.try();
|
93 | };
|
94 | RetryableOperation.prototype.cancel = function () {
|
95 | if (this.currentSubscription) {
|
96 | this.currentSubscription.unsubscribe();
|
97 | }
|
98 | clearTimeout(this.timerId);
|
99 | this.timerId = null;
|
100 | this.currentSubscription = null;
|
101 | this.canceled = true;
|
102 | };
|
103 | RetryableOperation.prototype.try = function () {
|
104 | this.currentSubscription = this.nextLink(this.operation).subscribe({
|
105 | next: this.onNext,
|
106 | error: this.onError,
|
107 | complete: this.onComplete,
|
108 | });
|
109 | };
|
110 | RetryableOperation.prototype.scheduleRetry = function (delay) {
|
111 | var _this = this;
|
112 | if (this.timerId) {
|
113 | throw new Error("RetryLink BUG! Encountered overlapping retries");
|
114 | }
|
115 | this.timerId = setTimeout(function () {
|
116 | _this.timerId = null;
|
117 | _this.try();
|
118 | }, delay);
|
119 | };
|
120 | return RetryableOperation;
|
121 | }());
|
122 | var RetryLink = (function (_super) {
|
123 | tslib_1.__extends(RetryLink, _super);
|
124 | function RetryLink(options) {
|
125 | var _this = _super.call(this) || this;
|
126 | var _a = options || {}, attempts = _a.attempts, delay = _a.delay;
|
127 | _this.delayFor =
|
128 | typeof delay === 'function' ? delay : delayFunction_1.buildDelayFunction(delay);
|
129 | _this.retryIf =
|
130 | typeof attempts === 'function' ? attempts : retryFunction_1.buildRetryFunction(attempts);
|
131 | return _this;
|
132 | }
|
133 | RetryLink.prototype.request = function (operation, nextLink) {
|
134 | var retryable = new RetryableOperation(operation, nextLink, this.delayFor, this.retryIf);
|
135 | retryable.start();
|
136 | return new apollo_link_1.Observable(function (observer) {
|
137 | retryable.subscribe(observer);
|
138 | return function () {
|
139 | retryable.unsubscribe(observer);
|
140 | };
|
141 | });
|
142 | };
|
143 | return RetryLink;
|
144 | }(apollo_link_1.ApolloLink));
|
145 | exports.RetryLink = RetryLink;
|
146 |
|
\ | No newline at end of file |