retuple 1.0.0-next.12 → 1.0.0-next.14
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.
- package/dist/index.cjs +396 -184
- package/dist/index.d.cts +291 -491
- package/dist/index.d.ts +291 -491
- package/dist/index.js +394 -180
- package/dist/symbol.cjs +38 -0
- package/dist/symbol.d.cts +36 -0
- package/dist/symbol.d.ts +36 -0
- package/dist/symbol.js +35 -0
- package/package.json +12 -2
package/dist/index.cjs
CHANGED
|
@@ -10,12 +10,13 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _ResultAsync_inner;
|
|
13
|
+
var _ResultAsync_inner, _a, _ResultRetry_f, _ResultRetry_promise, _ResultRetry_times, _ResultRetry_attempt, _ResultRetry_aborted, _ResultRetry_abort, _ResultRetry_getDelay, _ResultRetry_handler;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.
|
|
15
|
+
exports.RetupleArrayMethodUnavailableError = exports.RetupleInvalidUnionError = exports.RetupleCaughtValueError = exports.RetupleExpectFailed = exports.RetupleUnwrapErrFailed = exports.RetupleUnwrapFailed = void 0;
|
|
16
16
|
exports.Result = Result;
|
|
17
17
|
exports.Ok = Ok;
|
|
18
18
|
exports.Err = Err;
|
|
19
|
+
const symbol_js_1 = require("./symbol.cjs");
|
|
19
20
|
/**
|
|
20
21
|
* ## Retuple Unwrap Failed
|
|
21
22
|
*
|
|
@@ -53,19 +54,6 @@ class RetupleExpectFailed extends Error {
|
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
exports.RetupleExpectFailed = RetupleExpectFailed;
|
|
56
|
-
/**
|
|
57
|
-
* ## Retuple Expect Failed
|
|
58
|
-
*
|
|
59
|
-
* An error which occurs when calling `$flatten` on `Ok`, when the value
|
|
60
|
-
* contained in the `Ok` is not an `Ok` or `Err`.
|
|
61
|
-
*/
|
|
62
|
-
class RetupleFlattenFailed extends Error {
|
|
63
|
-
constructor(value) {
|
|
64
|
-
super("Flatten Result failed, the contained value was not a Result");
|
|
65
|
-
this.value = value;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
exports.RetupleFlattenFailed = RetupleFlattenFailed;
|
|
69
57
|
/**
|
|
70
58
|
* ## Retuple Thrown Value Error
|
|
71
59
|
*
|
|
@@ -73,28 +61,13 @@ exports.RetupleFlattenFailed = RetupleFlattenFailed;
|
|
|
73
61
|
* thrown error or rejected value is not an instance of `Error`, and when no
|
|
74
62
|
* map error function is provided.
|
|
75
63
|
*/
|
|
76
|
-
class
|
|
64
|
+
class RetupleCaughtValueError extends Error {
|
|
77
65
|
constructor(value) {
|
|
78
66
|
super("Caught value was not an instance of Error");
|
|
79
67
|
this.value = value;
|
|
80
68
|
}
|
|
81
69
|
}
|
|
82
|
-
exports.
|
|
83
|
-
/**
|
|
84
|
-
* ## Retuple Invalid Result Error
|
|
85
|
-
*
|
|
86
|
-
* This error is thrown when attempting to construct a `Result` from a tuple,
|
|
87
|
-
* when neither index 0 or 1 are null or undefined. In this case, it is
|
|
88
|
-
* impossible to determine whether the result should be `Ok` or `Err`.
|
|
89
|
-
*/
|
|
90
|
-
class RetupleInvalidResultError extends Error {
|
|
91
|
-
constructor(value) {
|
|
92
|
-
super("Constructing a Result from native tuple failed, at least one of the " +
|
|
93
|
-
"values at index 0 or 1 should be null or undefined");
|
|
94
|
-
this.value = value;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
exports.RetupleInvalidResultError = RetupleInvalidResultError;
|
|
70
|
+
exports.RetupleCaughtValueError = RetupleCaughtValueError;
|
|
98
71
|
/**
|
|
99
72
|
* ## Retuple Invalid Union Error
|
|
100
73
|
*
|
|
@@ -130,24 +103,20 @@ exports.RetupleArrayMethodUnavailableError = RetupleArrayMethodUnavailableError;
|
|
|
130
103
|
* @TODO
|
|
131
104
|
*/
|
|
132
105
|
function Result(resultLike) {
|
|
133
|
-
|
|
134
|
-
if (err === null || err === undefined) {
|
|
135
|
-
return new ResultOk(ok);
|
|
136
|
-
}
|
|
137
|
-
if (ok === null || ok === undefined) {
|
|
138
|
-
return new ResultErr(err);
|
|
139
|
-
}
|
|
140
|
-
throw new RetupleInvalidResultError(resultLike);
|
|
106
|
+
return asResult(resultLike);
|
|
141
107
|
}
|
|
142
108
|
Result.Ok = Ok;
|
|
143
109
|
Result.Err = Err;
|
|
144
|
-
Result.$
|
|
145
|
-
Result.$
|
|
146
|
-
Result.$
|
|
147
|
-
Result.$
|
|
148
|
-
Result.$
|
|
149
|
-
Result.$
|
|
150
|
-
Result.$
|
|
110
|
+
Result.$from = $from;
|
|
111
|
+
Result.$resolve = $resolve;
|
|
112
|
+
Result.$nonNullable = $nonNullable;
|
|
113
|
+
Result.$truthy = $truthy;
|
|
114
|
+
Result.$fromUnion = $fromUnion;
|
|
115
|
+
Result.$safe = $safe;
|
|
116
|
+
Result.$safeAsync = $safeAsync;
|
|
117
|
+
Result.$safePromise = $safePromise;
|
|
118
|
+
Result.$retry = $retry;
|
|
119
|
+
Result.$safeRetry = $safeRetry;
|
|
151
120
|
Object.freeze(Result);
|
|
152
121
|
function Ok(val) {
|
|
153
122
|
return new ResultOk(val);
|
|
@@ -155,30 +124,111 @@ function Ok(val) {
|
|
|
155
124
|
function Err(err) {
|
|
156
125
|
return new ResultErr(err);
|
|
157
126
|
}
|
|
158
|
-
|
|
159
|
-
|
|
127
|
+
/**
|
|
128
|
+
* Construct a {@link Result} from a {@link ResultLike}.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
*
|
|
132
|
+
* ```ts
|
|
133
|
+
* const value: Result<T, E> | ResultLike<T, E> = someResultFn();
|
|
134
|
+
* const result: Result<T, E> = Result.$from(result);
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
function $from(result) {
|
|
138
|
+
if (result instanceof ResultOk || result instanceof ResultErr) {
|
|
160
139
|
return result;
|
|
161
140
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
141
|
+
return asResult(result);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Construct a {@link ResultAsync} from a {@link ResultLikeAwaitable}.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
*
|
|
148
|
+
* ```ts
|
|
149
|
+
* const value:
|
|
150
|
+
* | Result<T, E>
|
|
151
|
+
* | ResultLike<T, E>
|
|
152
|
+
* | ResultAsync<T, E>
|
|
153
|
+
* | Promise<Result<T, E>>
|
|
154
|
+
* | Promise<ResultLike<T, E>>
|
|
155
|
+
* | PromiseLike<Result<T, E>>
|
|
156
|
+
* | PromiseLike<ResultLike<T, E>> = someResultFn();
|
|
157
|
+
*
|
|
158
|
+
* const result: ResultAsync<T, E> = Result.$resolve(result);
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
function $resolve(result) {
|
|
162
|
+
switch (true) {
|
|
163
|
+
case result instanceof ResultAsync:
|
|
164
|
+
return result;
|
|
165
|
+
case result instanceof ResultRetry:
|
|
166
|
+
return new ResultAsync(result);
|
|
167
|
+
case result instanceof ResultOk:
|
|
168
|
+
case result instanceof ResultErr:
|
|
169
|
+
return new ResultAsync(Promise.resolve(result));
|
|
170
|
+
default:
|
|
171
|
+
return new ResultAsync(Promise.resolve(result).then((asResult)));
|
|
167
172
|
}
|
|
168
173
|
}
|
|
169
|
-
function nonNullable(value, error = mapTrue) {
|
|
174
|
+
function $nonNullable(value, error = mapTrue) {
|
|
170
175
|
if (value !== null && value !== undefined) {
|
|
171
176
|
return Ok(value);
|
|
172
177
|
}
|
|
173
178
|
return Err(error());
|
|
174
179
|
}
|
|
175
|
-
function truthy(value, error = mapTrue) {
|
|
180
|
+
function $truthy(value, error = mapTrue) {
|
|
176
181
|
if (value) {
|
|
177
182
|
return Ok(value);
|
|
178
183
|
}
|
|
179
184
|
return Err(error());
|
|
180
185
|
}
|
|
181
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Construct a {@link Result} from a common discriminated union shape. If the
|
|
188
|
+
* union is 'success' then the result is `Ok`
|
|
189
|
+
*
|
|
190
|
+
* Otherwise, the result is `Err` containing:
|
|
191
|
+
*
|
|
192
|
+
* - the returned value from the error function when provided;
|
|
193
|
+
* - or `true` otherwise.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
*
|
|
197
|
+
* ```ts
|
|
198
|
+
* const result: Result<string, Error> = Result.$truthy(
|
|
199
|
+
* username.trim(),
|
|
200
|
+
* () => new Error("Username is empty"),
|
|
201
|
+
* );
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
*
|
|
206
|
+
* ```ts
|
|
207
|
+
* const [err, value] = Result.$truthy("test");
|
|
208
|
+
*
|
|
209
|
+
* assert.equal(err, undefined);
|
|
210
|
+
* assert.equal(value, "test");
|
|
211
|
+
* ```
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
*
|
|
215
|
+
* ```ts
|
|
216
|
+
* const [err, value] = Result.$truthy("");
|
|
217
|
+
*
|
|
218
|
+
* assert.equal(err, true);
|
|
219
|
+
* assert.equal(value, undefined);
|
|
220
|
+
* ```
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
*
|
|
224
|
+
* ```ts
|
|
225
|
+
* const [err, value] = Result.$truthy(0, () => "error");
|
|
226
|
+
*
|
|
227
|
+
* assert.equal(err, "error");
|
|
228
|
+
* assert.equal(value, undefined);
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
function $fromUnion(union) {
|
|
182
232
|
if (union.success === true) {
|
|
183
233
|
return Ok(union.data);
|
|
184
234
|
}
|
|
@@ -187,7 +237,7 @@ function union(union) {
|
|
|
187
237
|
}
|
|
188
238
|
throw new RetupleInvalidUnionError(union);
|
|
189
239
|
}
|
|
190
|
-
function safe(f, mapError = ensureError) {
|
|
240
|
+
function $safe(f, mapError = ensureError) {
|
|
191
241
|
try {
|
|
192
242
|
return Ok(f());
|
|
193
243
|
}
|
|
@@ -195,7 +245,7 @@ function safe(f, mapError = ensureError) {
|
|
|
195
245
|
return Err(mapError(err));
|
|
196
246
|
}
|
|
197
247
|
}
|
|
198
|
-
function safeAsync(f, mapError = ensureError) {
|
|
248
|
+
function $safeAsync(f, mapError = ensureError) {
|
|
199
249
|
return new ResultAsync((async () => {
|
|
200
250
|
try {
|
|
201
251
|
return Ok(await f());
|
|
@@ -205,9 +255,38 @@ function safeAsync(f, mapError = ensureError) {
|
|
|
205
255
|
}
|
|
206
256
|
})());
|
|
207
257
|
}
|
|
208
|
-
function safePromise(promise, mapError = ensureError) {
|
|
258
|
+
function $safePromise(promise, mapError = ensureError) {
|
|
209
259
|
return new ResultAsync(promise.then((Ok), async (err) => Err(await mapError(err))));
|
|
210
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Construct a {@link ResultRetry} from a function which returns a
|
|
263
|
+
* {@link Result}. The function will be retried based on provided retry
|
|
264
|
+
* settings and eventually return a `Result`. No attempt is made to catch
|
|
265
|
+
* thrown errors or promise rejections.
|
|
266
|
+
*
|
|
267
|
+
* To retry a potentially unsafe function, use {@link Result.$safeRetry}.
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
*
|
|
271
|
+
* ```ts
|
|
272
|
+
* // Retry someResultFn up to 3 times until Ok is returned,
|
|
273
|
+
* // with a 1 second delay between each invocation:
|
|
274
|
+
* const result = await Result.$retry(someResultFn).$times(3).$delay(1000);
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
function $retry(f) {
|
|
278
|
+
return new ResultRetry(f);
|
|
279
|
+
}
|
|
280
|
+
function $safeRetry(f, mapError = ensureError) {
|
|
281
|
+
return new ResultRetry(async () => {
|
|
282
|
+
try {
|
|
283
|
+
return Ok(await f());
|
|
284
|
+
}
|
|
285
|
+
catch (err) {
|
|
286
|
+
return Err(mapError(err));
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
211
290
|
/**
|
|
212
291
|
* ## RetupleArray
|
|
213
292
|
*
|
|
@@ -218,8 +297,7 @@ class RetupleArray extends Array {
|
|
|
218
297
|
/**
|
|
219
298
|
* ## Method not available
|
|
220
299
|
*
|
|
221
|
-
* Built-in array methods not available on
|
|
222
|
-
* to a tuple using `$tuple()` first.
|
|
300
|
+
* Built-in array methods not available on {@link Result} types.
|
|
223
301
|
*
|
|
224
302
|
* @deprecated
|
|
225
303
|
*/
|
|
@@ -229,8 +307,7 @@ class RetupleArray extends Array {
|
|
|
229
307
|
/**
|
|
230
308
|
* ## Method not available
|
|
231
309
|
*
|
|
232
|
-
* Built-in array methods not available on
|
|
233
|
-
* to a tuple using `$tuple()` first.
|
|
310
|
+
* Built-in array methods not available on {@link Result} types.
|
|
234
311
|
*
|
|
235
312
|
* @deprecated
|
|
236
313
|
*/
|
|
@@ -240,8 +317,7 @@ class RetupleArray extends Array {
|
|
|
240
317
|
/**
|
|
241
318
|
* ## Method not available
|
|
242
319
|
*
|
|
243
|
-
* Built-in array methods not available on
|
|
244
|
-
* to a tuple using `$tuple()` first.
|
|
320
|
+
* Built-in array methods not available on {@link Result} types.
|
|
245
321
|
*
|
|
246
322
|
* @deprecated
|
|
247
323
|
*/
|
|
@@ -251,8 +327,7 @@ class RetupleArray extends Array {
|
|
|
251
327
|
/**
|
|
252
328
|
* ## Method not available
|
|
253
329
|
*
|
|
254
|
-
* Built-in array methods not available on
|
|
255
|
-
* to a tuple using `$tuple()` first.
|
|
330
|
+
* Built-in array methods not available on {@link Result} types.
|
|
256
331
|
*
|
|
257
332
|
* @deprecated
|
|
258
333
|
*/
|
|
@@ -262,8 +337,7 @@ class RetupleArray extends Array {
|
|
|
262
337
|
/**
|
|
263
338
|
* ## Method not available
|
|
264
339
|
*
|
|
265
|
-
* Built-in array methods not available on
|
|
266
|
-
* to a tuple using `$tuple()` first.
|
|
340
|
+
* Built-in array methods not available on {@link Result} types.
|
|
267
341
|
*
|
|
268
342
|
* @deprecated
|
|
269
343
|
*/
|
|
@@ -273,8 +347,7 @@ class RetupleArray extends Array {
|
|
|
273
347
|
/**
|
|
274
348
|
* ## Method not available
|
|
275
349
|
*
|
|
276
|
-
* Built-in array methods not available on
|
|
277
|
-
* to a tuple using `$tuple()` first.
|
|
350
|
+
* Built-in array methods not available on {@link Result} types.
|
|
278
351
|
*
|
|
279
352
|
* @deprecated
|
|
280
353
|
*/
|
|
@@ -284,8 +357,7 @@ class RetupleArray extends Array {
|
|
|
284
357
|
/**
|
|
285
358
|
* ## Method not available
|
|
286
359
|
*
|
|
287
|
-
* Built-in array methods not available on
|
|
288
|
-
* to a tuple using `$tuple()` first.
|
|
360
|
+
* Built-in array methods not available on {@link Result} types.
|
|
289
361
|
*
|
|
290
362
|
* @deprecated
|
|
291
363
|
*/
|
|
@@ -295,8 +367,7 @@ class RetupleArray extends Array {
|
|
|
295
367
|
/**
|
|
296
368
|
* ## Method not available
|
|
297
369
|
*
|
|
298
|
-
* Built-in array methods not available on
|
|
299
|
-
* to a tuple using `$tuple()` first.
|
|
370
|
+
* Built-in array methods not available on {@link Result} types.
|
|
300
371
|
*
|
|
301
372
|
* @deprecated
|
|
302
373
|
*/
|
|
@@ -306,8 +377,7 @@ class RetupleArray extends Array {
|
|
|
306
377
|
/**
|
|
307
378
|
* ## Method not available
|
|
308
379
|
*
|
|
309
|
-
* Built-in array methods not available on
|
|
310
|
-
* to a tuple using `$tuple()` first.
|
|
380
|
+
* Built-in array methods not available on {@link Result} types.
|
|
311
381
|
*
|
|
312
382
|
* @deprecated
|
|
313
383
|
*/
|
|
@@ -317,8 +387,7 @@ class RetupleArray extends Array {
|
|
|
317
387
|
/**
|
|
318
388
|
* ## Method not available
|
|
319
389
|
*
|
|
320
|
-
* Built-in array methods not available on
|
|
321
|
-
* to a tuple using `$tuple()` first.
|
|
390
|
+
* Built-in array methods not available on {@link Result} types.
|
|
322
391
|
*
|
|
323
392
|
* @deprecated
|
|
324
393
|
*/
|
|
@@ -328,8 +397,7 @@ class RetupleArray extends Array {
|
|
|
328
397
|
/**
|
|
329
398
|
* ## Method not available
|
|
330
399
|
*
|
|
331
|
-
* Built-in array methods not available on
|
|
332
|
-
* to a tuple using `$tuple()` first.
|
|
400
|
+
* Built-in array methods not available on {@link Result} types.
|
|
333
401
|
*
|
|
334
402
|
* @deprecated
|
|
335
403
|
*/
|
|
@@ -339,8 +407,7 @@ class RetupleArray extends Array {
|
|
|
339
407
|
/**
|
|
340
408
|
* ## Method not available
|
|
341
409
|
*
|
|
342
|
-
* Built-in array methods not available on
|
|
343
|
-
* to a tuple using `$tuple()` first.
|
|
410
|
+
* Built-in array methods not available on {@link Result} types.
|
|
344
411
|
*
|
|
345
412
|
* @deprecated
|
|
346
413
|
*/
|
|
@@ -350,8 +417,7 @@ class RetupleArray extends Array {
|
|
|
350
417
|
/**
|
|
351
418
|
* ## Method not available
|
|
352
419
|
*
|
|
353
|
-
* Built-in array methods not available on
|
|
354
|
-
* to a tuple using `$tuple()` first.
|
|
420
|
+
* Built-in array methods not available on {@link Result} types.
|
|
355
421
|
*
|
|
356
422
|
* @deprecated
|
|
357
423
|
*/
|
|
@@ -361,8 +427,7 @@ class RetupleArray extends Array {
|
|
|
361
427
|
/**
|
|
362
428
|
* ## Method not available
|
|
363
429
|
*
|
|
364
|
-
* Built-in array methods not available on
|
|
365
|
-
* to a tuple using `$tuple()` first.
|
|
430
|
+
* Built-in array methods not available on {@link Result} types.
|
|
366
431
|
*
|
|
367
432
|
* @deprecated
|
|
368
433
|
*/
|
|
@@ -372,8 +437,7 @@ class RetupleArray extends Array {
|
|
|
372
437
|
/**
|
|
373
438
|
* ## Method not available
|
|
374
439
|
*
|
|
375
|
-
* Built-in array methods not available on
|
|
376
|
-
* to a tuple using `$tuple()` first.
|
|
440
|
+
* Built-in array methods not available on {@link Result} types.
|
|
377
441
|
*
|
|
378
442
|
* @deprecated
|
|
379
443
|
*/
|
|
@@ -383,8 +447,7 @@ class RetupleArray extends Array {
|
|
|
383
447
|
/**
|
|
384
448
|
* ## Method not available
|
|
385
449
|
*
|
|
386
|
-
* Built-in array methods not available on
|
|
387
|
-
* to a tuple using `$tuple()` first.
|
|
450
|
+
* Built-in array methods not available on {@link Result} types.
|
|
388
451
|
*
|
|
389
452
|
* @deprecated
|
|
390
453
|
*/
|
|
@@ -394,8 +457,7 @@ class RetupleArray extends Array {
|
|
|
394
457
|
/**
|
|
395
458
|
* ## Method not available
|
|
396
459
|
*
|
|
397
|
-
* Built-in array methods not available on
|
|
398
|
-
* to a tuple using `$tuple()` first.
|
|
460
|
+
* Built-in array methods not available on {@link Result} types.
|
|
399
461
|
*
|
|
400
462
|
* @deprecated
|
|
401
463
|
*/
|
|
@@ -405,8 +467,7 @@ class RetupleArray extends Array {
|
|
|
405
467
|
/**
|
|
406
468
|
* ## Method not available
|
|
407
469
|
*
|
|
408
|
-
* Built-in array methods not available on
|
|
409
|
-
* to a tuple using `$tuple()` first.
|
|
470
|
+
* Built-in array methods not available on {@link Result} types.
|
|
410
471
|
*
|
|
411
472
|
* @deprecated
|
|
412
473
|
*/
|
|
@@ -416,8 +477,7 @@ class RetupleArray extends Array {
|
|
|
416
477
|
/**
|
|
417
478
|
* ## Method not available
|
|
418
479
|
*
|
|
419
|
-
* Built-in array methods not available on
|
|
420
|
-
* to a tuple using `$tuple()` first.
|
|
480
|
+
* Built-in array methods not available on {@link Result} types.
|
|
421
481
|
*
|
|
422
482
|
* @deprecated
|
|
423
483
|
*/
|
|
@@ -427,8 +487,7 @@ class RetupleArray extends Array {
|
|
|
427
487
|
/**
|
|
428
488
|
* ## Method not available
|
|
429
489
|
*
|
|
430
|
-
* Built-in array methods not available on
|
|
431
|
-
* to a tuple using `$tuple()` first.
|
|
490
|
+
* Built-in array methods not available on {@link Result} types.
|
|
432
491
|
*
|
|
433
492
|
* @deprecated
|
|
434
493
|
*/
|
|
@@ -438,8 +497,7 @@ class RetupleArray extends Array {
|
|
|
438
497
|
/**
|
|
439
498
|
* ## Method not available
|
|
440
499
|
*
|
|
441
|
-
* Built-in array methods not available on
|
|
442
|
-
* to a tuple using `$tuple()` first.
|
|
500
|
+
* Built-in array methods not available on {@link Result} types.
|
|
443
501
|
*
|
|
444
502
|
* @deprecated
|
|
445
503
|
*/
|
|
@@ -449,8 +507,7 @@ class RetupleArray extends Array {
|
|
|
449
507
|
/**
|
|
450
508
|
* ## Method not available
|
|
451
509
|
*
|
|
452
|
-
* Built-in array methods not available on
|
|
453
|
-
* to a tuple using `$tuple()` first.
|
|
510
|
+
* Built-in array methods not available on {@link Result} types.
|
|
454
511
|
*
|
|
455
512
|
* @deprecated
|
|
456
513
|
*/
|
|
@@ -460,8 +517,7 @@ class RetupleArray extends Array {
|
|
|
460
517
|
/**
|
|
461
518
|
* ## Method not available
|
|
462
519
|
*
|
|
463
|
-
* Built-in array methods not available on
|
|
464
|
-
* to a tuple using `$tuple()` first.
|
|
520
|
+
* Built-in array methods not available on {@link Result} types.
|
|
465
521
|
*
|
|
466
522
|
* @deprecated
|
|
467
523
|
*/
|
|
@@ -471,8 +527,7 @@ class RetupleArray extends Array {
|
|
|
471
527
|
/**
|
|
472
528
|
* ## Method not available
|
|
473
529
|
*
|
|
474
|
-
* Built-in array methods not available on
|
|
475
|
-
* to a tuple using `$tuple()` first.
|
|
530
|
+
* Built-in array methods not available on {@link Result} types.
|
|
476
531
|
*
|
|
477
532
|
* @deprecated
|
|
478
533
|
*/
|
|
@@ -482,8 +537,7 @@ class RetupleArray extends Array {
|
|
|
482
537
|
/**
|
|
483
538
|
* ## Method not available
|
|
484
539
|
*
|
|
485
|
-
* Built-in array methods not available on
|
|
486
|
-
* to a tuple using `$tuple()` first.
|
|
540
|
+
* Built-in array methods not available on {@link Result} types.
|
|
487
541
|
*
|
|
488
542
|
* @deprecated
|
|
489
543
|
*/
|
|
@@ -493,8 +547,7 @@ class RetupleArray extends Array {
|
|
|
493
547
|
/**
|
|
494
548
|
* ## Method not available
|
|
495
549
|
*
|
|
496
|
-
* Built-in array methods not available on
|
|
497
|
-
* to a tuple using `$tuple()` first.
|
|
550
|
+
* Built-in array methods not available on {@link Result} types.
|
|
498
551
|
*
|
|
499
552
|
* @deprecated
|
|
500
553
|
*/
|
|
@@ -504,8 +557,7 @@ class RetupleArray extends Array {
|
|
|
504
557
|
/**
|
|
505
558
|
* ## Method not available
|
|
506
559
|
*
|
|
507
|
-
* Built-in array methods not available on
|
|
508
|
-
* to a tuple using `$tuple()` first.
|
|
560
|
+
* Built-in array methods not available on {@link Result} types.
|
|
509
561
|
*
|
|
510
562
|
* @deprecated
|
|
511
563
|
*/
|
|
@@ -515,8 +567,7 @@ class RetupleArray extends Array {
|
|
|
515
567
|
/**
|
|
516
568
|
* ## Method not available
|
|
517
569
|
*
|
|
518
|
-
* Built-in array methods not available on
|
|
519
|
-
* to a tuple using `$tuple()` first.
|
|
570
|
+
* Built-in array methods not available on {@link Result} types.
|
|
520
571
|
*
|
|
521
572
|
* @deprecated
|
|
522
573
|
*/
|
|
@@ -526,8 +577,7 @@ class RetupleArray extends Array {
|
|
|
526
577
|
/**
|
|
527
578
|
* ## Method not available
|
|
528
579
|
*
|
|
529
|
-
* Built-in array methods not available on
|
|
530
|
-
* to a tuple using `$tuple()` first.
|
|
580
|
+
* Built-in array methods not available on {@link Result} types.
|
|
531
581
|
*
|
|
532
582
|
* @deprecated
|
|
533
583
|
*/
|
|
@@ -537,8 +587,7 @@ class RetupleArray extends Array {
|
|
|
537
587
|
/**
|
|
538
588
|
* ## Method not available
|
|
539
589
|
*
|
|
540
|
-
* Built-in array methods not available on
|
|
541
|
-
* to a tuple using `$tuple()` first.
|
|
590
|
+
* Built-in array methods not available on {@link Result} types.
|
|
542
591
|
*
|
|
543
592
|
* @deprecated
|
|
544
593
|
*/
|
|
@@ -548,8 +597,7 @@ class RetupleArray extends Array {
|
|
|
548
597
|
/**
|
|
549
598
|
* ## Method not available
|
|
550
599
|
*
|
|
551
|
-
* Built-in array methods not available on
|
|
552
|
-
* to a tuple using `$tuple()` first.
|
|
600
|
+
* Built-in array methods not available on {@link Result} types.
|
|
553
601
|
*
|
|
554
602
|
* @deprecated
|
|
555
603
|
*/
|
|
@@ -559,8 +607,7 @@ class RetupleArray extends Array {
|
|
|
559
607
|
/**
|
|
560
608
|
* ## Method not available
|
|
561
609
|
*
|
|
562
|
-
* Built-in array methods not available on
|
|
563
|
-
* to a tuple using `$tuple()` first.
|
|
610
|
+
* Built-in array methods not available on {@link Result} types.
|
|
564
611
|
*
|
|
565
612
|
* @deprecated
|
|
566
613
|
*/
|
|
@@ -570,8 +617,7 @@ class RetupleArray extends Array {
|
|
|
570
617
|
/**
|
|
571
618
|
* ## Method not available
|
|
572
619
|
*
|
|
573
|
-
* Built-in array methods not available on
|
|
574
|
-
* to a tuple using `$tuple()` first.
|
|
620
|
+
* Built-in array methods not available on {@link Result} types.
|
|
575
621
|
*
|
|
576
622
|
* @deprecated
|
|
577
623
|
*/
|
|
@@ -581,8 +627,7 @@ class RetupleArray extends Array {
|
|
|
581
627
|
/**
|
|
582
628
|
* ## Method not available
|
|
583
629
|
*
|
|
584
|
-
* Built-in array methods not available on
|
|
585
|
-
* to a tuple using `$tuple()` first.
|
|
630
|
+
* Built-in array methods not available on {@link Result} types.
|
|
586
631
|
*
|
|
587
632
|
* @deprecated
|
|
588
633
|
*/
|
|
@@ -592,8 +637,7 @@ class RetupleArray extends Array {
|
|
|
592
637
|
/**
|
|
593
638
|
* ## Method not available
|
|
594
639
|
*
|
|
595
|
-
* Built-in array methods not available on
|
|
596
|
-
* to a tuple using `$tuple()` first.
|
|
640
|
+
* Built-in array methods not available on {@link Result} types.
|
|
597
641
|
*
|
|
598
642
|
* @deprecated
|
|
599
643
|
*/
|
|
@@ -603,8 +647,7 @@ class RetupleArray extends Array {
|
|
|
603
647
|
/**
|
|
604
648
|
* ## Method not available
|
|
605
649
|
*
|
|
606
|
-
* Built-in array methods not available on
|
|
607
|
-
* to a tuple using `$tuple()` first.
|
|
650
|
+
* Built-in array methods not available on {@link Result} types.
|
|
608
651
|
*
|
|
609
652
|
* @deprecated
|
|
610
653
|
*/
|
|
@@ -614,8 +657,7 @@ class RetupleArray extends Array {
|
|
|
614
657
|
/**
|
|
615
658
|
* ## Method not available
|
|
616
659
|
*
|
|
617
|
-
* Built-in array methods not available on
|
|
618
|
-
* to a tuple using `$tuple()` first.
|
|
660
|
+
* Built-in array methods not available on {@link Result} types.
|
|
619
661
|
*
|
|
620
662
|
* @deprecated
|
|
621
663
|
*/
|
|
@@ -625,8 +667,7 @@ class RetupleArray extends Array {
|
|
|
625
667
|
/**
|
|
626
668
|
* ## Method not available
|
|
627
669
|
*
|
|
628
|
-
* Built-in array methods not available on
|
|
629
|
-
* to a tuple using `$tuple()` first.
|
|
670
|
+
* Built-in array methods not available on {@link Result} types.
|
|
630
671
|
*
|
|
631
672
|
* @deprecated
|
|
632
673
|
*/
|
|
@@ -645,6 +686,9 @@ class ResultOk extends RetupleArray {
|
|
|
645
686
|
this[0] = undefined;
|
|
646
687
|
this[1] = value;
|
|
647
688
|
}
|
|
689
|
+
[symbol_js_1.ResultLikeSymbol]() {
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
648
692
|
toJSON() {
|
|
649
693
|
return this[1];
|
|
650
694
|
}
|
|
@@ -688,10 +732,10 @@ class ResultOk extends RetupleArray {
|
|
|
688
732
|
return Ok(f(this[1]));
|
|
689
733
|
}
|
|
690
734
|
$andAssertOr(def, condition = isTruthy) {
|
|
691
|
-
return condition(this[1]) ? this : def;
|
|
735
|
+
return condition(this[1]) ? this : asResult(def);
|
|
692
736
|
}
|
|
693
737
|
$andAssertOrElse(def, condition = isTruthy) {
|
|
694
|
-
return condition(this[1]) ? this : def(this[1]);
|
|
738
|
+
return condition(this[1]) ? this : asResult(def(this[1]));
|
|
695
739
|
}
|
|
696
740
|
$or() {
|
|
697
741
|
return this;
|
|
@@ -703,13 +747,13 @@ class ResultOk extends RetupleArray {
|
|
|
703
747
|
return this;
|
|
704
748
|
}
|
|
705
749
|
$and(and) {
|
|
706
|
-
return and;
|
|
750
|
+
return asResult(and);
|
|
707
751
|
}
|
|
708
752
|
$andThen(f) {
|
|
709
|
-
return f(this[1]);
|
|
753
|
+
return asResult(f(this[1]));
|
|
710
754
|
}
|
|
711
755
|
$andThrough(f) {
|
|
712
|
-
const res = f(this[1]);
|
|
756
|
+
const res = asResult(f(this[1]));
|
|
713
757
|
return res instanceof ResultErr ? res : this;
|
|
714
758
|
}
|
|
715
759
|
$andSafe(f, mapError = ensureError) {
|
|
@@ -732,11 +776,7 @@ class ResultOk extends RetupleArray {
|
|
|
732
776
|
return this;
|
|
733
777
|
}
|
|
734
778
|
$flatten() {
|
|
735
|
-
|
|
736
|
-
if (inner instanceof ResultOk || inner instanceof ResultErr) {
|
|
737
|
-
return inner;
|
|
738
|
-
}
|
|
739
|
-
throw new RetupleFlattenFailed(this[1]);
|
|
779
|
+
return this[1];
|
|
740
780
|
}
|
|
741
781
|
$async() {
|
|
742
782
|
return new ResultAsync(Promise.resolve(this));
|
|
@@ -744,9 +784,6 @@ class ResultOk extends RetupleArray {
|
|
|
744
784
|
$promise() {
|
|
745
785
|
return Promise.resolve(this);
|
|
746
786
|
}
|
|
747
|
-
$tuple() {
|
|
748
|
-
return [undefined, this[1]];
|
|
749
|
-
}
|
|
750
787
|
*$iter() {
|
|
751
788
|
yield* this[1];
|
|
752
789
|
}
|
|
@@ -762,6 +799,9 @@ class ResultErr extends RetupleArray {
|
|
|
762
799
|
this[0] = err;
|
|
763
800
|
this[1] = undefined;
|
|
764
801
|
}
|
|
802
|
+
[symbol_js_1.ResultLikeSymbol]() {
|
|
803
|
+
return this;
|
|
804
|
+
}
|
|
765
805
|
toJSON() {
|
|
766
806
|
return null;
|
|
767
807
|
}
|
|
@@ -814,10 +854,10 @@ class ResultErr extends RetupleArray {
|
|
|
814
854
|
return this;
|
|
815
855
|
}
|
|
816
856
|
$or(or) {
|
|
817
|
-
return or;
|
|
857
|
+
return asResult(or);
|
|
818
858
|
}
|
|
819
859
|
$orElse(f) {
|
|
820
|
-
return f(this[0]);
|
|
860
|
+
return asResult(f(this[0]));
|
|
821
861
|
}
|
|
822
862
|
$orSafe(f, mapError = ensureError) {
|
|
823
863
|
try {
|
|
@@ -859,9 +899,6 @@ class ResultErr extends RetupleArray {
|
|
|
859
899
|
$promise() {
|
|
860
900
|
return Promise.resolve(this);
|
|
861
901
|
}
|
|
862
|
-
$tuple() {
|
|
863
|
-
return [this[0], undefined];
|
|
864
|
-
}
|
|
865
902
|
*$iter() {
|
|
866
903
|
return;
|
|
867
904
|
}
|
|
@@ -914,37 +951,47 @@ class ResultAsync {
|
|
|
914
951
|
return res instanceof ResultOk ? res[1] : f();
|
|
915
952
|
}
|
|
916
953
|
/**
|
|
917
|
-
* The same as {@link Retuple.$map|$map}, except it returns
|
|
954
|
+
* The same as {@link Retuple.$map|$map}, except it returns
|
|
955
|
+
* {@link ResultAsync}.
|
|
918
956
|
*/
|
|
919
957
|
$map(f) {
|
|
920
958
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
921
|
-
return res instanceof ResultOk
|
|
959
|
+
return res instanceof ResultOk
|
|
960
|
+
? new ResultOk(f(res[1]))
|
|
961
|
+
: res;
|
|
922
962
|
}));
|
|
923
963
|
}
|
|
924
964
|
/**
|
|
925
965
|
* The same as {@link Retuple.$mapErr|$mapErr}, except it returns
|
|
926
|
-
*
|
|
966
|
+
* {@link ResultAsync}.
|
|
927
967
|
*/
|
|
928
968
|
$mapErr(f) {
|
|
929
969
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
930
|
-
return res instanceof ResultErr
|
|
970
|
+
return res instanceof ResultErr
|
|
971
|
+
? new ResultErr(f(res[0]))
|
|
972
|
+
: res;
|
|
931
973
|
}));
|
|
932
974
|
}
|
|
933
975
|
/**
|
|
934
|
-
* The same as {@link Retuple.$mapOr|$mapOr}, except it returns
|
|
976
|
+
* The same as {@link Retuple.$mapOr|$mapOr}, except it returns
|
|
977
|
+
* {@link ResultAsync}.
|
|
935
978
|
*/
|
|
936
979
|
$mapOr(def, f) {
|
|
937
980
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
938
|
-
return res instanceof ResultOk
|
|
981
|
+
return res instanceof ResultOk
|
|
982
|
+
? new ResultOk(f(res[1]))
|
|
983
|
+
: new ResultOk(def);
|
|
939
984
|
}));
|
|
940
985
|
}
|
|
941
986
|
/**
|
|
942
987
|
* The same as {@link Retuple.$mapOrElse|$mapOrElse}, except it returns
|
|
943
|
-
*
|
|
988
|
+
* {@link ResultAsync}.
|
|
944
989
|
*/
|
|
945
990
|
$mapOrElse(def, f) {
|
|
946
991
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
947
|
-
return res instanceof ResultOk
|
|
992
|
+
return res instanceof ResultOk
|
|
993
|
+
? new ResultOk(f(res[1]))
|
|
994
|
+
: new ResultOk(def(res[0]));
|
|
948
995
|
}));
|
|
949
996
|
}
|
|
950
997
|
$andAssertOr(def, condition = isTruthy) {
|
|
@@ -952,7 +999,7 @@ class ResultAsync {
|
|
|
952
999
|
if (res instanceof ResultErr || condition(res[1])) {
|
|
953
1000
|
return res;
|
|
954
1001
|
}
|
|
955
|
-
return await def;
|
|
1002
|
+
return asResult(await def);
|
|
956
1003
|
}));
|
|
957
1004
|
}
|
|
958
1005
|
$andAssertOrElse(def, condition = isTruthy) {
|
|
@@ -960,18 +1007,20 @@ class ResultAsync {
|
|
|
960
1007
|
if (res instanceof ResultErr || condition(res[1])) {
|
|
961
1008
|
return res;
|
|
962
1009
|
}
|
|
963
|
-
return await def(res[1]);
|
|
1010
|
+
return asResult(await def(res[1]));
|
|
964
1011
|
}));
|
|
965
1012
|
}
|
|
966
1013
|
$or(or) {
|
|
967
1014
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
968
|
-
return res instanceof ResultErr
|
|
1015
|
+
return res instanceof ResultErr
|
|
1016
|
+
? asResult(await or)
|
|
1017
|
+
: res;
|
|
969
1018
|
}));
|
|
970
1019
|
}
|
|
971
1020
|
$orElse(f) {
|
|
972
1021
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
973
1022
|
return res instanceof ResultErr
|
|
974
|
-
? await f(res[0])
|
|
1023
|
+
? asResult(await f(res[0]))
|
|
975
1024
|
: res;
|
|
976
1025
|
}));
|
|
977
1026
|
}
|
|
@@ -981,10 +1030,10 @@ class ResultAsync {
|
|
|
981
1030
|
return res;
|
|
982
1031
|
}
|
|
983
1032
|
try {
|
|
984
|
-
return
|
|
1033
|
+
return new ResultOk(await f(res[0]));
|
|
985
1034
|
}
|
|
986
1035
|
catch (err) {
|
|
987
|
-
return
|
|
1036
|
+
return new ResultErr(mapError(err));
|
|
988
1037
|
}
|
|
989
1038
|
}));
|
|
990
1039
|
}
|
|
@@ -994,27 +1043,31 @@ class ResultAsync {
|
|
|
994
1043
|
return res;
|
|
995
1044
|
}
|
|
996
1045
|
try {
|
|
997
|
-
return
|
|
1046
|
+
return new ResultOk(await promise);
|
|
998
1047
|
}
|
|
999
1048
|
catch (err) {
|
|
1000
|
-
return
|
|
1049
|
+
return new ResultErr(mapError(err));
|
|
1001
1050
|
}
|
|
1002
1051
|
}));
|
|
1003
1052
|
}
|
|
1004
1053
|
$and(and) {
|
|
1005
1054
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
1006
|
-
return res instanceof ResultOk
|
|
1055
|
+
return res instanceof ResultOk
|
|
1056
|
+
? asResult(await and)
|
|
1057
|
+
: res;
|
|
1007
1058
|
}));
|
|
1008
1059
|
}
|
|
1009
1060
|
$andThen(f) {
|
|
1010
1061
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
1011
|
-
return res instanceof ResultOk
|
|
1062
|
+
return res instanceof ResultOk
|
|
1063
|
+
? asResult(await f(res[1]))
|
|
1064
|
+
: res;
|
|
1012
1065
|
}));
|
|
1013
1066
|
}
|
|
1014
1067
|
$andThrough(f) {
|
|
1015
1068
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
1016
1069
|
if (res instanceof ResultOk) {
|
|
1017
|
-
const through = await f(res[1]);
|
|
1070
|
+
const through = asResult(await f(res[1]));
|
|
1018
1071
|
if (through instanceof ResultErr) {
|
|
1019
1072
|
return through;
|
|
1020
1073
|
}
|
|
@@ -1052,7 +1105,7 @@ class ResultAsync {
|
|
|
1052
1105
|
* The same as {@link Retuple.$peek|$peek}, except it:
|
|
1053
1106
|
*
|
|
1054
1107
|
* - awaits the peek function;
|
|
1055
|
-
* - returns
|
|
1108
|
+
* - returns {@link ResultAsync}.
|
|
1056
1109
|
*/
|
|
1057
1110
|
$peek(f) {
|
|
1058
1111
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
@@ -1064,7 +1117,7 @@ class ResultAsync {
|
|
|
1064
1117
|
* The same as {@link Retuple.$tap|$tap}, except it:
|
|
1065
1118
|
*
|
|
1066
1119
|
* - awaits the tap function;
|
|
1067
|
-
* - returns
|
|
1120
|
+
* - returns {@link ResultAsync}.
|
|
1068
1121
|
*/
|
|
1069
1122
|
$tap(f) {
|
|
1070
1123
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
@@ -1078,7 +1131,7 @@ class ResultAsync {
|
|
|
1078
1131
|
* The same as {@link Retuple.$tapErr|$tapErr}, except it:
|
|
1079
1132
|
*
|
|
1080
1133
|
* - awaits the tap error function;
|
|
1081
|
-
* - returns
|
|
1134
|
+
* - returns {@link ResultAsync}.
|
|
1082
1135
|
*/
|
|
1083
1136
|
$tapErr(f) {
|
|
1084
1137
|
return new ResultAsync(__classPrivateFieldGet(this, _ResultAsync_inner, "f").then(async (res) => {
|
|
@@ -1095,24 +1148,183 @@ class ResultAsync {
|
|
|
1095
1148
|
return Promise.resolve(this);
|
|
1096
1149
|
}
|
|
1097
1150
|
/**
|
|
1098
|
-
* The same as {@link Retuple.$
|
|
1099
|
-
*/
|
|
1100
|
-
async $tuple() {
|
|
1101
|
-
return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$tuple();
|
|
1102
|
-
}
|
|
1103
|
-
/**
|
|
1104
|
-
* The same as {@link Retuple.$tuple|$iter}, except it returns a `Promise`.
|
|
1151
|
+
* The same as {@link Retuple.$iter|$iter}, except it returns a `Promise`.
|
|
1105
1152
|
*/
|
|
1106
1153
|
async $iter() {
|
|
1107
1154
|
return (await __classPrivateFieldGet(this, _ResultAsync_inner, "f")).$iter();
|
|
1108
1155
|
}
|
|
1109
1156
|
}
|
|
1110
1157
|
_ResultAsync_inner = new WeakMap();
|
|
1158
|
+
/**
|
|
1159
|
+
* ## ResultRetry
|
|
1160
|
+
*/
|
|
1161
|
+
class ResultRetry {
|
|
1162
|
+
static zero() {
|
|
1163
|
+
return 0;
|
|
1164
|
+
}
|
|
1165
|
+
static delay(ms) {
|
|
1166
|
+
return new Promise((resolve) => setTimeout(resolve, Math.min(ms, _a.MAX_TIMEOUT)));
|
|
1167
|
+
}
|
|
1168
|
+
static integer(value) {
|
|
1169
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
1170
|
+
return Math.max(0, value);
|
|
1171
|
+
}
|
|
1172
|
+
return 0;
|
|
1173
|
+
}
|
|
1174
|
+
constructor(f) {
|
|
1175
|
+
_ResultRetry_f.set(this, void 0);
|
|
1176
|
+
_ResultRetry_promise.set(this, void 0);
|
|
1177
|
+
_ResultRetry_times.set(this, 1);
|
|
1178
|
+
_ResultRetry_attempt.set(this, 0);
|
|
1179
|
+
_ResultRetry_aborted.set(this, false);
|
|
1180
|
+
_ResultRetry_abort.set(this, () => (__classPrivateFieldSet(this, _ResultRetry_aborted, true, "f")));
|
|
1181
|
+
_ResultRetry_getDelay.set(this, _a.zero);
|
|
1182
|
+
_ResultRetry_handler.set(this, void 0);
|
|
1183
|
+
__classPrivateFieldSet(this, _ResultRetry_f, f, "f");
|
|
1184
|
+
__classPrivateFieldSet(this, _ResultRetry_promise, this.drain(), "f");
|
|
1185
|
+
}
|
|
1186
|
+
then(onfulfilled, onrejected) {
|
|
1187
|
+
return __classPrivateFieldGet(this, _ResultRetry_promise, "f").then(onfulfilled, onrejected);
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Sets the maximum number of times the retry function can be executed,
|
|
1191
|
+
* mutating this `ResultRetry` instance.
|
|
1192
|
+
*
|
|
1193
|
+
* **The default value is 1 - meaning that unless set, no retries will be
|
|
1194
|
+
* attempted.**
|
|
1195
|
+
*
|
|
1196
|
+
* The retry function can be called up to the maximum number of times until
|
|
1197
|
+
* it returns `Ok`. If it never returns `Ok`, the most recent `Err` is
|
|
1198
|
+
* returned.
|
|
1199
|
+
*
|
|
1200
|
+
* This function accepts a positive integer between 1 and 100:
|
|
1201
|
+
*
|
|
1202
|
+
* - Integers outside of this range are clamped to the nearest valid value;
|
|
1203
|
+
* - Any other value (NaN, Infinity, fractions, strings) are treated as 1.
|
|
1204
|
+
*
|
|
1205
|
+
* @example
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* // Retry someResultFn up to 3 times until Ok is returned:
|
|
1209
|
+
* const result = await Result.$retry(someResultFn).$times(3);
|
|
1210
|
+
* ```
|
|
1211
|
+
*/
|
|
1212
|
+
$times(times) {
|
|
1213
|
+
__classPrivateFieldSet(this, _ResultRetry_times, Math.min(Math.max(1, _a.integer(times)), _a.MAX_RETRY), "f");
|
|
1214
|
+
return this;
|
|
1215
|
+
}
|
|
1216
|
+
$delay(fnOrMs) {
|
|
1217
|
+
if (typeof fnOrMs === "function") {
|
|
1218
|
+
__classPrivateFieldSet(this, _ResultRetry_getDelay, fnOrMs, "f");
|
|
1219
|
+
return this;
|
|
1220
|
+
}
|
|
1221
|
+
const delay = _a.integer(fnOrMs);
|
|
1222
|
+
if (delay > 0) {
|
|
1223
|
+
__classPrivateFieldSet(this, _ResultRetry_getDelay, () => delay, "f");
|
|
1224
|
+
}
|
|
1225
|
+
return this;
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Sets a handler to be called when an attempt returns `Err`, mutating this
|
|
1229
|
+
* `ResultRetry` instance. The handler can be used to capture information
|
|
1230
|
+
* about each failure, and to abort early and prevent further retries.
|
|
1231
|
+
*
|
|
1232
|
+
* The handler function is called with `ResultRetryHandleState`, containing:
|
|
1233
|
+
*
|
|
1234
|
+
* - **error** - The error value from the last failed attempt;
|
|
1235
|
+
* - **attempt** - The attempt number;
|
|
1236
|
+
* - **abort** - A function which when called, prevents further retries.
|
|
1237
|
+
*
|
|
1238
|
+
* @example
|
|
1239
|
+
*
|
|
1240
|
+
* ```ts
|
|
1241
|
+
* // Retry someResultFn up to 3 times until Ok is returned, logging each
|
|
1242
|
+
* // attempt and aborting early if the error code is "UNAUTHORIZED".
|
|
1243
|
+
* const result = await Result.$retry(someResultFn)
|
|
1244
|
+
* .$times(3)
|
|
1245
|
+
* .$handle(({ error, attempt, abort }) => {
|
|
1246
|
+
* console.info(`Attempt ${attempt} failed: ${error}`);
|
|
1247
|
+
* if (error === "UNAUTHORIZED") {
|
|
1248
|
+
* abort();
|
|
1249
|
+
* }
|
|
1250
|
+
* });
|
|
1251
|
+
* ```
|
|
1252
|
+
*/
|
|
1253
|
+
$handle(f) {
|
|
1254
|
+
__classPrivateFieldSet(this, _ResultRetry_handler, f, "f");
|
|
1255
|
+
return this;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Returns {@link ResultAsync} which resolves to this retried {@link Result}.
|
|
1259
|
+
*
|
|
1260
|
+
* @example
|
|
1261
|
+
*
|
|
1262
|
+
* ```ts
|
|
1263
|
+
* const result: Result<string, SomeError> = await Result
|
|
1264
|
+
* .$retry(someResultFn)
|
|
1265
|
+
* .$times(3)
|
|
1266
|
+
* .$delay(100)
|
|
1267
|
+
* .$async()
|
|
1268
|
+
* .$andThen((message) => `Success: ${message}`)
|
|
1269
|
+
* .$mapErr((code) => new SomeError({ code }));
|
|
1270
|
+
* ```
|
|
1271
|
+
*/
|
|
1272
|
+
$async() {
|
|
1273
|
+
return new ResultAsync(this);
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Returns a `Promise` which resolves to this retried {@link Result}.
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
*
|
|
1280
|
+
* ```ts
|
|
1281
|
+
* const promise: Promise<Result<string, Error>> = Result
|
|
1282
|
+
* .$retry(someResultFn)
|
|
1283
|
+
* .$times(3)
|
|
1284
|
+
* .$promise();
|
|
1285
|
+
* ```
|
|
1286
|
+
*/
|
|
1287
|
+
$promise() {
|
|
1288
|
+
return Promise.resolve(this);
|
|
1289
|
+
}
|
|
1290
|
+
async drain() {
|
|
1291
|
+
var _b;
|
|
1292
|
+
while (__classPrivateFieldGet(this, _ResultRetry_attempt, "f") < __classPrivateFieldGet(this, _ResultRetry_times, "f")) {
|
|
1293
|
+
const result = asResult(await __classPrivateFieldGet(this, _ResultRetry_f, "f").call(this));
|
|
1294
|
+
__classPrivateFieldSet(this, _ResultRetry_attempt, (_b = __classPrivateFieldGet(this, _ResultRetry_attempt, "f"), _b++, _b), "f");
|
|
1295
|
+
if (result.$isOk()) {
|
|
1296
|
+
return result;
|
|
1297
|
+
}
|
|
1298
|
+
if (__classPrivateFieldGet(this, _ResultRetry_handler, "f")) {
|
|
1299
|
+
await __classPrivateFieldGet(this, _ResultRetry_handler, "f").call(this, {
|
|
1300
|
+
error: result[0],
|
|
1301
|
+
attempt: __classPrivateFieldGet(this, _ResultRetry_attempt, "f"),
|
|
1302
|
+
abort: __classPrivateFieldGet(this, _ResultRetry_abort, "f"),
|
|
1303
|
+
});
|
|
1304
|
+
}
|
|
1305
|
+
if (__classPrivateFieldGet(this, _ResultRetry_aborted, "f") || __classPrivateFieldGet(this, _ResultRetry_attempt, "f") >= __classPrivateFieldGet(this, _ResultRetry_times, "f")) {
|
|
1306
|
+
return result;
|
|
1307
|
+
}
|
|
1308
|
+
const delay = _a.integer(__classPrivateFieldGet(this, _ResultRetry_getDelay, "f").call(this, __classPrivateFieldGet(this, _ResultRetry_attempt, "f")));
|
|
1309
|
+
if (delay > 0) {
|
|
1310
|
+
await _a.delay(delay);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
/* v8 ignore next */
|
|
1314
|
+
throw new Error("Retuple: Unreachable code executed");
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
_a = ResultRetry, _ResultRetry_f = new WeakMap(), _ResultRetry_promise = new WeakMap(), _ResultRetry_times = new WeakMap(), _ResultRetry_attempt = new WeakMap(), _ResultRetry_aborted = new WeakMap(), _ResultRetry_abort = new WeakMap(), _ResultRetry_getDelay = new WeakMap(), _ResultRetry_handler = new WeakMap();
|
|
1318
|
+
ResultRetry.MAX_TIMEOUT = 3600000;
|
|
1319
|
+
ResultRetry.MAX_RETRY = 100;
|
|
1320
|
+
function asResult(resultLike) {
|
|
1321
|
+
return resultLike[symbol_js_1.ResultLikeSymbol]();
|
|
1322
|
+
}
|
|
1111
1323
|
function ensureError(err) {
|
|
1112
1324
|
if (err instanceof Error) {
|
|
1113
1325
|
return err;
|
|
1114
1326
|
}
|
|
1115
|
-
return new
|
|
1327
|
+
return new RetupleCaughtValueError(err);
|
|
1116
1328
|
}
|
|
1117
1329
|
function mapTrue() {
|
|
1118
1330
|
return true;
|