@oscarpalmer/atoms 0.158.0 → 0.159.0
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/atoms.full.js +161 -154
- package/dist/index.js +3 -3
- package/dist/promise/delay.js +1 -1
- package/dist/promise/helpers.js +6 -1
- package/dist/promise/index.js +7 -3
- package/dist/promise/misc.js +1 -1
- package/dist/promise/timed.js +1 -1
- package/dist/result/index.js +3 -3
- package/package.json +1 -1
- package/src/promise/helpers.ts +13 -3
- package/src/promise/index.ts +89 -19
- package/src/promise/misc.ts +1 -4
- package/src/promise/models.ts +18 -12
- package/types/promise/helpers.d.ts +3 -1
- package/types/promise/index.d.ts +52 -5
- package/types/promise/misc.d.ts +1 -1
- package/types/promise/models.d.ts +15 -11
package/dist/atoms.full.js
CHANGED
|
@@ -3275,6 +3275,105 @@ function roundNumber(callback, value, decimals) {
|
|
|
3275
3275
|
function sum(array, key) {
|
|
3276
3276
|
return getAggregated("sum", array, key);
|
|
3277
3277
|
}
|
|
3278
|
+
async function asyncMatchResult(result, first, error) {
|
|
3279
|
+
let value;
|
|
3280
|
+
if (typeof result === "function") value = await result();
|
|
3281
|
+
else if (result instanceof Promise) value = await result;
|
|
3282
|
+
else value = result;
|
|
3283
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3284
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
3285
|
+
const okHandler = hasObj ? first.ok : first;
|
|
3286
|
+
const errorHandler = hasObj ? first.error : error;
|
|
3287
|
+
if (isOk(value)) return okHandler(value.value);
|
|
3288
|
+
return errorHandler(value.error, value.original);
|
|
3289
|
+
}
|
|
3290
|
+
function matchResult(result, first, error) {
|
|
3291
|
+
const value = typeof result === "function" ? result() : result;
|
|
3292
|
+
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3293
|
+
const hasObj = typeof first === "object" && first !== null;
|
|
3294
|
+
const okHandler = hasObj ? first.ok : first;
|
|
3295
|
+
const errorHandler = hasObj ? first.error : error;
|
|
3296
|
+
if (isOk(value)) return okHandler(value.value);
|
|
3297
|
+
return errorHandler(value.error, value.original);
|
|
3298
|
+
}
|
|
3299
|
+
matchResult.async = asyncMatchResult;
|
|
3300
|
+
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
3301
|
+
function error(value, original) {
|
|
3302
|
+
return getError(value, original);
|
|
3303
|
+
}
|
|
3304
|
+
function getError(value, original) {
|
|
3305
|
+
const errorResult = {
|
|
3306
|
+
error: value,
|
|
3307
|
+
ok: false
|
|
3308
|
+
};
|
|
3309
|
+
if (original instanceof Error) errorResult.original = original;
|
|
3310
|
+
return errorResult;
|
|
3311
|
+
}
|
|
3312
|
+
/**
|
|
3313
|
+
* Creates an ok result
|
|
3314
|
+
* @param value Value
|
|
3315
|
+
* @returns Ok result
|
|
3316
|
+
*/
|
|
3317
|
+
function ok(value) {
|
|
3318
|
+
return {
|
|
3319
|
+
ok: true,
|
|
3320
|
+
value
|
|
3321
|
+
};
|
|
3322
|
+
}
|
|
3323
|
+
/**
|
|
3324
|
+
* Converts a result to a promise
|
|
3325
|
+
*
|
|
3326
|
+
* Resolves if ok, rejects for error
|
|
3327
|
+
* @param result Result to convert
|
|
3328
|
+
* @returns Promised result
|
|
3329
|
+
*/
|
|
3330
|
+
async function toPromise(result) {
|
|
3331
|
+
const actual = typeof result === "function" ? result() : result;
|
|
3332
|
+
if (!isResult(actual)) return Promise.reject(new Error(MESSAGE_PROMISE_RESULT));
|
|
3333
|
+
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
3334
|
+
}
|
|
3335
|
+
function unwrap(value, defaultValue) {
|
|
3336
|
+
return isOk(value) ? value.value : defaultValue;
|
|
3337
|
+
}
|
|
3338
|
+
const MESSAGE_PROMISE_RESULT = "toPromise expected to receive a Result";
|
|
3339
|
+
function attemptAsyncFlow(...fns) {
|
|
3340
|
+
let Flow;
|
|
3341
|
+
return (...args) => attempt.async(() => {
|
|
3342
|
+
Flow ??= flow.async(...fns);
|
|
3343
|
+
return Flow(...args.map((value) => {
|
|
3344
|
+
if (isError(value)) throw value.error;
|
|
3345
|
+
return isOk(value) ? value.value : value;
|
|
3346
|
+
}));
|
|
3347
|
+
});
|
|
3348
|
+
}
|
|
3349
|
+
function attemptFlow(...fns) {
|
|
3350
|
+
let Flow;
|
|
3351
|
+
return (...args) => attempt(() => {
|
|
3352
|
+
Flow ??= flow(...fns);
|
|
3353
|
+
return Flow(...args.map((value) => {
|
|
3354
|
+
if (isError(value)) throw value.error;
|
|
3355
|
+
return isOk(value) ? value.value : value;
|
|
3356
|
+
}));
|
|
3357
|
+
});
|
|
3358
|
+
}
|
|
3359
|
+
attemptFlow.async = attemptAsyncFlow;
|
|
3360
|
+
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
3361
|
+
return attempt.async(() => {
|
|
3362
|
+
if (isError(initial)) throw initial.error;
|
|
3363
|
+
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3364
|
+
if (first == null) return value;
|
|
3365
|
+
return pipe.async(value, ...[first, ...seconds]);
|
|
3366
|
+
});
|
|
3367
|
+
}
|
|
3368
|
+
function attemptPipe(initial, first, ...seconds) {
|
|
3369
|
+
return attempt(() => {
|
|
3370
|
+
if (isError(initial)) throw initial.error;
|
|
3371
|
+
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3372
|
+
if (first == null) return value;
|
|
3373
|
+
return pipe(value, ...[first, ...seconds]);
|
|
3374
|
+
});
|
|
3375
|
+
}
|
|
3376
|
+
attemptPipe.async = attemptAsyncPipe;
|
|
3278
3377
|
var CancelablePromise = class extends Promise {
|
|
3279
3378
|
#rejector;
|
|
3280
3379
|
constructor(executor) {
|
|
@@ -3311,6 +3410,60 @@ const PROMISE_STRATEGY_ALL = new Set(["complete", "first"]);
|
|
|
3311
3410
|
const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
3312
3411
|
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
3313
3412
|
const PROMISE_TYPE_REJECTED = "rejected";
|
|
3413
|
+
/**
|
|
3414
|
+
* Create a cancelable promise
|
|
3415
|
+
* @param executor Executor function for the promise
|
|
3416
|
+
* @returns Cancelable promise
|
|
3417
|
+
*/
|
|
3418
|
+
function cancelable(executor) {
|
|
3419
|
+
return new CancelablePromise(executor);
|
|
3420
|
+
}
|
|
3421
|
+
function handleResult(status, parameters) {
|
|
3422
|
+
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3423
|
+
if (signal?.aborted ?? false) return;
|
|
3424
|
+
if (!complete && status === "rejected") {
|
|
3425
|
+
settlePromise(abort, handlers.reject, value, signal);
|
|
3426
|
+
return;
|
|
3427
|
+
}
|
|
3428
|
+
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
3429
|
+
status,
|
|
3430
|
+
value
|
|
3431
|
+
} : {
|
|
3432
|
+
status,
|
|
3433
|
+
reason: value
|
|
3434
|
+
};
|
|
3435
|
+
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
3436
|
+
}
|
|
3437
|
+
function settlePromise(aborter, settler, value, signal) {
|
|
3438
|
+
signal?.removeEventListener(PROMISE_EVENT_NAME, aborter);
|
|
3439
|
+
settler(value);
|
|
3440
|
+
}
|
|
3441
|
+
async function toResult(value) {
|
|
3442
|
+
const actual = typeof value === "function" ? value() : value;
|
|
3443
|
+
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
3444
|
+
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
3445
|
+
}
|
|
3446
|
+
async function asyncAttempt(value, err) {
|
|
3447
|
+
try {
|
|
3448
|
+
let result = typeof value === "function" ? value() : await value;
|
|
3449
|
+
if (result instanceof Promise) result = await result;
|
|
3450
|
+
return ok(result);
|
|
3451
|
+
} catch (thrown) {
|
|
3452
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3453
|
+
}
|
|
3454
|
+
}
|
|
3455
|
+
function attempt(callback, err) {
|
|
3456
|
+
try {
|
|
3457
|
+
return ok(callback());
|
|
3458
|
+
} catch (thrown) {
|
|
3459
|
+
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3460
|
+
}
|
|
3461
|
+
}
|
|
3462
|
+
attempt.async = asyncAttempt;
|
|
3463
|
+
attempt.flow = attemptFlow;
|
|
3464
|
+
attempt.match = matchResult;
|
|
3465
|
+
attempt.pipe = attemptPipe;
|
|
3466
|
+
attempt.promise = attemptPromise;
|
|
3314
3467
|
function getNumberOrDefault$1(value) {
|
|
3315
3468
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
3316
3469
|
}
|
|
@@ -3338,6 +3491,9 @@ function getPromisesOptions(input) {
|
|
|
3338
3491
|
strategy: getStrategyOrDefault(options.strategy)
|
|
3339
3492
|
};
|
|
3340
3493
|
}
|
|
3494
|
+
function getResultsFromPromises(promised) {
|
|
3495
|
+
return promised.map((result) => isFulfilled(result) ? ok(result.value) : error(result.reason));
|
|
3496
|
+
}
|
|
3341
3497
|
function getStrategyOrDefault(value) {
|
|
3342
3498
|
return PROMISE_STRATEGY_ALL.has(value) ? value : PROMISE_STRATEGY_DEFAULT;
|
|
3343
3499
|
}
|
|
@@ -3360,77 +3516,6 @@ function isRejected(value) {
|
|
|
3360
3516
|
function isType(value, type) {
|
|
3361
3517
|
return typeof value === "object" && value !== null && value.status === type;
|
|
3362
3518
|
}
|
|
3363
|
-
function error(value, original) {
|
|
3364
|
-
return getError(value, original);
|
|
3365
|
-
}
|
|
3366
|
-
function getError(value, original) {
|
|
3367
|
-
const errorResult = {
|
|
3368
|
-
error: value,
|
|
3369
|
-
ok: false
|
|
3370
|
-
};
|
|
3371
|
-
if (original instanceof Error) errorResult.original = original;
|
|
3372
|
-
return errorResult;
|
|
3373
|
-
}
|
|
3374
|
-
/**
|
|
3375
|
-
* Creates an ok result
|
|
3376
|
-
* @param value Value
|
|
3377
|
-
* @returns Ok result
|
|
3378
|
-
*/
|
|
3379
|
-
function ok(value) {
|
|
3380
|
-
return {
|
|
3381
|
-
ok: true,
|
|
3382
|
-
value
|
|
3383
|
-
};
|
|
3384
|
-
}
|
|
3385
|
-
/**
|
|
3386
|
-
* Converts a result to a promise
|
|
3387
|
-
*
|
|
3388
|
-
* Resolves if ok, rejects for error
|
|
3389
|
-
* @param result Result to convert
|
|
3390
|
-
* @returns Promised result
|
|
3391
|
-
*/
|
|
3392
|
-
async function toPromise(result) {
|
|
3393
|
-
const actual = typeof result === "function" ? result() : result;
|
|
3394
|
-
if (!isResult(actual)) return Promise.reject(new Error(MESSAGE_PROMISE_RESULT));
|
|
3395
|
-
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
3396
|
-
}
|
|
3397
|
-
function unwrap(value, defaultValue) {
|
|
3398
|
-
return isOk(value) ? value.value : defaultValue;
|
|
3399
|
-
}
|
|
3400
|
-
const MESSAGE_PROMISE_RESULT = "toPromise expected to receive a Result";
|
|
3401
|
-
/**
|
|
3402
|
-
* Create a cancelable promise
|
|
3403
|
-
* @param executor Executor function for the promise
|
|
3404
|
-
* @returns Cancelable promise
|
|
3405
|
-
*/
|
|
3406
|
-
function cancelable(executor) {
|
|
3407
|
-
return new CancelablePromise(executor);
|
|
3408
|
-
}
|
|
3409
|
-
function handleResult(status, parameters) {
|
|
3410
|
-
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3411
|
-
if (signal?.aborted ?? false) return;
|
|
3412
|
-
if (!complete && status === "rejected") {
|
|
3413
|
-
settlePromise(abort, handlers.reject, value, signal);
|
|
3414
|
-
return;
|
|
3415
|
-
}
|
|
3416
|
-
data.result[index] = !complete ? value : status === "fulfilled" ? {
|
|
3417
|
-
status,
|
|
3418
|
-
value
|
|
3419
|
-
} : {
|
|
3420
|
-
status,
|
|
3421
|
-
reason: value
|
|
3422
|
-
};
|
|
3423
|
-
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
3424
|
-
}
|
|
3425
|
-
function settlePromise(aborter, settler, value, signal) {
|
|
3426
|
-
signal?.removeEventListener(PROMISE_EVENT_NAME, aborter);
|
|
3427
|
-
settler(value);
|
|
3428
|
-
}
|
|
3429
|
-
async function toResult(value) {
|
|
3430
|
-
const actual = typeof value === "function" ? value() : value;
|
|
3431
|
-
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
3432
|
-
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
3433
|
-
}
|
|
3434
3519
|
async function getTimedPromise(promise, time, signal) {
|
|
3435
3520
|
function abort() {
|
|
3436
3521
|
timer.cancel();
|
|
@@ -3507,7 +3592,7 @@ async function promises(items, options) {
|
|
|
3507
3592
|
if (!Array.isArray(items)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
3508
3593
|
const actual = items.filter((item) => item instanceof Promise);
|
|
3509
3594
|
const { length } = actual;
|
|
3510
|
-
if (length === 0) return
|
|
3595
|
+
if (length === 0) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
3511
3596
|
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
3512
3597
|
function abort() {
|
|
3513
3598
|
handlers.reject(signal.reason);
|
|
@@ -3542,6 +3627,10 @@ async function promises(items, options) {
|
|
|
3542
3627
|
}));
|
|
3543
3628
|
});
|
|
3544
3629
|
}
|
|
3630
|
+
promises.result = resultPromises;
|
|
3631
|
+
async function resultPromises(items, signal) {
|
|
3632
|
+
return promises(items, signal).then(getResultsFromPromises);
|
|
3633
|
+
}
|
|
3545
3634
|
/**
|
|
3546
3635
|
* Convert a query string to a plain _(nested)_ object
|
|
3547
3636
|
* @param query Query string to convert
|
|
@@ -3869,88 +3958,6 @@ const ALPHABET = "abcdefghijklmnopqrstuvwxyz";
|
|
|
3869
3958
|
const BOOLEAN_MODIFIER = .5;
|
|
3870
3959
|
const HEX_CHARACTERS = "0123456789ABCDEF";
|
|
3871
3960
|
const HEX_MAXIMUM = 15;
|
|
3872
|
-
async function asyncMatchResult(result, first, error) {
|
|
3873
|
-
let value;
|
|
3874
|
-
if (typeof result === "function") value = await result();
|
|
3875
|
-
else if (result instanceof Promise) value = await result;
|
|
3876
|
-
else value = result;
|
|
3877
|
-
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3878
|
-
const hasObj = typeof first === "object" && first !== null;
|
|
3879
|
-
const okHandler = hasObj ? first.ok : first;
|
|
3880
|
-
const errorHandler = hasObj ? first.error : error;
|
|
3881
|
-
if (isOk(value)) return okHandler(value.value);
|
|
3882
|
-
return errorHandler(value.error, value.original);
|
|
3883
|
-
}
|
|
3884
|
-
function matchResult(result, first, error) {
|
|
3885
|
-
const value = typeof result === "function" ? result() : result;
|
|
3886
|
-
if (!isResult(value)) throw new Error(MESSAGE_RESULT);
|
|
3887
|
-
const hasObj = typeof first === "object" && first !== null;
|
|
3888
|
-
const okHandler = hasObj ? first.ok : first;
|
|
3889
|
-
const errorHandler = hasObj ? first.error : error;
|
|
3890
|
-
if (isOk(value)) return okHandler(value.value);
|
|
3891
|
-
return errorHandler(value.error, value.original);
|
|
3892
|
-
}
|
|
3893
|
-
matchResult.async = asyncMatchResult;
|
|
3894
|
-
const MESSAGE_RESULT = "`result.match` expected a Result or a function that returns a Result";
|
|
3895
|
-
function attemptAsyncFlow(...fns) {
|
|
3896
|
-
let Flow;
|
|
3897
|
-
return (...args) => attempt.async(() => {
|
|
3898
|
-
Flow ??= flow.async(...fns);
|
|
3899
|
-
return Flow(...args.map((value) => {
|
|
3900
|
-
if (isError(value)) throw value.error;
|
|
3901
|
-
return isOk(value) ? value.value : value;
|
|
3902
|
-
}));
|
|
3903
|
-
});
|
|
3904
|
-
}
|
|
3905
|
-
function attemptFlow(...fns) {
|
|
3906
|
-
let Flow;
|
|
3907
|
-
return (...args) => attempt(() => {
|
|
3908
|
-
Flow ??= flow(...fns);
|
|
3909
|
-
return Flow(...args.map((value) => {
|
|
3910
|
-
if (isError(value)) throw value.error;
|
|
3911
|
-
return isOk(value) ? value.value : value;
|
|
3912
|
-
}));
|
|
3913
|
-
});
|
|
3914
|
-
}
|
|
3915
|
-
attemptFlow.async = attemptAsyncFlow;
|
|
3916
|
-
async function attemptAsyncPipe(initial, first, ...seconds) {
|
|
3917
|
-
return attempt.async(() => {
|
|
3918
|
-
if (isError(initial)) throw initial.error;
|
|
3919
|
-
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3920
|
-
if (first == null) return value;
|
|
3921
|
-
return pipe.async(value, ...[first, ...seconds]);
|
|
3922
|
-
});
|
|
3923
|
-
}
|
|
3924
|
-
function attemptPipe(initial, first, ...seconds) {
|
|
3925
|
-
return attempt(() => {
|
|
3926
|
-
if (isError(initial)) throw initial.error;
|
|
3927
|
-
const value = typeof initial === "function" ? initial() : isOk(initial) ? initial.value : initial;
|
|
3928
|
-
if (first == null) return value;
|
|
3929
|
-
return pipe(value, ...[first, ...seconds]);
|
|
3930
|
-
});
|
|
3931
|
-
}
|
|
3932
|
-
attemptPipe.async = attemptAsyncPipe;
|
|
3933
|
-
async function asyncAttempt(value, err) {
|
|
3934
|
-
try {
|
|
3935
|
-
let result = typeof value === "function" ? value() : await value;
|
|
3936
|
-
if (result instanceof Promise) result = await result;
|
|
3937
|
-
return ok(result);
|
|
3938
|
-
} catch (thrown) {
|
|
3939
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3940
|
-
}
|
|
3941
|
-
}
|
|
3942
|
-
function attempt(callback, err) {
|
|
3943
|
-
try {
|
|
3944
|
-
return ok(callback());
|
|
3945
|
-
} catch (thrown) {
|
|
3946
|
-
return getError(err ?? thrown, err == null ? void 0 : thrown);
|
|
3947
|
-
}
|
|
3948
|
-
}
|
|
3949
|
-
attempt.async = asyncAttempt;
|
|
3950
|
-
attempt.flow = attemptFlow;
|
|
3951
|
-
attempt.match = matchResult;
|
|
3952
|
-
attempt.pipe = attemptPipe;
|
|
3953
|
-
attempt.promise = attemptPromise;
|
|
3954
3961
|
/**
|
|
3955
3962
|
* - A Set with a maximum size
|
|
3956
3963
|
* - Behavior is similar to a _LRU_-cache, where the oldest values are removed
|
package/dist/index.js
CHANGED
|
@@ -65,16 +65,16 @@ import { merge } from "./value/merge.js";
|
|
|
65
65
|
import { isEmpty, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
|
|
66
66
|
import { logger } from "./logger.js";
|
|
67
67
|
import { average, ceil, count, floor, median, min, round, sum } from "./math.js";
|
|
68
|
-
import { CancelablePromise, PromiseTimeoutError } from "./promise/models.js";
|
|
69
|
-
import { isFulfilled, isRejected } from "./promise/helpers.js";
|
|
70
68
|
import { error, ok, toPromise, unwrap } from "./result/misc.js";
|
|
69
|
+
import { CancelablePromise, PromiseTimeoutError } from "./promise/models.js";
|
|
71
70
|
import { cancelable, toResult } from "./promise/misc.js";
|
|
71
|
+
import { attempt } from "./result/index.js";
|
|
72
|
+
import { isFulfilled, isRejected } from "./promise/helpers.js";
|
|
72
73
|
import { timed } from "./promise/timed.js";
|
|
73
74
|
import { delay } from "./promise/delay.js";
|
|
74
75
|
import { attemptPromise, promises } from "./promise/index.js";
|
|
75
76
|
import { fromQuery, toQuery } from "./query.js";
|
|
76
77
|
import { QueueError, queue } from "./queue.js";
|
|
77
78
|
import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
|
|
78
|
-
import { attempt } from "./result/index.js";
|
|
79
79
|
import { SizedSet } from "./sized/set.js";
|
|
80
80
|
export { CancelablePromise, PromiseTimeoutError, QueueError, RetryError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, difference, drop, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hasValue, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, retry, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, slice, smush, snakeCase, sort, splice, startsWith, sum, take, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/promise/delay.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.js";
|
|
2
2
|
import { PROMISE_ABORT_OPTIONS } from "./models.js";
|
|
3
|
-
import { getPromiseOptions } from "./helpers.js";
|
|
4
3
|
import { settlePromise } from "./misc.js";
|
|
4
|
+
import { getPromiseOptions } from "./helpers.js";
|
|
5
5
|
function delay(options) {
|
|
6
6
|
const { signal, time } = getPromiseOptions(options);
|
|
7
7
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
package/dist/promise/helpers.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { error, ok } from "../result/misc.js";
|
|
1
2
|
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.js";
|
|
3
|
+
import "../result/index.js";
|
|
2
4
|
function getNumberOrDefault(value) {
|
|
3
5
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
4
6
|
}
|
|
@@ -26,6 +28,9 @@ function getPromisesOptions(input) {
|
|
|
26
28
|
strategy: getStrategyOrDefault(options.strategy)
|
|
27
29
|
};
|
|
28
30
|
}
|
|
31
|
+
function getResultsFromPromises(promised) {
|
|
32
|
+
return promised.map((result) => isFulfilled(result) ? ok(result.value) : error(result.reason));
|
|
33
|
+
}
|
|
29
34
|
function getStrategyOrDefault(value) {
|
|
30
35
|
return PROMISE_STRATEGY_ALL.has(value) ? value : PROMISE_STRATEGY_DEFAULT;
|
|
31
36
|
}
|
|
@@ -48,4 +53,4 @@ function isRejected(value) {
|
|
|
48
53
|
function isType(value, type) {
|
|
49
54
|
return typeof value === "object" && value !== null && value.status === type;
|
|
50
55
|
}
|
|
51
|
-
export { getPromiseOptions, getPromisesOptions, getStrategyOrDefault, isFulfilled, isRejected };
|
|
56
|
+
export { getPromiseOptions, getPromisesOptions, getResultsFromPromises, getStrategyOrDefault, isFulfilled, isRejected };
|
package/dist/promise/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { CancelablePromise, PROMISE_ABORT_OPTIONS, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_PROMISES, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError } from "./models.js";
|
|
2
|
-
import { getPromiseOptions, getPromisesOptions, isFulfilled, isRejected } from "./helpers.js";
|
|
3
1
|
import { toPromise } from "../result/misc.js";
|
|
2
|
+
import { CancelablePromise, PROMISE_ABORT_OPTIONS, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_PROMISES, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError } from "./models.js";
|
|
4
3
|
import { cancelable, handleResult, settlePromise, toResult } from "./misc.js";
|
|
4
|
+
import { getPromiseOptions, getPromisesOptions, getResultsFromPromises, isFulfilled, isRejected } from "./helpers.js";
|
|
5
5
|
import { getTimedPromise, timed } from "./timed.js";
|
|
6
6
|
import { delay } from "./delay.js";
|
|
7
7
|
async function attemptPromise(value, options) {
|
|
@@ -35,7 +35,7 @@ async function promises(items, options) {
|
|
|
35
35
|
if (!Array.isArray(items)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
36
36
|
const actual = items.filter((item) => item instanceof Promise);
|
|
37
37
|
const { length } = actual;
|
|
38
|
-
if (length === 0) return
|
|
38
|
+
if (length === 0) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
39
39
|
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
40
40
|
function abort() {
|
|
41
41
|
handlers.reject(signal.reason);
|
|
@@ -70,4 +70,8 @@ async function promises(items, options) {
|
|
|
70
70
|
}));
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
|
+
promises.result = resultPromises;
|
|
74
|
+
async function resultPromises(items, signal) {
|
|
75
|
+
return promises(items, signal).then(getResultsFromPromises);
|
|
76
|
+
}
|
|
73
77
|
export { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, toPromise as fromResult, isFulfilled, isRejected, promises, timed, toResult };
|
package/dist/promise/misc.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { CancelablePromise, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_RESULT } from "./models.js";
|
|
2
1
|
import { error, ok } from "../result/misc.js";
|
|
2
|
+
import { CancelablePromise, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_RESULT } from "./models.js";
|
|
3
3
|
/**
|
|
4
4
|
* Create a cancelable promise
|
|
5
5
|
* @param executor Executor function for the promise
|
package/dist/promise/timed.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TIMER_WAIT, getTimer } from "../internal/function/timer.js";
|
|
2
2
|
import { PROMISE_ABORT_OPTIONS, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_TIMED, PromiseTimeoutError } from "./models.js";
|
|
3
|
-
import { getPromiseOptions } from "./helpers.js";
|
|
4
3
|
import { settlePromise } from "./misc.js";
|
|
4
|
+
import { getPromiseOptions } from "./helpers.js";
|
|
5
5
|
async function getTimedPromise(promise, time, signal) {
|
|
6
6
|
function abort() {
|
|
7
7
|
timer.cancel();
|
package/dist/result/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isError, isOk, isResult } from "../internal/result.js";
|
|
2
|
-
import { error, getError, ok, toPromise, unwrap } from "./misc.js";
|
|
3
|
-
import { toResult } from "../promise/misc.js";
|
|
4
|
-
import { attemptPromise } from "../promise/index.js";
|
|
5
2
|
import { matchResult } from "./match.js";
|
|
3
|
+
import { error, getError, ok, toPromise, unwrap } from "./misc.js";
|
|
6
4
|
import { attemptFlow } from "./work/flow.js";
|
|
7
5
|
import { attemptPipe } from "./work/pipe.js";
|
|
6
|
+
import { toResult } from "../promise/misc.js";
|
|
7
|
+
import { attemptPromise } from "../promise/index.js";
|
|
8
8
|
async function asyncAttempt(value, err) {
|
|
9
9
|
try {
|
|
10
10
|
let result = typeof value === "function" ? value() : await value;
|
package/package.json
CHANGED
package/src/promise/helpers.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import type {RequiredKeys} from '../models';
|
|
2
|
+
import {error, ok} from '../result';
|
|
3
|
+
import type {Result} from '../result/models';
|
|
2
4
|
import {
|
|
3
|
-
PROMISE_STRATEGY_DEFAULT,
|
|
4
5
|
PROMISE_STRATEGY_ALL,
|
|
6
|
+
PROMISE_STRATEGY_DEFAULT,
|
|
5
7
|
PROMISE_TYPE_FULFILLED,
|
|
6
8
|
PROMISE_TYPE_REJECTED,
|
|
7
9
|
type FulfilledPromise,
|
|
8
10
|
type PromiseOptions,
|
|
9
11
|
type PromisesOptions,
|
|
10
|
-
type PromisesResultItem,
|
|
11
12
|
type PromiseStrategy,
|
|
13
|
+
type PromisesValue,
|
|
12
14
|
type RejectedPromise,
|
|
13
15
|
} from './models';
|
|
14
16
|
|
|
@@ -56,6 +58,14 @@ export function getPromisesOptions(input: unknown): RequiredKeys<PromisesOptions
|
|
|
56
58
|
};
|
|
57
59
|
}
|
|
58
60
|
|
|
61
|
+
export function getResultsFromPromises<Value>(
|
|
62
|
+
promised: PromisesValue<Value>[],
|
|
63
|
+
): Result<Value>[] {
|
|
64
|
+
return promised.map(result =>
|
|
65
|
+
isFulfilled(result) ? ok(result.value) : error(result.reason),
|
|
66
|
+
) as Result<Value>[];
|
|
67
|
+
}
|
|
68
|
+
|
|
59
69
|
export function getStrategyOrDefault(value: unknown): PromiseStrategy {
|
|
60
70
|
return PROMISE_STRATEGY_ALL.has(value as PromiseStrategy)
|
|
61
71
|
? (value as PromiseStrategy)
|
|
@@ -84,7 +94,7 @@ function isType(value: unknown, type: string): boolean {
|
|
|
84
94
|
return (
|
|
85
95
|
typeof value === 'object' &&
|
|
86
96
|
value !== null &&
|
|
87
|
-
(value as
|
|
97
|
+
(value as PromisesValue<unknown>).status === type
|
|
88
98
|
);
|
|
89
99
|
}
|
|
90
100
|
|
package/src/promise/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type {Result} from '../result/models';
|
|
2
|
+
import {getPromiseOptions, getPromisesOptions, getResultsFromPromises} from './helpers';
|
|
2
3
|
import {handleResult, settlePromise} from './misc';
|
|
3
4
|
import {
|
|
4
5
|
PROMISE_ABORT_OPTIONS,
|
|
@@ -11,9 +12,11 @@ import {
|
|
|
11
12
|
type PromiseData,
|
|
12
13
|
type PromiseHandlers,
|
|
13
14
|
type PromiseOptions,
|
|
14
|
-
type
|
|
15
|
+
type PromisesItems,
|
|
15
16
|
type PromisesOptions,
|
|
16
17
|
type PromisesResult,
|
|
18
|
+
type PromisesValue,
|
|
19
|
+
type PromisesValues,
|
|
17
20
|
} from './models';
|
|
18
21
|
import {getTimedPromise} from './timed';
|
|
19
22
|
|
|
@@ -111,9 +114,22 @@ export async function attemptPromise<Value>(
|
|
|
111
114
|
* @returns List of results
|
|
112
115
|
*/
|
|
113
116
|
export async function promises<Items extends unknown[], Options extends PromisesOptions>(
|
|
114
|
-
items:
|
|
117
|
+
items: [...Items],
|
|
115
118
|
options?: Options,
|
|
116
|
-
): Promise<Options['strategy'] extends 'first' ? Items :
|
|
119
|
+
): Promise<Options['strategy'] extends 'first' ? Items : PromisesValues<PromisesItems<Items>>>;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
123
|
+
*
|
|
124
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
125
|
+
* @param items List of promises
|
|
126
|
+
* @param options Options for handling the promises
|
|
127
|
+
* @returns List of results
|
|
128
|
+
*/
|
|
129
|
+
export async function promises<Value, Options extends PromisesOptions>(
|
|
130
|
+
items: Promise<Value>[],
|
|
131
|
+
options?: Options,
|
|
132
|
+
): Promise<Options['strategy'] extends 'first' ? Value[] : PromisesValue<Value>[]>;
|
|
117
133
|
|
|
118
134
|
/**
|
|
119
135
|
* Handle a list of promises, returning their results in an ordered array.
|
|
@@ -124,9 +140,19 @@ export async function promises<Items extends unknown[], Options extends Promises
|
|
|
124
140
|
* @returns List of results
|
|
125
141
|
*/
|
|
126
142
|
export async function promises<Items extends unknown[]>(
|
|
127
|
-
items:
|
|
143
|
+
items: [...Items],
|
|
128
144
|
strategy: 'first',
|
|
129
|
-
): Promise<Items
|
|
145
|
+
): Promise<PromisesItems<Items>>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
149
|
+
*
|
|
150
|
+
* If any promise in the list is rejected, the whole function will reject
|
|
151
|
+
* @param items List of promises
|
|
152
|
+
* @param strategy Strategy for handling the promises; rejects on the first error encountered
|
|
153
|
+
* @returns List of results
|
|
154
|
+
*/
|
|
155
|
+
export async function promises<Value>(items: Promise<Value>[], strategy: 'first'): Promise<Value[]>;
|
|
130
156
|
|
|
131
157
|
/**
|
|
132
158
|
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
@@ -135,14 +161,22 @@ export async function promises<Items extends unknown[]>(
|
|
|
135
161
|
* @returns List of results
|
|
136
162
|
*/
|
|
137
163
|
export async function promises<Items extends unknown[]>(
|
|
138
|
-
items:
|
|
164
|
+
items: [...Items],
|
|
139
165
|
signal?: AbortSignal,
|
|
140
|
-
): Promise<
|
|
166
|
+
): Promise<PromisesValues<PromisesItems<Items>>>;
|
|
141
167
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
170
|
+
* @param items List of promises
|
|
171
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
172
|
+
* @returns List of results
|
|
173
|
+
*/
|
|
174
|
+
export async function promises<Value>(
|
|
175
|
+
items: Promise<Value>[],
|
|
176
|
+
signal?: AbortSignal,
|
|
177
|
+
): Promise<PromisesValue<Value>[]>;
|
|
178
|
+
|
|
179
|
+
export async function promises(items: unknown[], options?: unknown): Promise<unknown[]> {
|
|
146
180
|
const {signal, strategy} = getPromisesOptions(options);
|
|
147
181
|
|
|
148
182
|
if (signal?.aborted ?? false) {
|
|
@@ -157,7 +191,7 @@ export async function promises<Items extends unknown[]>(
|
|
|
157
191
|
const {length} = actual;
|
|
158
192
|
|
|
159
193
|
if (length === 0) {
|
|
160
|
-
return
|
|
194
|
+
return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
161
195
|
}
|
|
162
196
|
|
|
163
197
|
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
@@ -168,12 +202,12 @@ export async function promises<Items extends unknown[]>(
|
|
|
168
202
|
|
|
169
203
|
signal?.addEventListener('abort', abort, PROMISE_ABORT_OPTIONS);
|
|
170
204
|
|
|
171
|
-
const data: PromiseData
|
|
205
|
+
const data: PromiseData = {
|
|
172
206
|
last: length - 1,
|
|
173
|
-
result: [] as unknown
|
|
207
|
+
result: [] as unknown[],
|
|
174
208
|
};
|
|
175
209
|
|
|
176
|
-
let handlers: PromiseHandlers
|
|
210
|
+
let handlers: PromiseHandlers;
|
|
177
211
|
|
|
178
212
|
return new Promise((resolve, reject) => {
|
|
179
213
|
handlers = {reject, resolve};
|
|
@@ -206,6 +240,41 @@ export async function promises<Items extends unknown[]>(
|
|
|
206
240
|
});
|
|
207
241
|
}
|
|
208
242
|
|
|
243
|
+
promises.result = resultPromises;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
247
|
+
*
|
|
248
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
249
|
+
* @param items List of promises
|
|
250
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
251
|
+
* @returns List of results
|
|
252
|
+
*/
|
|
253
|
+
async function resultPromises<Items extends unknown[]>(
|
|
254
|
+
items: [...Items],
|
|
255
|
+
signal?: AbortSignal,
|
|
256
|
+
): Promise<PromisesResult<PromisesItems<Items>>>;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
260
|
+
*
|
|
261
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
262
|
+
* @param items List of promises
|
|
263
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
264
|
+
* @returns List of results
|
|
265
|
+
*/
|
|
266
|
+
async function resultPromises<Value>(
|
|
267
|
+
items: Promise<Value>[],
|
|
268
|
+
signal?: AbortSignal,
|
|
269
|
+
): Promise<Result<Awaited<Value>>[]>;
|
|
270
|
+
|
|
271
|
+
async function resultPromises(
|
|
272
|
+
items: Promise<unknown>[],
|
|
273
|
+
signal?: AbortSignal,
|
|
274
|
+
): Promise<Result<unknown>[]> {
|
|
275
|
+
return promises(items, signal).then(getResultsFromPromises);
|
|
276
|
+
}
|
|
277
|
+
|
|
209
278
|
// #endregion
|
|
210
279
|
|
|
211
280
|
// #region Exports
|
|
@@ -218,12 +287,13 @@ export {
|
|
|
218
287
|
CancelablePromise,
|
|
219
288
|
PromiseTimeoutError,
|
|
220
289
|
type FulfilledPromise,
|
|
221
|
-
type RejectedPromise,
|
|
222
290
|
type PromiseOptions,
|
|
223
|
-
type PromiseStrategy,
|
|
224
291
|
type PromisesOptions,
|
|
225
292
|
type PromisesResult,
|
|
226
|
-
type
|
|
293
|
+
type PromiseStrategy,
|
|
294
|
+
type PromisesValues as PromisesValue,
|
|
295
|
+
type PromisesValue as PromisesValueItem,
|
|
296
|
+
type RejectedPromise,
|
|
227
297
|
} from './models';
|
|
228
298
|
export {timed} from './timed';
|
|
229
299
|
|
package/src/promise/misc.ts
CHANGED
|
@@ -22,10 +22,7 @@ export function cancelable<Value>(
|
|
|
22
22
|
return new CancelablePromise(executor);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
export function handleResult
|
|
26
|
-
status: string,
|
|
27
|
-
parameters: PromiseParameters<Items>,
|
|
28
|
-
): void {
|
|
25
|
+
export function handleResult(status: string, parameters: PromiseParameters): void {
|
|
29
26
|
const {abort, complete, data, handlers, index, signal, value} = parameters;
|
|
30
27
|
|
|
31
28
|
if (signal?.aborted ?? false) {
|
package/src/promise/models.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type {Result} from '../result/models';
|
|
2
|
+
|
|
1
3
|
// #region Types
|
|
2
4
|
|
|
3
5
|
export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
@@ -28,16 +30,16 @@ export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
28
30
|
|
|
29
31
|
export type FulfilledPromise<Value> = {
|
|
30
32
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
31
|
-
value: Value
|
|
33
|
+
value: Awaited<Value>;
|
|
32
34
|
};
|
|
33
35
|
|
|
34
|
-
export type PromiseData
|
|
36
|
+
export type PromiseData = {
|
|
35
37
|
last: number;
|
|
36
|
-
result:
|
|
38
|
+
result: unknown[];
|
|
37
39
|
};
|
|
38
40
|
|
|
39
|
-
export type PromiseHandlers
|
|
40
|
-
resolve: (value:
|
|
41
|
+
export type PromiseHandlers = {
|
|
42
|
+
resolve: (value: unknown[]) => void;
|
|
41
43
|
reject: (reason: unknown) => void;
|
|
42
44
|
};
|
|
43
45
|
|
|
@@ -52,11 +54,11 @@ export type PromiseOptions = {
|
|
|
52
54
|
time?: number;
|
|
53
55
|
};
|
|
54
56
|
|
|
55
|
-
export type PromiseParameters
|
|
57
|
+
export type PromiseParameters = {
|
|
56
58
|
abort: () => void;
|
|
57
59
|
complete: boolean;
|
|
58
|
-
data: PromiseData
|
|
59
|
-
handlers: PromiseHandlers
|
|
60
|
+
data: PromiseData;
|
|
61
|
+
handlers: PromiseHandlers;
|
|
60
62
|
index: number;
|
|
61
63
|
signal?: AbortSignal;
|
|
62
64
|
value?: unknown;
|
|
@@ -64,7 +66,7 @@ export type PromiseParameters<Items extends unknown[]> = {
|
|
|
64
66
|
|
|
65
67
|
/**
|
|
66
68
|
* Promise handling strategy
|
|
67
|
-
*
|
|
69
|
+
*
|
|
68
70
|
* - `complete`: wait for all promises to settle, then return the results
|
|
69
71
|
* - Returns an array of fulfilled and/or rejected results
|
|
70
72
|
* - `first`: rejects on the first rejected promise
|
|
@@ -80,7 +82,7 @@ export class PromiseTimeoutError extends Error {
|
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
|
|
83
|
-
export type
|
|
85
|
+
export type PromisesItems<Items extends unknown[]> = {
|
|
84
86
|
[K in keyof Items]: Promise<Items[K]>;
|
|
85
87
|
};
|
|
86
88
|
|
|
@@ -90,10 +92,14 @@ export type PromisesOptions = {
|
|
|
90
92
|
};
|
|
91
93
|
|
|
92
94
|
export type PromisesResult<Items extends unknown[]> = {
|
|
93
|
-
[K in keyof Items]: Items[K] extends Promise<infer Value> ?
|
|
95
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? Result<Awaited<Value>> : never;
|
|
94
96
|
};
|
|
95
97
|
|
|
96
|
-
export type
|
|
98
|
+
export type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
99
|
+
|
|
100
|
+
export type PromisesValues<Items extends unknown[]> = {
|
|
101
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never;
|
|
102
|
+
};
|
|
97
103
|
|
|
98
104
|
export type RejectedPromise = {
|
|
99
105
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { RequiredKeys } from '../models';
|
|
2
|
-
import
|
|
2
|
+
import type { Result } from '../result/models';
|
|
3
|
+
import { type FulfilledPromise, type PromiseOptions, type PromisesOptions, type PromiseStrategy, type PromisesValue, type RejectedPromise } from './models';
|
|
3
4
|
export declare function getPromiseOptions(input: unknown): RequiredKeys<PromiseOptions, 'time'>;
|
|
4
5
|
export declare function getPromisesOptions(input: unknown): RequiredKeys<PromisesOptions, 'strategy'>;
|
|
6
|
+
export declare function getResultsFromPromises<Value>(promised: PromisesValue<Value>[]): Result<Value>[];
|
|
5
7
|
export declare function getStrategyOrDefault(value: unknown): PromiseStrategy;
|
|
6
8
|
/**
|
|
7
9
|
* Is the value a fulfilled promise result?
|
package/types/promise/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { Result } from '../result/models';
|
|
2
|
+
import { type PromiseOptions, type PromisesItems, type PromisesOptions, type PromisesResult, type PromisesValue, type PromisesValues } from './models';
|
|
2
3
|
/**
|
|
3
4
|
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
4
5
|
* @param promise Promise to wrap
|
|
@@ -28,7 +29,25 @@ export declare function attemptPromise<Value>(callback: () => Value, options?: P
|
|
|
28
29
|
* @param options Options for handling the promises
|
|
29
30
|
* @returns List of results
|
|
30
31
|
*/
|
|
31
|
-
export declare function promises<Items extends unknown[], Options extends PromisesOptions>(items:
|
|
32
|
+
export declare function promises<Items extends unknown[], Options extends PromisesOptions>(items: [...Items], options?: Options): Promise<Options['strategy'] extends 'first' ? Items : PromisesValues<PromisesItems<Items>>>;
|
|
33
|
+
/**
|
|
34
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
35
|
+
*
|
|
36
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
37
|
+
* @param items List of promises
|
|
38
|
+
* @param options Options for handling the promises
|
|
39
|
+
* @returns List of results
|
|
40
|
+
*/
|
|
41
|
+
export declare function promises<Value, Options extends PromisesOptions>(items: Promise<Value>[], options?: Options): Promise<Options['strategy'] extends 'first' ? Value[] : PromisesValue<Value>[]>;
|
|
42
|
+
/**
|
|
43
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
44
|
+
*
|
|
45
|
+
* If any promise in the list is rejected, the whole function will reject
|
|
46
|
+
* @param items List of promises
|
|
47
|
+
* @param strategy Strategy for handling the promises; rejects on the first error encountered
|
|
48
|
+
* @returns List of results
|
|
49
|
+
*/
|
|
50
|
+
export declare function promises<Items extends unknown[]>(items: [...Items], strategy: 'first'): Promise<PromisesItems<Items>>;
|
|
32
51
|
/**
|
|
33
52
|
* Handle a list of promises, returning their results in an ordered array.
|
|
34
53
|
*
|
|
@@ -37,17 +56,45 @@ export declare function promises<Items extends unknown[], Options extends Promis
|
|
|
37
56
|
* @param strategy Strategy for handling the promises; rejects on the first error encountered
|
|
38
57
|
* @returns List of results
|
|
39
58
|
*/
|
|
40
|
-
export declare function promises<
|
|
59
|
+
export declare function promises<Value>(items: Promise<Value>[], strategy: 'first'): Promise<Value[]>;
|
|
60
|
+
/**
|
|
61
|
+
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
62
|
+
* @param items List of promises
|
|
63
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
64
|
+
* @returns List of results
|
|
65
|
+
*/
|
|
66
|
+
export declare function promises<Items extends unknown[]>(items: [...Items], signal?: AbortSignal): Promise<PromisesValues<PromisesItems<Items>>>;
|
|
41
67
|
/**
|
|
42
68
|
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
43
69
|
* @param items List of promises
|
|
44
70
|
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
45
71
|
* @returns List of results
|
|
46
72
|
*/
|
|
47
|
-
export declare function promises<
|
|
73
|
+
export declare function promises<Value>(items: Promise<Value>[], signal?: AbortSignal): Promise<PromisesValue<Value>[]>;
|
|
74
|
+
export declare namespace promises {
|
|
75
|
+
var result: typeof resultPromises;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
79
|
+
*
|
|
80
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
81
|
+
* @param items List of promises
|
|
82
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
83
|
+
* @returns List of results
|
|
84
|
+
*/
|
|
85
|
+
declare function resultPromises<Items extends unknown[]>(items: [...Items], signal?: AbortSignal): Promise<PromisesResult<PromisesItems<Items>>>;
|
|
86
|
+
/**
|
|
87
|
+
* Handle a list of promises, returning their results in an ordered array of results _({@link Result})_.
|
|
88
|
+
*
|
|
89
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
90
|
+
* @param items List of promises
|
|
91
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
92
|
+
* @returns List of results
|
|
93
|
+
*/
|
|
94
|
+
declare function resultPromises<Value>(items: Promise<Value>[], signal?: AbortSignal): Promise<Result<Awaited<Value>>[]>;
|
|
48
95
|
export { toPromise as fromResult } from '../result/misc';
|
|
49
96
|
export { delay } from './delay';
|
|
50
97
|
export { isFulfilled, isRejected } from './helpers';
|
|
51
98
|
export { cancelable, toResult } from './misc';
|
|
52
|
-
export { CancelablePromise, PromiseTimeoutError, type FulfilledPromise, type
|
|
99
|
+
export { CancelablePromise, PromiseTimeoutError, type FulfilledPromise, type PromiseOptions, type PromisesOptions, type PromisesResult, type PromiseStrategy, type PromisesValues as PromisesValue, type PromisesValue as PromisesValueItem, type RejectedPromise, } from './models';
|
|
53
100
|
export { timed } from './timed';
|
package/types/promise/misc.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { CancelablePromise, type PromiseParameters } from './models';
|
|
|
6
6
|
* @returns Cancelable promise
|
|
7
7
|
*/
|
|
8
8
|
export declare function cancelable<Value>(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void): CancelablePromise<Value>;
|
|
9
|
-
export declare function handleResult
|
|
9
|
+
export declare function handleResult(status: string, parameters: PromiseParameters): void;
|
|
10
10
|
export declare function settlePromise(aborter: () => void, settler: (value: any) => void, value: unknown, signal?: AbortSignal): void;
|
|
11
11
|
/**
|
|
12
12
|
* Converts a promise to a promised result
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Result } from '../result/models';
|
|
1
2
|
export declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
2
3
|
#private;
|
|
3
4
|
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
@@ -9,14 +10,14 @@ export declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
|
9
10
|
}
|
|
10
11
|
export type FulfilledPromise<Value> = {
|
|
11
12
|
status: typeof PROMISE_TYPE_FULFILLED;
|
|
12
|
-
value: Value
|
|
13
|
+
value: Awaited<Value>;
|
|
13
14
|
};
|
|
14
|
-
export type PromiseData
|
|
15
|
+
export type PromiseData = {
|
|
15
16
|
last: number;
|
|
16
|
-
result:
|
|
17
|
+
result: unknown[];
|
|
17
18
|
};
|
|
18
|
-
export type PromiseHandlers
|
|
19
|
-
resolve: (value:
|
|
19
|
+
export type PromiseHandlers = {
|
|
20
|
+
resolve: (value: unknown[]) => void;
|
|
20
21
|
reject: (reason: unknown) => void;
|
|
21
22
|
};
|
|
22
23
|
export type PromiseOptions = {
|
|
@@ -29,11 +30,11 @@ export type PromiseOptions = {
|
|
|
29
30
|
*/
|
|
30
31
|
time?: number;
|
|
31
32
|
};
|
|
32
|
-
export type PromiseParameters
|
|
33
|
+
export type PromiseParameters = {
|
|
33
34
|
abort: () => void;
|
|
34
35
|
complete: boolean;
|
|
35
|
-
data: PromiseData
|
|
36
|
-
handlers: PromiseHandlers
|
|
36
|
+
data: PromiseData;
|
|
37
|
+
handlers: PromiseHandlers;
|
|
37
38
|
index: number;
|
|
38
39
|
signal?: AbortSignal;
|
|
39
40
|
value?: unknown;
|
|
@@ -50,7 +51,7 @@ export type PromiseStrategy = 'complete' | 'first';
|
|
|
50
51
|
export declare class PromiseTimeoutError extends Error {
|
|
51
52
|
constructor();
|
|
52
53
|
}
|
|
53
|
-
export type
|
|
54
|
+
export type PromisesItems<Items extends unknown[]> = {
|
|
54
55
|
[K in keyof Items]: Promise<Items[K]>;
|
|
55
56
|
};
|
|
56
57
|
export type PromisesOptions = {
|
|
@@ -58,9 +59,12 @@ export type PromisesOptions = {
|
|
|
58
59
|
strategy?: PromiseStrategy;
|
|
59
60
|
};
|
|
60
61
|
export type PromisesResult<Items extends unknown[]> = {
|
|
61
|
-
[K in keyof Items]: Items[K] extends Promise<infer Value> ?
|
|
62
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? Result<Awaited<Value>> : never;
|
|
63
|
+
};
|
|
64
|
+
export type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
65
|
+
export type PromisesValues<Items extends unknown[]> = {
|
|
66
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesValue<Awaited<Value>> : never;
|
|
62
67
|
};
|
|
63
|
-
export type PromisesResultItem<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
64
68
|
export type RejectedPromise = {
|
|
65
69
|
status: typeof PROMISE_TYPE_REJECTED;
|
|
66
70
|
reason: unknown;
|