UNPKG

6.14 kBJavaScriptView Raw
1import { __extends, __awaiter, __generator } from 'tslib';
2import { Observable, ApolloLink } from 'apollo-link';
3
4function buildDelayFunction(delayOptions) {
5 var _a = delayOptions || {}, _b = _a.initial, initial = _b === void 0 ? 300 : _b, _c = _a.jitter, jitter = _c === void 0 ? true : _c, _d = _a.max, max = _d === void 0 ? Infinity : _d;
6 var baseDelay = jitter ? initial : initial / 2;
7 return function delayFunction(count) {
8 var delay = Math.min(max, baseDelay * Math.pow(2, count));
9 if (jitter) {
10 delay = Math.random() * delay;
11 }
12 return delay;
13 };
14}
15
16function buildRetryFunction(retryOptions) {
17 var _a = retryOptions || {}, retryIf = _a.retryIf, _b = _a.max, max = _b === void 0 ? 5 : _b;
18 return function retryFunction(count, operation, error) {
19 if (count >= max)
20 return false;
21 return retryIf ? retryIf(error, operation) : !!error;
22 };
23}
24
25var RetryableOperation = (function () {
26 function RetryableOperation(operation, nextLink, delayFor, retryIf) {
27 var _this = this;
28 this.operation = operation;
29 this.nextLink = nextLink;
30 this.delayFor = delayFor;
31 this.retryIf = retryIf;
32 this.retryCount = 0;
33 this.values = [];
34 this.complete = false;
35 this.canceled = false;
36 this.observers = [];
37 this.currentSubscription = null;
38 this.onNext = function (value) {
39 _this.values.push(value);
40 for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) {
41 var observer = _a[_i];
42 if (!observer)
43 continue;
44 observer.next(value);
45 }
46 };
47 this.onComplete = function () {
48 _this.complete = true;
49 for (var _i = 0, _a = _this.observers; _i < _a.length; _i++) {
50 var observer = _a[_i];
51 if (!observer)
52 continue;
53 observer.complete();
54 }
55 };
56 this.onError = function (error) { return __awaiter(_this, void 0, void 0, function () {
57 var shouldRetry, _i, _a, observer;
58 return __generator(this, function (_b) {
59 switch (_b.label) {
60 case 0:
61 this.retryCount += 1;
62 return [4, this.retryIf(this.retryCount, this.operation, error)];
63 case 1:
64 shouldRetry = _b.sent();
65 if (shouldRetry) {
66 this.scheduleRetry(this.delayFor(this.retryCount, this.operation, error));
67 return [2];
68 }
69 this.error = error;
70 for (_i = 0, _a = this.observers; _i < _a.length; _i++) {
71 observer = _a[_i];
72 if (!observer)
73 continue;
74 observer.error(error);
75 }
76 return [2];
77 }
78 });
79 }); };
80 }
81 RetryableOperation.prototype.subscribe = function (observer) {
82 if (this.canceled) {
83 throw new Error("Subscribing to a retryable link that was canceled is not supported");
84 }
85 this.observers.push(observer);
86 for (var _i = 0, _a = this.values; _i < _a.length; _i++) {
87 var value = _a[_i];
88 observer.next(value);
89 }
90 if (this.complete) {
91 observer.complete();
92 }
93 else if (this.error) {
94 observer.error(this.error);
95 }
96 };
97 RetryableOperation.prototype.unsubscribe = function (observer) {
98 var index = this.observers.indexOf(observer);
99 if (index < 0) {
100 throw new Error("RetryLink BUG! Attempting to unsubscribe unknown observer!");
101 }
102 this.observers[index] = null;
103 if (this.observers.every(function (o) { return o === null; })) {
104 this.cancel();
105 }
106 };
107 RetryableOperation.prototype.start = function () {
108 if (this.currentSubscription)
109 return;
110 this.try();
111 };
112 RetryableOperation.prototype.cancel = function () {
113 if (this.currentSubscription) {
114 this.currentSubscription.unsubscribe();
115 }
116 clearTimeout(this.timerId);
117 this.timerId = null;
118 this.currentSubscription = null;
119 this.canceled = true;
120 };
121 RetryableOperation.prototype.try = function () {
122 this.currentSubscription = this.nextLink(this.operation).subscribe({
123 next: this.onNext,
124 error: this.onError,
125 complete: this.onComplete,
126 });
127 };
128 RetryableOperation.prototype.scheduleRetry = function (delay) {
129 var _this = this;
130 if (this.timerId) {
131 throw new Error("RetryLink BUG! Encountered overlapping retries");
132 }
133 this.timerId = setTimeout(function () {
134 _this.timerId = null;
135 _this.try();
136 }, delay);
137 };
138 return RetryableOperation;
139}());
140var RetryLink = (function (_super) {
141 __extends(RetryLink, _super);
142 function RetryLink(options) {
143 var _this = _super.call(this) || this;
144 var _a = options || {}, attempts = _a.attempts, delay = _a.delay;
145 _this.delayFor =
146 typeof delay === 'function' ? delay : buildDelayFunction(delay);
147 _this.retryIf =
148 typeof attempts === 'function' ? attempts : buildRetryFunction(attempts);
149 return _this;
150 }
151 RetryLink.prototype.request = function (operation, nextLink) {
152 var retryable = new RetryableOperation(operation, nextLink, this.delayFor, this.retryIf);
153 retryable.start();
154 return new Observable(function (observer) {
155 retryable.subscribe(observer);
156 return function () {
157 retryable.unsubscribe(observer);
158 };
159 });
160 };
161 return RetryLink;
162}(ApolloLink));
163
164export { RetryLink };
165//# sourceMappingURL=bundle.esm.js.map