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