qdone 2.0.29-alpha → 2.0.30-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,45 +14,9 @@
14
14
  * }
15
15
  * )
16
16
  */
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
17
  Object.defineProperty(exports, "__esModule", { value: true });
54
18
  exports.ExponentialBackoff = void 0;
55
- var ExponentialBackoff = /** @class */ (function () {
19
+ class ExponentialBackoff {
56
20
  /**
57
21
  * Creates various behaviors for backoff.
58
22
  * @param {number} maxRetries - Number of times to attempt the action before
@@ -64,10 +28,7 @@ var ExponentialBackoff = /** @class */ (function () {
64
28
  * @param {number} exponentBase - The base for the exponent. Defaults to 2,
65
29
  * which means the delay doubles every attempt.
66
30
  */
67
- function ExponentialBackoff(maxRetries, maxJitterPercent, exponentBase) {
68
- if (maxRetries === void 0) { maxRetries = 3; }
69
- if (maxJitterPercent === void 0) { maxJitterPercent = 0.5; }
70
- if (exponentBase === void 0) { exponentBase = 2; }
31
+ constructor(maxRetries = 3, maxJitterPercent = 0.5, exponentBase = 2) {
71
32
  if (maxRetries < 1)
72
33
  throw new Error('maxRetries must be >= 1');
73
34
  if (maxJitterPercent < 0.1 || maxJitterPercent > 1)
@@ -82,26 +43,22 @@ var ExponentialBackoff = /** @class */ (function () {
82
43
  /**
83
44
  * Calculates how many ms to delay based on the current attempt number.
84
45
  */
85
- ExponentialBackoff.prototype.calculateDelayMs = function (attemptNumber) {
86
- var secondsRaw = Math.pow(this.exponentBase, attemptNumber); // 2, 4, 8, 16, ....
87
- var jitter = this.maxJitterPercent * (Math.random() - 0.5); // [-0.5, 0.5]
88
- var delayMs = Math.round(secondsRaw * (1 + jitter) * 1000);
46
+ calculateDelayMs(attemptNumber) {
47
+ const secondsRaw = this.exponentBase ** attemptNumber; // 2, 4, 8, 16, ....
48
+ const jitter = this.maxJitterPercent * (Math.random() - 0.5); // [-0.5, 0.5]
49
+ const delayMs = Math.round(secondsRaw * (1 + jitter) * 1000);
89
50
  // console.log({ secondsRaw, jitter, delayMs })
90
51
  return delayMs;
91
- };
52
+ }
92
53
  /**
93
54
  * Resolves after a delay set by the current attempt.
94
55
  */
95
- ExponentialBackoff.prototype.delay = function (attemptNumber) {
96
- return __awaiter(this, void 0, void 0, function () {
97
- var delay;
98
- return __generator(this, function (_a) {
99
- delay = this.calculateDelayMs(attemptNumber);
100
- // console.log({ function: 'delay', attemptNumber, delay })
101
- return [2 /*return*/, new Promise(function (resolve, reject) { return setTimeout(resolve, delay); })];
102
- });
103
- });
104
- };
56
+ async delay(attemptNumber) {
57
+ // console.log(attemptNumber)
58
+ const delay = this.calculateDelayMs(attemptNumber);
59
+ // console.log({ function: 'delay', attemptNumber, delay })
60
+ return new Promise((resolve, reject) => setTimeout(resolve, delay));
61
+ }
105
62
  /**
106
63
  * Call another function repeatedly, retrying with exponential backoff and
107
64
  * jitter if not successful.
@@ -114,59 +71,31 @@ var ExponentialBackoff = /** @class */ (function () {
114
71
  * backoff delay. Defaults to a function that returns true if an exception
115
72
  * is thrown.
116
73
  */
117
- ExponentialBackoff.prototype.run = function (action, shouldRetry) {
118
- var _this = this;
119
- if (action === void 0) { action = function (attemptNumber) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
120
- return [2 /*return*/, undefined];
121
- }); }); }; }
122
- if (shouldRetry === void 0) { shouldRetry = function (returnValue, error) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
123
- return [2 /*return*/, !!error];
124
- }); }); }; }
125
- return __awaiter(this, void 0, void 0, function () {
126
- var attemptNumber, result, e_1;
127
- return __generator(this, function (_a) {
128
- switch (_a.label) {
129
- case 0:
130
- attemptNumber = 0;
131
- _a.label = 1;
132
- case 1:
133
- if (!(attemptNumber++ < this.maxRetries)) return [3 /*break*/, 14];
134
- _a.label = 2;
135
- case 2:
136
- _a.trys.push([2, 8, , 13]);
137
- return [4 /*yield*/, action(attemptNumber)];
138
- case 3:
139
- result = _a.sent();
140
- return [4 /*yield*/, shouldRetry(result, undefined)];
141
- case 4:
142
- if (!_a.sent()) return [3 /*break*/, 6];
143
- if (attemptNumber >= this.maxRetries)
144
- throw new Error('Maximum number of attempts reached');
145
- return [4 /*yield*/, this.delay(attemptNumber)];
146
- case 5:
147
- _a.sent();
148
- return [3 /*break*/, 7];
149
- case 6: return [2 /*return*/, result];
150
- case 7: return [3 /*break*/, 13];
151
- case 8:
152
- e_1 = _a.sent();
153
- return [4 /*yield*/, shouldRetry(undefined, e_1)];
154
- case 9:
155
- if (!_a.sent()) return [3 /*break*/, 11];
156
- if (attemptNumber >= this.maxRetries)
157
- throw e_1;
158
- return [4 /*yield*/, this.delay(attemptNumber)];
159
- case 10:
160
- _a.sent();
161
- return [3 /*break*/, 12];
162
- case 11: throw e_1;
163
- case 12: return [3 /*break*/, 13];
164
- case 13: return [3 /*break*/, 1];
165
- case 14: return [2 /*return*/];
74
+ async run(action = async (attemptNumber) => undefined, shouldRetry = async (returnValue, error) => !!error) {
75
+ let attemptNumber = 0;
76
+ while (attemptNumber++ < this.maxRetries) {
77
+ try {
78
+ const result = await action(attemptNumber);
79
+ if (await shouldRetry(result, undefined)) {
80
+ if (attemptNumber >= this.maxRetries)
81
+ throw new Error('Maximum number of attempts reached');
82
+ await this.delay(attemptNumber);
83
+ }
84
+ else {
85
+ return result;
166
86
  }
167
- });
168
- });
169
- };
170
- return ExponentialBackoff;
171
- }());
87
+ }
88
+ catch (e) {
89
+ if (await shouldRetry(undefined, e)) {
90
+ if (attemptNumber >= this.maxRetries)
91
+ throw e;
92
+ await this.delay(attemptNumber);
93
+ }
94
+ else {
95
+ throw e;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
172
101
  exports.ExponentialBackoff = ExponentialBackoff;