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