UNPKG

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