@superutils/promise 1.2.0 → 1.2.1

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.d.ts CHANGED
@@ -6,6 +6,7 @@ interface IPromisE<T = unknown> extends Promise<T> {
6
6
  readonly state: 0 | 1 | 2;
7
7
  /** callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve/reject methods */
8
8
  onEarlyFinalize: OnEarlyFinalize<T>[];
9
+ onFinalize: OnFinalize<T>[];
9
10
  /** Indicates if the promise is still pending/unfinalized */
10
11
  readonly pending: boolean;
11
12
  /** Reject pending promise early. */
@@ -18,6 +19,7 @@ interface IPromisE<T = unknown> extends Promise<T> {
18
19
  readonly resolved: boolean;
19
20
  }
20
21
  type OnEarlyFinalize<T> = <TResolved extends boolean, TValue = TResolved extends true ? T : unknown>(resolved: TResolved, resultOrReason: TValue) => ValueOrPromise<unknown>;
22
+ type OnFinalize<T> = (result?: T | PromiseLike<T>, error?: unknown) => ValueOrPromise<void>;
21
23
  type PromiseParams<T = unknown> = ConstructorParameters<typeof Promise<T>>;
22
24
 
23
25
  /** Return type of `PromisE.deferred()` */
@@ -28,7 +30,7 @@ type DeferredAsyncDefaults<ThisArg = unknown, Delay = unknown> = Pick<Required<D
28
30
  delayMs: number;
29
31
  };
30
32
  /** Options for `PromisE.deferred` and other related functions */
31
- type DeferredAsyncOptions<ThisArg = unknown, Delay = number> = {
33
+ type DeferredAsyncOptions<ThisArg = unknown, Delay = unknown> = {
32
34
  /**
33
35
  * Delay in milliseconds, used for `debounce` and `throttle` modes. Use `0` for sequential execution.
34
36
  *
@@ -36,10 +38,10 @@ type DeferredAsyncOptions<ThisArg = unknown, Delay = number> = {
36
38
  *
37
39
  * Default: `100` (or whatever is set in `PromisE.deferred.defaults.delayMs`)
38
40
  */
39
- delayMs?: number | PositiveNumber<Delay>;
41
+ delayMs?: 0 | PositiveNumber<Delay>;
40
42
  /**
41
43
  * Whether to ignore (based on `resolveIgnored` settings) stale promises.
42
- * In debouce/throttle mode, when an older promise is resolved after a newly resolved promise,
44
+ * In debounce/throttle mode, when an older promise is resolved after a newly resolved promise,
43
45
  * the older one is considered stale.
44
46
  *
45
47
  * Default: `false`
@@ -68,17 +70,13 @@ type DeferredAsyncOptions<ThisArg = unknown, Delay = number> = {
68
70
  * See {@link ResolveError} for available options.
69
71
  */
70
72
  resolveError?: ResolveError;
71
- } & (({
72
- delayMs?: PositiveNumber<Delay>;
73
- } & DeferredOptions<ThisArg>) | ({
74
- delayMs: 0;
75
- } & {
73
+ } & (Delay extends 0 ? {
76
74
  throttle?: false;
77
75
  /** Callback invoked whenever promise/function throws error */
78
76
  onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
79
77
  /** The value to be used as "thisArg" whenever any of the callbacks are invoked */
80
78
  thisArg?: ThisArg;
81
- }));
79
+ } : DeferredOptions<ThisArg>);
82
80
  /** Determines what to do when deferred promise/function fails */
83
81
  declare enum ResolveError {
84
82
  /** Neither resolve nor reject the failed */
@@ -213,6 +211,7 @@ declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T>
213
211
  /**
214
212
  * callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve()/reject() methods */
215
213
  onEarlyFinalize: OnEarlyFinalize<T>[];
214
+ onFinalize: OnFinalize<T>[];
216
215
  /** Create a PromisE instance as a drop-in replacement for Promise */
217
216
  constructor(...args: PromiseParams<T>);
218
217
  /** Extend an existing Promise instance to check status or finalize early */
@@ -302,6 +301,8 @@ declare class PromisEBase<T = unknown> extends Promise<T> implements IPromisE<T>
302
301
  * Descibes a timeout PromisE and it's additional properties.
303
302
  */
304
303
  type IPromisE_Timeout<T = unknown> = IPromisE<T> & {
304
+ readonly abortCtrl?: AbortController;
305
+ /** Read-only property indicating if the promise rejected because of external abort. */
305
306
  readonly aborted: boolean;
306
307
  /**
307
308
  * Removes `abortCtrl/signal` listeners, effectively disabling external cancellation via AbortController.
@@ -318,10 +319,11 @@ type IPromisE_Timeout<T = unknown> = IPromisE<T> & {
318
319
  /** The internal promise that handles the timeout logic. It rejects when the duration expires. */
319
320
  timeout: IPromisE_Delay<T>;
320
321
  };
321
- type TimeoutResult<T extends unknown[], TFunc extends keyof TimeoutFunc<T>, Values extends unknown[] = {
322
+ type TimeoutResult<T extends unknown[], BatchFunc extends keyof BatchFuncs<T>, Values extends unknown[] = {
322
323
  -readonly [P in keyof T]: T[P] extends (...args: unknown[]) => infer ReturnType ? ReturnType : T[P];
323
- }> = Awaited<T['length'] extends 1 ? Values[0] : ReturnType<TimeoutFunc<Values>[TFunc]>>;
324
- type TimeoutFunc<T extends unknown[] = []> = {
324
+ }> = Awaited<T['length'] extends 1 ? Values[0] : ReturnType<BatchFuncs<Values>[BatchFunc]>>;
325
+ /** Suported function names for batch operations */
326
+ type BatchFuncs<T extends unknown[] = []> = {
325
327
  all: typeof PromisEBase.all<T>;
326
328
  allSettled: typeof PromisEBase.allSettled<T>;
327
329
  any: typeof PromisEBase.any<T>;
@@ -333,27 +335,56 @@ type TimeoutFunc<T extends unknown[] = []> = {
333
335
  * @param func (optional) name of the supported `PromiEBase` static method to be used to resolve
334
336
  * when more than one promise/function is provided. Default: `"all"`
335
337
  * @param timeout (optional) timeout duration in milliseconds. Default: `10_000` (10 seconds)
336
- * @param timeoutMsg (optional) timeout error message. Default: `"Timed out after 10000ms"`
337
338
  *
338
339
  */
339
- type TimeoutOptions<T extends unknown[] = [], Func extends string = 'all'> = {
340
+ type TimeoutOptions<T extends unknown[] = [], BatchFuncName extends string = 'all'> = {
341
+ /**
342
+ * An `AbortController` instance.
343
+ *
344
+ * If provided:
345
+ * - It will be aborted automatically when the timeout occurs.
346
+ * - If it is aborted externally, the promise will be rejected and the timeout will be cleared.
347
+ */
340
348
  abortCtrl?: AbortController;
341
- func?: T['length'] extends 0 ? never : T['length'] extends 1 ? never : Func;
349
+ /**
350
+ * Whether to call `abortCtrl.abort()` if the promise is finalized externally
351
+ * (resolved or rejected before timeout or abort).
352
+ *
353
+ * Default: `true`
354
+ */
355
+ abortOnEarlyFinalize?: boolean;
356
+ /**
357
+ * The name of the `PromisEBase` static method to use for resolving multiple promises/functions.
358
+ *
359
+ * Only applicable when more than one promise/function is provided.
360
+ */
361
+ batchFunc?: T['length'] extends 0 ? never : T['length'] extends 1 ? never : BatchFuncName;
342
362
  /**
343
363
  * Callback invoked when the promise is rejected due to an abort signal.
344
- * Optionally, return an `Error` object to reject the promise with a custom error.
364
+ *
365
+ * Can be used to transform the abort error by returning a custom `Error` object.
345
366
  */
346
367
  onAbort?: () => ValueOrPromise<void | Error>;
347
368
  /**
348
369
  * Callback invoked when the promise times out.
349
- * Optionally, return an `Error` object to reject the promise with a custom error.
370
+ *
371
+ * Can be used to transform the timeout error by returning a custom `Error` object.
350
372
  */
351
373
  onTimeout?: () => ValueOrPromise<void | Error>;
374
+ /**
375
+ * An `AbortSignal` to listen to.
376
+ *
377
+ * If aborted:
378
+ * - The promise will be rejected.
379
+ * - The `abortCtrl` (if provided and distinct) will be aborted.
380
+ * - The timeout will be cleared.
381
+ */
352
382
  signal?: AbortSignal;
383
+ /** Timeout duration in milliseconds. */
353
384
  timeout?: number;
354
385
  };
355
386
  /** Default options for `PromisE.timeout()` */
356
- type TimeoutOptionsDefault = Required<Omit<TimeoutOptions<unknown[], keyof TimeoutFunc>, 'abortCtrl' | 'signal'>>;
387
+ type TimeoutOptionsDefault = Required<Omit<TimeoutOptions<unknown[], keyof BatchFuncs>, 'abortCtrl' | 'signal'>>;
357
388
 
358
389
  /**
359
390
  * @function PromisE.deferred
@@ -590,8 +621,6 @@ declare namespace delay {
590
621
  */
591
622
  declare function delayReject<T = never>(duration: number, reason?: unknown): IPromisE_Delay<T>;
592
623
 
593
- /** Timeout duration (in milliseconds) used as a fallback when positive number is not provided to {@link timeout} */
594
- declare const FALLBACK_TIMEOUT = 10000;
595
624
  /**
596
625
  * Creates a new promise that wraps one or more promises and rejects if they do not settle within a
597
626
  * specified timeout duration. When multiple promises are provided, they can be processed using methods like
@@ -674,7 +703,6 @@ declare function timeout<T extends [unknown, ...unknown[]]>(timeout: number, ...
674
703
  *
675
704
  * @param options An options object can be passed with one or more of the following properties:
676
705
  * @param options.abortCtrl (optional) AbortController to manually reject promise externally and/or to sync abort with timeout rejection
677
- * @param options.abortMsg (optional) error message when promise is rejected by abort controller/signal
678
706
  * @param options.func (optional) Name of the `PromisE` static method to be used to combine the `values`.
679
707
  * Only used when more than one promise is provided. Default: `"all"`
680
708
  *
@@ -683,9 +711,12 @@ declare function timeout<T extends [unknown, ...unknown[]]>(timeout: number, ...
683
711
  * 2. `'allSettled'`: for `PromisE.allSettled`
684
712
  * 3. `'any'`: for `PromisE.any`
685
713
  * 4. `'race'`: for `PromisE.race`
714
+ * @param options.onAbort (optional) Callback invoked when the promise is rejected due to an abort signal.
715
+ * Optionally, return an `Error` object to reject the promise with a custom error.
716
+ * @param options.onTimeout (optional) Callback invoked when the promise times out.
717
+ * Optionally, return an `Error` object to reject the promise with a custom error.
686
718
  * @param options.signal (optional) AbortSignal to manually reject promise externally
687
719
  * @param options.timeout (optional) timeout duration in milliseconds. If positive number is not provided, the default value will be used. Default: `10_000` (10 seconds)
688
- * @param options.timeoutMsg (optional) custom error message to be used when promises timeout.
689
720
  *
690
721
  * @param values Mix of promises, values and/or functions
691
722
  *
@@ -698,7 +729,6 @@ declare function timeout<T extends [unknown, ...unknown[]]>(timeout: number, ...
698
729
  * { // instead of `timeout: number` an object can be used for additional options
699
730
  * func: 'race', // tells PromisE.timeout to use `PromisE.race(promises)`
700
731
  * timeout: 5000, // timeout after 5000ms
701
- * timeoutMsg: 'My custom timed out message',
702
732
  * },
703
733
  * PromisE.delay(1000), // resolves after 1000ms with value 1000
704
734
  * () => PromisE.delay(2000), // resolves after 2000ms with value 2000
@@ -707,9 +737,9 @@ declare function timeout<T extends [unknown, ...unknown[]]>(timeout: number, ...
707
737
  * // Result: 1000 (Result of `Promise.race(promises)`)
708
738
  * ```
709
739
  */
710
- declare function timeout<T extends unknown[], TFunc extends keyof TimeoutFunc<T> = keyof TimeoutFunc<T>>(options: TimeoutOptions<T, TFunc>, ...values: T): IPromisE_Timeout<TimeoutResult<T, TFunc>>;
740
+ declare function timeout<T extends unknown[], TFunc extends keyof BatchFuncs<T> = keyof BatchFuncs<T>>(options: TimeoutOptions<T, TFunc>, ...values: T): IPromisE_Timeout<TimeoutResult<T, TFunc>>;
711
741
  declare namespace timeout {
712
- var defaults: Required<Omit<TimeoutOptions<unknown[], keyof TimeoutFunc<[]>>, "abortCtrl" | "signal">>;
742
+ var defaults: Required<Omit<TimeoutOptions<unknown[], keyof BatchFuncs<[]>>, "abortCtrl" | "signal">>;
713
743
  }
714
744
 
715
745
  /**
@@ -840,4 +870,27 @@ declare const retry: {
840
870
  };
841
871
  };
842
872
 
843
- export { type DeferredAsyncCallback, type DeferredAsyncDefaults, type DeferredAsyncOptions, FALLBACK_TIMEOUT, type GetPromiseFunc, type IPromisE, type IPromisE_Delay, type IPromisE_Timeout, type OnEarlyFinalize, PromisE, PromisEBase, type PromiseParams, ResolveError, ResolveIgnored, type RetryIfFunc, type RetryOptions, type TimeoutFunc, type TimeoutOptions, type TimeoutOptionsDefault, type TimeoutResult, PromisE as default, deferred, deferredCallback, delay, delayReject, retry, timeout };
873
+ /** Timeout duration (in milliseconds) used as a fallback when positive number is not provided to {@link timeout} */
874
+ declare const TIMEOUT_FALLBACK = 10000;
875
+ /** Node.js setTimeout limit is 2147483647 (2^31-1). Larger values fire immediately. */
876
+ declare const TIMEOUT_MAX = 2147483647;
877
+ declare class TimeoutPromise<T> extends PromisEBase<T> implements IPromisE_Timeout<T> {
878
+ readonly data: IPromisE<T>;
879
+ readonly options: TimeoutOptions;
880
+ readonly timeout: IPromisE_Delay<T>;
881
+ readonly started: Date;
882
+ private _signals?;
883
+ constructor(data: IPromisE<T>, timeout: IPromisE_Delay<T>, options: TimeoutOptions, _signals?: AbortSignal[]);
884
+ get abortCtrl(): AbortController | undefined;
885
+ get aborted(): boolean;
886
+ cancelAbort(): void;
887
+ clearTimeout(): void;
888
+ get timedout(): boolean;
889
+ private _setup;
890
+ private _handleAbort;
891
+ private _handleEarlyFinalize;
892
+ private _handleFinalize;
893
+ static defaults: TimeoutOptionsDefault;
894
+ }
895
+
896
+ export { type BatchFuncs, type DeferredAsyncCallback, type DeferredAsyncDefaults, type DeferredAsyncOptions, type GetPromiseFunc, type IPromisE, type IPromisE_Delay, type IPromisE_Timeout, type OnEarlyFinalize, type OnFinalize, PromisE, PromisEBase, type PromiseParams, ResolveError, ResolveIgnored, type RetryIfFunc, type RetryOptions, TIMEOUT_FALLBACK, TIMEOUT_MAX, type TimeoutOptions, type TimeoutOptionsDefault, TimeoutPromise, type TimeoutResult, PromisE as default, deferred, deferredCallback, delay, delayReject, retry, timeout };
package/dist/index.js CHANGED
@@ -17,10 +17,16 @@ var _PromisEBase = class _PromisEBase extends Promise {
17
17
  _reject = (reason) => {
18
18
  reject(reason);
19
19
  this._state = 2;
20
+ this.onFinalize.forEach(
21
+ (fn) => fallbackIfFails(fn, [void 0, reason], void 0)
22
+ );
20
23
  };
21
24
  _resolve = (value) => {
22
25
  resolve(value);
23
26
  this._state = 1;
27
+ this.onFinalize.forEach(
28
+ (fn) => fallbackIfFails(fn, [value, void 0], void 0)
29
+ );
24
30
  };
25
31
  if (isFn(input)) {
26
32
  fallbackIfFails(input, [_resolve, _reject], _reject);
@@ -34,6 +40,7 @@ var _PromisEBase = class _PromisEBase extends Promise {
34
40
  /**
35
41
  * callbacks to be invoked whenever PromisE instance is finalized early using non-static resolve()/reject() methods */
36
42
  this.onEarlyFinalize = [];
43
+ this.onFinalize = [];
37
44
  //
38
45
  //
39
46
  // --------------------------- Early resolve/reject ---------------------------
@@ -411,117 +418,141 @@ retry.defaults = {
411
418
  };
412
419
  var retry_default = retry;
413
420
 
421
+ // src/TimeoutPromise.ts
422
+ import { arrUnique, fallbackIfFails as fallbackIfFails5, isObj } from "@superutils/core";
423
+ var TIMEOUT_FALLBACK = 1e4;
424
+ var TIMEOUT_MAX = 2147483647;
425
+ var TimeoutPromise = class extends PromisEBase_default {
426
+ constructor(data, timeout2, options, _signals) {
427
+ super(data);
428
+ this.started = /* @__PURE__ */ new Date();
429
+ this._setup = () => {
430
+ var _a, _b, _c, _d;
431
+ this._signals = arrUnique(
432
+ [(_b = (_a = this.options) == null ? void 0 : _a.abortCtrl) == null ? void 0 : _b.signal, (_c = this.options) == null ? void 0 : _c.signal].filter(
433
+ Boolean
434
+ )
435
+ );
436
+ !this.onEarlyFinalize.includes(this._handleEarlyFinalize) && this.onEarlyFinalize.push(this._handleEarlyFinalize);
437
+ !this.onFinalize.includes(this._handleFinalize) && this.onFinalize.push(this._handleFinalize);
438
+ (_d = this._signals) == null ? void 0 : _d.forEach(
439
+ (signal) => signal == null ? void 0 : signal.addEventListener(
440
+ "abort",
441
+ this._handleAbort
442
+ )
443
+ );
444
+ };
445
+ this._handleAbort = async () => {
446
+ var _a, _b, _c;
447
+ if (!((_a = this._signals) == null ? void 0 : _a.length) || !this.pending) return;
448
+ let err = await fallbackIfFails5((_b = this.options) == null ? void 0 : _b.onAbort, [], void 0);
449
+ err != null ? err : err = new Error(
450
+ `Aborted after ${(/* @__PURE__ */ new Date()).getTime() - this.started.getTime()}ms`
451
+ );
452
+ (_c = err.name) != null ? _c : err.name = "AbortError";
453
+ this.reject(err);
454
+ };
455
+ this._handleEarlyFinalize = () => {
456
+ var _a, _b, _c, _d;
457
+ ((_a = this.options) == null ? void 0 : _a.abortOnEarlyFinalize) && ((_d = (_c = (_b = this.options) == null ? void 0 : _b.abortCtrl) == null ? void 0 : _c.abort) == null ? void 0 : _d.call(_c));
458
+ };
459
+ // cleanup after execution
460
+ this._handleFinalize = ((_, err) => {
461
+ var _a, _b, _c, _d, _e;
462
+ const failed = !this.timeout.rejected && !((_a = this._signals) == null ? void 0 : _a.find((x) => x == null ? void 0 : x.aborted));
463
+ if (failed) return;
464
+ ((_d = (_c = (_b = this.options) == null ? void 0 : _b.abortCtrl) == null ? void 0 : _c.signal) == null ? void 0 : _d.aborted) === false && ((_e = this.options) == null ? void 0 : _e.abortCtrl.abort(err));
465
+ this.cancelAbort();
466
+ this.clearTimeout();
467
+ });
468
+ this.data = data;
469
+ this.options = isObj(options) ? options : {};
470
+ this.timeout = timeout2;
471
+ this._signals = _signals;
472
+ this._setup();
473
+ }
474
+ get abortCtrl() {
475
+ return this.options.abortCtrl;
476
+ }
477
+ get aborted() {
478
+ var _a;
479
+ return this.rejected && !this.timeout.rejected && !!((_a = this._signals) == null ? void 0 : _a.find((s) => s == null ? void 0 : s.aborted));
480
+ }
481
+ cancelAbort() {
482
+ var _a;
483
+ (_a = this._signals) == null ? void 0 : _a.forEach(
484
+ (signal) => signal == null ? void 0 : signal.removeEventListener(
485
+ "abort",
486
+ this._handleAbort
487
+ )
488
+ );
489
+ }
490
+ clearTimeout() {
491
+ clearTimeout(this.timeout.timeoutId);
492
+ }
493
+ get timedout() {
494
+ return this.rejected && this.timeout.rejected;
495
+ }
496
+ };
497
+ TimeoutPromise.defaults = {
498
+ abortOnEarlyFinalize: true,
499
+ batchFunc: "all",
500
+ timeout: TIMEOUT_FALLBACK
501
+ };
502
+ var TimeoutPromise_default = TimeoutPromise;
503
+
414
504
  // src/timeout.ts
415
505
  import {
416
- arrUnique,
417
- fallbackIfFails as fallbackIfFails5,
506
+ arrUnique as arrUnique2,
418
507
  isFn as isFn4,
419
- isObj,
508
+ isNumber,
509
+ isObj as isObj2,
420
510
  isPositiveNumber as isPositiveNumber2,
421
- noop,
422
511
  objCopy as objCopy3
423
512
  } from "@superutils/core";
424
- var FALLBACK_TIMEOUT = 1e4;
425
- function timeout(timeoutOrOptions, ...values) {
426
- const options = objCopy3(
513
+ function timeout(options, ...values) {
514
+ var _a;
515
+ const opts = objCopy3(
427
516
  timeout.defaults,
428
- isObj(timeoutOrOptions) ? timeoutOrOptions : { timeout: timeoutOrOptions },
517
+ isNumber(options) ? { timeout: options } : isObj2(options) ? options : {},
429
518
  [],
430
519
  "empty"
431
520
  );
432
- const { func, onTimeout } = options;
433
- const duration = isPositiveNumber2(options.timeout) ? options.timeout : FALLBACK_TIMEOUT;
434
- const arrPromises = values.map((v) => isFn4(v) ? PromisEBase_default.try(v) : v);
435
- const dataPromise = arrPromises.length <= 1 ? (
521
+ opts.timeout = Math.min(
522
+ isPositiveNumber2(opts.timeout) ? opts.timeout : TIMEOUT_FALLBACK,
523
+ TIMEOUT_MAX
524
+ );
525
+ const promises = values.map(
526
+ (v) => isFn4(v) ? PromisEBase_default.try(v) : v
527
+ );
528
+ const dataPromise = promises.length <= 1 ? (
436
529
  // single promise resolves to a single result
437
- arrPromises[0] instanceof PromisEBase_default ? arrPromises[0] : new PromisEBase_default(arrPromises[0])
530
+ promises[0] instanceof PromisEBase_default ? promises[0] : new PromisEBase_default(promises[0])
438
531
  ) : (
439
532
  // multiple promises resolve to an array of results
440
- (isFn4(PromisEBase_default[func]) ? PromisEBase_default[func] : PromisEBase_default.all)(
441
- arrPromises
533
+ (isFn4(PromisEBase_default[opts.batchFunc]) ? PromisEBase_default[opts.batchFunc] : PromisEBase_default.all)(promises)
534
+ );
535
+ const timeoutPromise = delayReject_default(opts.timeout, opts.onTimeout);
536
+ const promise = new TimeoutPromise_default(
537
+ PromisEBase_default.race([dataPromise, timeoutPromise]),
538
+ timeoutPromise,
539
+ opts,
540
+ arrUnique2(
541
+ [(_a = opts.abortCtrl) == null ? void 0 : _a.signal, opts.signal].filter(Boolean)
442
542
  )
443
543
  );
444
- const timeoutPromise = delayReject_default(duration, onTimeout);
445
- const promise = PromisEBase_default.race([
446
- dataPromise,
447
- timeoutPromise
448
- ]);
449
- addPropsNListeners(promise, dataPromise, timeoutPromise, options);
450
544
  return promise;
451
545
  }
452
- timeout.defaults = {
453
- func: "all",
454
- timeout: FALLBACK_TIMEOUT
455
- };
456
546
  var timeout_default = timeout;
457
- var addPropsNListeners = (promise, dataPromise, timeoutPromise, options) => {
458
- const { abortCtrl, onAbort, signal } = options;
459
- const signals = arrUnique([abortCtrl == null ? void 0 : abortCtrl.signal, signal].filter(Boolean));
460
- Object.defineProperties(promise, {
461
- aborted: {
462
- get() {
463
- return promise.rejected && !!signals.find((s) => s.aborted);
464
- }
465
- },
466
- cancelAbort: {
467
- get() {
468
- return () => {
469
- signals == null ? void 0 : signals.forEach(
470
- (signal2) => signal2.removeEventListener("abort", handleAbort)
471
- );
472
- };
473
- }
474
- },
475
- clearTimeout: {
476
- get() {
477
- return () => clearTimeout(timeoutPromise.timeoutId);
478
- }
479
- },
480
- data: {
481
- get() {
482
- return dataPromise;
483
- }
484
- },
485
- timeout: {
486
- get() {
487
- return timeoutPromise;
488
- }
489
- },
490
- timedout: {
491
- get() {
492
- return promise.rejected && timeoutPromise.rejected;
493
- }
494
- }
495
- });
496
- const cleanup = () => {
497
- promise.cancelAbort();
498
- promise.clearTimeout();
499
- };
500
- promise.onEarlyFinalize.push(cleanup);
501
- promise.catch(() => {
502
- var _a;
503
- if (!timeoutPromise.rejected && !signals.find((x) => x.aborted))
504
- return;
505
- ((_a = abortCtrl == null ? void 0 : abortCtrl.signal) == null ? void 0 : _a.aborted) === false && abortCtrl.abort();
506
- }).finally(cleanup);
507
- if (!signals.length) return;
508
- const started = /* @__PURE__ */ new Date();
509
- function handleAbort() {
510
- if (!promise.pending) return;
511
- fallbackIfFails5(async () => await (onAbort == null ? void 0 : onAbort()), [], void 0).then(
512
- (err) => {
513
- var _a;
514
- err != null ? err : err = new Error(
515
- `Aborted after ${(/* @__PURE__ */ new Date()).getTime() - started.getTime()}ms`
516
- );
517
- (_a = err.name) != null ? _a : err.name = "AbortError";
518
- promise.reject(err);
519
- },
520
- noop
521
- );
547
+ timeout.defaults = TimeoutPromise_default.defaults;
548
+ Object.defineProperty(timeout, "defaults", {
549
+ get() {
550
+ return TimeoutPromise_default.defaults;
551
+ },
552
+ set(newDefaults) {
553
+ TimeoutPromise_default.defaults = newDefaults;
522
554
  }
523
- signals.forEach((signal2) => signal2.addEventListener("abort", handleAbort));
524
- };
555
+ });
525
556
 
526
557
  // src/PromisE.ts
527
558
  var PromisE = class extends PromisEBase_default {
@@ -537,11 +568,13 @@ var PromisE_default = PromisE;
537
568
  // src/index.ts
538
569
  var index_default = PromisE_default;
539
570
  export {
540
- FALLBACK_TIMEOUT,
541
571
  PromisE,
542
572
  PromisEBase,
543
573
  ResolveError,
544
574
  ResolveIgnored,
575
+ TIMEOUT_FALLBACK,
576
+ TIMEOUT_MAX,
577
+ TimeoutPromise,
545
578
  index_default as default,
546
579
  deferred,
547
580
  deferredCallback,
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "url": "https://github.com/alien45/superutils/issues"
5
5
  },
6
6
  "dependencies": {
7
- "@superutils/core": "^1.1.8"
7
+ "@superutils/core": "^1.2.1"
8
8
  },
9
9
  "description": "An extended Promise with additional features such as status tracking, deferred/throttled execution, timeout and retry mechanism.",
10
10
  "files": [
@@ -43,5 +43,6 @@
43
43
  "sideEffects": false,
44
44
  "type": "module",
45
45
  "types": "dist/index.d.ts",
46
- "version": "1.2.0"
46
+ "version": "1.2.1",
47
+ "gitHead": "a26f4b1bb701d99d4cb71443e18680ee3ea52974"
47
48
  }