@oscarpalmer/atoms 0.192.0 → 0.193.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.
Files changed (130) hide show
  1. package/dist/index.d.mts +25 -23
  2. package/dist/index.mjs +30 -31
  3. package/dist/queue.d.mts +25 -23
  4. package/dist/queue.mjs +27 -28
  5. package/package.json +3 -4
  6. package/src/array/difference.ts +0 -73
  7. package/src/array/exists.ts +0 -93
  8. package/src/array/filter.ts +0 -182
  9. package/src/array/find.ts +0 -183
  10. package/src/array/first.ts +0 -196
  11. package/src/array/flatten.ts +0 -20
  12. package/src/array/from.ts +0 -129
  13. package/src/array/get.ts +0 -131
  14. package/src/array/group-by.ts +0 -369
  15. package/src/array/index.ts +0 -26
  16. package/src/array/insert.ts +0 -52
  17. package/src/array/intersection.ts +0 -72
  18. package/src/array/last.ts +0 -194
  19. package/src/array/match.ts +0 -436
  20. package/src/array/move.ts +0 -315
  21. package/src/array/partition.ts +0 -91
  22. package/src/array/push.ts +0 -23
  23. package/src/array/reverse.ts +0 -33
  24. package/src/array/select.ts +0 -260
  25. package/src/array/single.ts +0 -93
  26. package/src/array/slice.ts +0 -335
  27. package/src/array/sort.ts +0 -607
  28. package/src/array/splice.ts +0 -106
  29. package/src/array/swap.ts +0 -322
  30. package/src/array/to-map.ts +0 -371
  31. package/src/array/to-record.ts +0 -351
  32. package/src/array/to-set.ts +0 -80
  33. package/src/array/toggle.ts +0 -79
  34. package/src/array/union.ts +0 -72
  35. package/src/array/unique.ts +0 -67
  36. package/src/array/update.ts +0 -75
  37. package/src/beacon.ts +0 -378
  38. package/src/color/constants.ts +0 -137
  39. package/src/color/index.ts +0 -83
  40. package/src/color/instance.ts +0 -162
  41. package/src/color/misc/alpha.ts +0 -56
  42. package/src/color/misc/format.ts +0 -29
  43. package/src/color/misc/get.ts +0 -210
  44. package/src/color/misc/is.ts +0 -221
  45. package/src/color/misc/state.ts +0 -243
  46. package/src/color/models.ts +0 -389
  47. package/src/color/space/hex.ts +0 -184
  48. package/src/color/space/hsl.ts +0 -174
  49. package/src/color/space/hwb.ts +0 -153
  50. package/src/color/space/rgb.ts +0 -240
  51. package/src/function/assert.ts +0 -174
  52. package/src/function/index.ts +0 -2
  53. package/src/function/limit.ts +0 -83
  54. package/src/function/memoize.ts +0 -202
  55. package/src/function/once.ts +0 -200
  56. package/src/function/retry.ts +0 -175
  57. package/src/function/work.ts +0 -1007
  58. package/src/herald.ts +0 -216
  59. package/src/index.ts +0 -68
  60. package/src/internal/array/callbacks.ts +0 -46
  61. package/src/internal/array/chunk.ts +0 -51
  62. package/src/internal/array/compact.ts +0 -56
  63. package/src/internal/array/find.ts +0 -200
  64. package/src/internal/array/group.ts +0 -42
  65. package/src/internal/array/index-of.ts +0 -182
  66. package/src/internal/array/insert.ts +0 -80
  67. package/src/internal/array/overlap.ts +0 -35
  68. package/src/internal/array/sets.ts +0 -74
  69. package/src/internal/array/shuffle.ts +0 -33
  70. package/src/internal/array/update.ts +0 -52
  71. package/src/internal/function/misc.ts +0 -8
  72. package/src/internal/function/timer.ts +0 -168
  73. package/src/internal/is.ts +0 -244
  74. package/src/internal/math/aggregate.ts +0 -164
  75. package/src/internal/math/misc.ts +0 -28
  76. package/src/internal/number.ts +0 -129
  77. package/src/internal/random.ts +0 -49
  78. package/src/internal/result.ts +0 -92
  79. package/src/internal/sized.ts +0 -25
  80. package/src/internal/string.ts +0 -128
  81. package/src/internal/subscription.ts +0 -264
  82. package/src/internal/value/compare.ts +0 -170
  83. package/src/internal/value/equal.ts +0 -465
  84. package/src/internal/value/get.ts +0 -57
  85. package/src/internal/value/handlers.ts +0 -95
  86. package/src/internal/value/has.ts +0 -118
  87. package/src/internal/value/misc.ts +0 -125
  88. package/src/internal/value/partial.ts +0 -46
  89. package/src/internal/value/set.ts +0 -132
  90. package/src/is.ts +0 -182
  91. package/src/logger.ts +0 -230
  92. package/src/math.ts +0 -281
  93. package/src/models.ts +0 -286
  94. package/src/number.ts +0 -1
  95. package/src/promise/delay.ts +0 -62
  96. package/src/promise/helpers.ts +0 -96
  97. package/src/promise/index.ts +0 -300
  98. package/src/promise/misc.ts +0 -96
  99. package/src/promise/models.ts +0 -195
  100. package/src/promise/timed.ts +0 -91
  101. package/src/query.ts +0 -147
  102. package/src/queue.ts +0 -814
  103. package/src/random.ts +0 -125
  104. package/src/result/index.ts +0 -105
  105. package/src/result/match.ts +0 -124
  106. package/src/result/misc.ts +0 -129
  107. package/src/result/models.ts +0 -83
  108. package/src/result/work/flow.ts +0 -488
  109. package/src/result/work/pipe.ts +0 -496
  110. package/src/sized/map.ts +0 -110
  111. package/src/sized/set.ts +0 -107
  112. package/src/string/case.ts +0 -236
  113. package/src/string/fuzzy.ts +0 -444
  114. package/src/string/index.ts +0 -199
  115. package/src/string/match.ts +0 -80
  116. package/src/string/normalize.ts +0 -205
  117. package/src/string/template.ts +0 -161
  118. package/src/value/clone.ts +0 -319
  119. package/src/value/collection.ts +0 -63
  120. package/src/value/diff.ts +0 -232
  121. package/src/value/freeze.ts +0 -322
  122. package/src/value/handle.ts +0 -3
  123. package/src/value/index.ts +0 -5
  124. package/src/value/merge.ts +0 -278
  125. package/src/value/omit.ts +0 -20
  126. package/src/value/pick.ts +0 -20
  127. package/src/value/shake.ts +0 -45
  128. package/src/value/smush.ts +0 -89
  129. package/src/value/transform.ts +0 -153
  130. package/src/value/unsmush.ts +0 -84
@@ -1,300 +0,0 @@
1
- import type {Result} from '../result/models';
2
- import {getPromiseOptions, getPromisesOptions, getResultsFromPromises} from './helpers';
3
- import {handleResult, settlePromise} from './misc';
4
- import {
5
- PROMISE_ABORT_OPTIONS,
6
- PROMISE_ABORT_EVENT,
7
- PROMISE_MESSAGE_EXPECTATION_ATTEMPT,
8
- PROMISE_STRATEGY_DEFAULT,
9
- PROMISE_TYPE_FULFILLED,
10
- PROMISE_TYPE_REJECTED,
11
- type PromiseData,
12
- type PromiseHandlers,
13
- type PromiseOptions,
14
- type PromisesItems,
15
- type PromisesOptions,
16
- type PromisesResult,
17
- type PromisesValue,
18
- type PromisesValues,
19
- type PromisesUnwrapped,
20
- } from './models';
21
- import {getTimedPromise} from './timed';
22
-
23
- // #region Functions
24
-
25
- /**
26
- * Wrap a _Promise_ with safety handlers, with optional abort capabilities and timeout
27
- *
28
- * @param promise _Promise_ to wrap
29
- * @param options Options for the _Promise_
30
- * @returns Wrapped _Promise_
31
- */
32
- export async function attemptPromise<Value>(
33
- promise: Promise<Value>,
34
- options?: PromiseOptions | AbortSignal | number,
35
- ): Promise<Value>;
36
-
37
- /**
38
- * Wrap a _Promise_-returning callback with safety handlers, with optional abort capabilities and timeout
39
- *
40
- * @param callback Callback to wrap
41
- * @param options Options for the _Promise_
42
- * @returns _Promise_-wrapped callback
43
- */
44
- export async function attemptPromise<Value>(
45
- callback: () => Promise<Value>,
46
- options?: PromiseOptions | AbortSignal | number,
47
- ): Promise<Value>;
48
-
49
- /**
50
- * Wrap a callback with a _Promise_ and safety handlers, with optional abort capabilities and timeout
51
- *
52
- * @param callback Callback to wrap
53
- * @param options Options for the _Promise_
54
- * @returns _Promise_-wrapped callback
55
- */
56
- export async function attemptPromise<Value>(
57
- callback: () => Value,
58
- options?: PromiseOptions | AbortSignal | number,
59
- ): Promise<Value>;
60
-
61
- export async function attemptPromise<Value>(
62
- value: (() => Value) | Promise<Value>,
63
- options?: PromiseOptions | AbortSignal | number,
64
- ): Promise<Value> {
65
- const isFunction = typeof value === 'function';
66
-
67
- if (!isFunction && !(value instanceof Promise)) {
68
- return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
69
- }
70
-
71
- const {signal, time} = getPromiseOptions(options);
72
-
73
- if (signal?.aborted ?? false) {
74
- return Promise.reject(signal!.reason);
75
- }
76
-
77
- function abort(): void {
78
- rejector(signal!.reason);
79
- }
80
-
81
- async function handler(
82
- resolve: (value: Value) => void,
83
- reject: (reason: unknown) => void,
84
- ): Promise<void> {
85
- try {
86
- let result = isFunction ? value() : await value;
87
-
88
- if (result instanceof Promise) {
89
- result = await result;
90
- }
91
-
92
- settlePromise(abort, resolve, result, signal);
93
- } catch (error) {
94
- settlePromise(abort, reject, error, signal);
95
- }
96
- }
97
-
98
- let rejector: (reason: unknown) => void;
99
-
100
- signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
101
-
102
- const promise = new Promise<Value>((resolve, reject) => {
103
- rejector = reject;
104
-
105
- void handler(resolve, reject);
106
- });
107
-
108
- return time > 0 ? getTimedPromise(promise, time, signal) : promise;
109
- }
110
-
111
- /**
112
- * Handle a list of _Promises_, returning their results in an ordered array
113
- *
114
- * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
115
- *
116
- * @param items List of _Promises_
117
- * @param options Options for handling the _Promises_
118
- * @returns List of results
119
- */
120
- export async function promises<Items extends unknown[], Options extends PromisesOptions>(
121
- items: [...Items],
122
- options?: Options,
123
- ): Promise<
124
- Options['strategy'] extends 'first'
125
- ? PromisesUnwrapped<Items>
126
- : PromisesValues<PromisesItems<Items>>
127
- >;
128
-
129
- /**
130
- * Handle a list of _Promises_, returning their results in an ordered array
131
- *
132
- * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
133
- *
134
- * @param items List of _Promises_
135
- * @param options Options for handling the _Promises_
136
- * @returns List of results
137
- */
138
- export async function promises<Value, Options extends PromisesOptions>(
139
- items: Promise<Value>[],
140
- options?: Options,
141
- ): Promise<Options['strategy'] extends 'first' ? Value[] : PromisesValue<Value>[]>;
142
-
143
- /**
144
- * Handle a list of _Promises_, returning their results in an ordered array
145
- *
146
- * If any _Promise_ in the list is rejected, the whole function will reject
147
- *
148
- * @param items List of _Promises_
149
- * @param strategy Strategy for handling the _Promises_; rejects on the first error encountered
150
- * @returns List of results
151
- */
152
- export async function promises<Items extends unknown[]>(
153
- items: [...Items],
154
- strategy: 'first',
155
- ): Promise<PromisesUnwrapped<Items>>;
156
-
157
- /**
158
- * Handle a list of _Promises_, returning their results in an ordered array
159
- *
160
- * If any _Promise_ in the list is rejected, the whole function will reject
161
- *
162
- * @param items List of _Promises_
163
- * @param strategy Strategy for handling the _Promises_; rejects on the first error encountered
164
- * @returns List of results
165
- */
166
- export async function promises<Value>(items: Promise<Value>[], strategy: 'first'): Promise<Value[]>;
167
-
168
- /**
169
- * Handle a list of _Promises_, returning their results in an ordered array of rejected and resolved results
170
- *
171
- * @param items List of _Promises_
172
- * @param signal AbortSignal for aborting the operation _(when aborted, the _Promise_ will reject with the reason of the signal)_
173
- * @returns List of results
174
- */
175
- export async function promises<Items extends unknown[]>(
176
- items: [...Items],
177
- signal?: AbortSignal,
178
- ): Promise<PromisesValues<PromisesItems<Items>>>;
179
-
180
- /**
181
- * Handle a list of _Promises_, returning their results in an ordered array of rejected and resolved results
182
- *
183
- * @param items List of _Promises_
184
- * @param signal AbortSignal for aborting the operation _(when aborted, the _Promise_ will reject with the reason of the signal)_
185
- * @returns List of results
186
- */
187
- export async function promises<Value>(
188
- items: Array<Promise<Value> | (() => Promise<Value>)>,
189
- signal?: AbortSignal,
190
- ): Promise<PromisesValue<Value>[]>;
191
-
192
- export async function promises(items: unknown[], options?: unknown): Promise<unknown[]> {
193
- const {signal, strategy} = getPromisesOptions(options);
194
-
195
- if (signal?.aborted ?? false) {
196
- return Promise.reject(signal!.reason);
197
- }
198
-
199
- if (!Array.isArray(items)) {
200
- return Promise.resolve([]);
201
- }
202
-
203
- const actual = items
204
- .map(item => (typeof item === 'function' ? item() : item))
205
- .filter(item => item instanceof Promise);
206
-
207
- const {length} = actual;
208
-
209
- if (length === 0) {
210
- return Promise.resolve([]);
211
- }
212
-
213
- const complete = strategy === PROMISE_STRATEGY_DEFAULT;
214
-
215
- function abort(): void {
216
- handlers.reject(signal!.reason);
217
- }
218
-
219
- signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
220
-
221
- const data: PromiseData = {
222
- last: length - 1,
223
- result: [] as unknown[],
224
- };
225
-
226
- let handlers: PromiseHandlers;
227
-
228
- return new Promise((resolve, reject) => {
229
- handlers = {reject, resolve};
230
-
231
- for (let index = 0; index < length; index += 1) {
232
- void actual[index]
233
- .then(value =>
234
- handleResult(PROMISE_TYPE_FULFILLED, {
235
- abort,
236
- complete,
237
- data,
238
- handlers,
239
- index,
240
- signal,
241
- value,
242
- }),
243
- )
244
- .catch(reason =>
245
- handleResult(PROMISE_TYPE_REJECTED, {
246
- abort,
247
- complete,
248
- data,
249
- handlers,
250
- index,
251
- signal,
252
- value: reason,
253
- }),
254
- );
255
- }
256
- });
257
- }
258
-
259
- promises.result = resultPromises;
260
-
261
- /**
262
- * Handle a list of _Promises_, returning their results in an ordered array of results _({@link Result})_
263
- *
264
- * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
265
- *
266
- * _Available as `resultPromises` and `promises.result`_
267
- *
268
- * @param items List of _Promises_
269
- * @param signal AbortSignal for aborting the operation _(when aborted, the _Promise_ will reject with the reason of the signal)_
270
- * @returns List of results
271
- */
272
- export async function resultPromises<Items extends unknown[]>(
273
- items: [...Items],
274
- signal?: AbortSignal,
275
- ): Promise<PromisesResult<PromisesItems<Items>>>;
276
-
277
- /**
278
- * Handle a list of _Promises_, returning their results in an ordered array of results _({@link Result})_
279
- *
280
- * Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
281
- *
282
- * _Available as `resultPromises` and `promises.result`_
283
- *
284
- * @param items List of _Promises_
285
- * @param signal AbortSignal for aborting the operation _(when aborted, the _Promise_ will reject with the reason of the signal)_
286
- * @returns List of results
287
- */
288
- export async function resultPromises<Value>(
289
- items: Promise<Value>[],
290
- signal?: AbortSignal,
291
- ): Promise<Result<Awaited<Value>>[]>;
292
-
293
- export async function resultPromises(
294
- items: Promise<unknown>[],
295
- signal?: AbortSignal,
296
- ): Promise<Result<unknown>[]> {
297
- return promises(items, signal).then(getResultsFromPromises);
298
- }
299
-
300
- // #endregion
@@ -1,96 +0,0 @@
1
- import {error, ok} from '../result/misc';
2
- import type {Result} from '../result/models';
3
- import {
4
- CancelablePromise,
5
- PROMISE_ABORT_EVENT,
6
- PROMISE_MESSAGE_EXPECTATION_RESULT,
7
- PROMISE_TYPE_FULFILLED,
8
- PROMISE_TYPE_REJECTED,
9
- type PromiseParameters,
10
- } from './models';
11
-
12
- // #region Functions
13
-
14
- /**
15
- * Create a cancelable _Promise_
16
- *
17
- * @param executor Executor function for the _Promise_
18
- * @returns Cancelable _Promise_
19
- */
20
- export function cancelable<Value>(
21
- executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void,
22
- ): CancelablePromise<Value> {
23
- return new CancelablePromise(executor);
24
- }
25
-
26
- export function handleResult(status: string, parameters: PromiseParameters): void {
27
- const {abort, complete, data, handlers, index, signal, value} = parameters;
28
-
29
- if (signal?.aborted ?? false) {
30
- return;
31
- }
32
-
33
- if (!complete && status === PROMISE_TYPE_REJECTED) {
34
- settlePromise(abort, handlers.reject, value, signal);
35
-
36
- return;
37
- }
38
-
39
- (data.result as unknown[])[index] = !complete
40
- ? value
41
- : status === PROMISE_TYPE_FULFILLED
42
- ? {status, value}
43
- : {status, reason: value};
44
-
45
- if (index === data.last) {
46
- settlePromise(abort, handlers.resolve, data.result, signal);
47
- }
48
- }
49
-
50
- export function settlePromise(
51
- aborter: () => void,
52
- settler: (value: any) => void,
53
- value: unknown,
54
- signal?: AbortSignal,
55
- ): void {
56
- signal?.removeEventListener(PROMISE_ABORT_EVENT, aborter);
57
-
58
- settler(value);
59
- }
60
-
61
- /**
62
- * Converts a _Promise_ to a promised result
63
- *
64
- * @param callback _Promise_ callback
65
- * @returns Promised result
66
- */
67
- export async function toResult<Value>(callback: () => Promise<Value>): Promise<Result<Value>>;
68
-
69
- /**
70
- * Converts a _Promise_ to a promised result
71
- *
72
- * @param promise _Promise_ to convert
73
- * @returns Promised result
74
- */
75
- export async function toResult<Value>(promise: Promise<Value>): Promise<Result<Value>>;
76
-
77
- export async function toResult<Value>(
78
- value: Promise<Value> | (() => Promise<Value>),
79
- ): Promise<Result<Value>> {
80
- const actual = typeof value === 'function' ? value() : value;
81
-
82
- if (!(actual instanceof Promise)) {
83
- return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
84
- }
85
-
86
- return actual.then(result => ok(result)).catch(reason => error(reason));
87
- }
88
-
89
- // #endregion
90
-
91
- // #region Exports
92
-
93
- export {toPromise as fromResult} from '../result/misc';
94
- export {isFulfilled, isRejected} from './helpers';
95
-
96
- // #endregion
@@ -1,195 +0,0 @@
1
- import type {GenericCallback} from '../models';
2
- import type {Result} from '../result/models';
3
-
4
- // #region Types
5
-
6
- /**
7
- * A _Promise_ that can be canceled
8
- */
9
- export class CancelablePromise<Value = void> extends Promise<Value> {
10
- #rejector!: (reason: unknown) => void;
11
-
12
- constructor(
13
- executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void,
14
- ) {
15
- let rejector: (reason: unknown) => void;
16
-
17
- super((resolve, reject) => {
18
- rejector = reject;
19
-
20
- executor(resolve, reject);
21
- });
22
-
23
- this.#rejector = rejector!;
24
- }
25
-
26
- /**
27
- * Cancel the _Promise_, rejecting it with an optional reason
28
- *
29
- * @param reason Optional reason for canceling the _Promise_
30
- */
31
- cancel(reason?: unknown): void {
32
- this.#rejector(reason);
33
- }
34
- }
35
-
36
- /**
37
- * A _Promise_ that was fulfilled
38
- */
39
- export type FulfilledPromise<Value> = {
40
- /**
41
- * Status of the _Promise_
42
- */
43
- status: typeof PROMISE_TYPE_FULFILLED;
44
- /**
45
- * Value of the _Promise_
46
- */
47
- value: Awaited<Value>;
48
- };
49
-
50
- export type PromiseData = {
51
- last: number;
52
- result: unknown[];
53
- };
54
-
55
- export type PromiseHandlers = {
56
- resolve: (value: unknown[]) => void;
57
- reject: (reason: unknown) => void;
58
- };
59
-
60
- /**
61
- * Options for a _Promise_-handling function
62
- */
63
- export type PromiseOptions = {
64
- /**
65
- * AbortSignal for aborting the _Promise_; when aborted, the _Promise_ will reject with the reason of the signal
66
- */
67
- signal?: AbortSignal;
68
- /**
69
- * How long to wait for _(in milliseconds; defaults to `0`)_
70
- */
71
- time?: number;
72
- };
73
-
74
- export type PromiseParameters = {
75
- abort: () => void;
76
- complete: boolean;
77
- data: PromiseData;
78
- handlers: PromiseHandlers;
79
- index: number;
80
- signal?: AbortSignal;
81
- value?: unknown;
82
- };
83
-
84
- /**
85
- * _Promise_ handling strategy
86
- *
87
- * - `complete`: wait for all _Promises_ to settle, then return the results, as an array of fulfilled and/or rejected results
88
- * - `first`: rejects on the first rejected _Promise_, and returns an array of values
89
- */
90
- export type PromiseStrategy = 'complete' | 'first';
91
-
92
- /**
93
- * An error thrown when a promise times out
94
- */
95
- export class PromiseTimeoutError extends Error {
96
- constructor() {
97
- super(PROMISE_MESSAGE_TIMEOUT);
98
-
99
- this.name = PROMISE_ERROR_NAME;
100
- }
101
- }
102
-
103
- export type PromisesItems<Items extends unknown[]> = {
104
- [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback
105
- ? ReturnType<Items[ItemsKey]> extends Promise<infer Value>
106
- ? Promise<Value>
107
- : never
108
- : Items[ItemsKey] extends Promise<infer Value>
109
- ? Promise<Value>
110
- : Promise<Items[ItemsKey]>;
111
- };
112
-
113
- /**
114
- * Options for handling multiple _Promises_
115
- */
116
- export type PromisesOptions = {
117
- /**
118
- * AbortSignal for aborting the _Promises_; when aborted, the _Promises_ will reject with the reason of the signal
119
- */
120
- signal?: AbortSignal;
121
- /**
122
- * Strategy for handling the _Promises_; defaults to `complete`
123
- */
124
- strategy?: PromiseStrategy;
125
- };
126
-
127
- export type PromisesResult<Items extends unknown[]> = {
128
- [ItemsKey in keyof Items]: Items[ItemsKey] extends Promise<infer Value>
129
- ? Result<Awaited<Value>>
130
- : never;
131
- };
132
-
133
- export type PromisesUnwrapped<Items extends unknown[]> = {
134
- [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback
135
- ? ReturnType<Items[ItemsKey]> extends Promise<infer Value>
136
- ? Awaited<Value>
137
- : never
138
- : Items[ItemsKey] extends Promise<infer Value>
139
- ? Awaited<Value>
140
- : never;
141
- };
142
-
143
- export type PromisesValue<Value> = FulfilledPromise<Value> | RejectedPromise;
144
-
145
- export type PromisesValues<Items extends unknown[]> = {
146
- [ItemsKey in keyof Items]: Items[ItemsKey] extends GenericCallback
147
- ? ReturnType<Items[ItemsKey]> extends Promise<infer Value>
148
- ? PromisesValue<Awaited<Value>>
149
- : never
150
- : Items[ItemsKey] extends Promise<infer Value>
151
- ? PromisesValue<Awaited<Value>>
152
- : never;
153
- };
154
-
155
- /**
156
- * A _Promise_ that was rejected
157
- */
158
- export type RejectedPromise = {
159
- /**
160
- * Status of the _Promise_
161
- */
162
- status: typeof PROMISE_TYPE_REJECTED;
163
- /**
164
- * Reason for the rejection
165
- */
166
- reason: unknown;
167
- };
168
-
169
- // #endregion
170
-
171
- // #region Variables
172
-
173
- export const PROMISE_ABORT_EVENT = 'abort';
174
-
175
- export const PROMISE_ABORT_OPTIONS = {once: true};
176
-
177
- export const PROMISE_ERROR_NAME = 'PromiseTimeoutError';
178
-
179
- export const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = 'Attempt expected a function or a promise';
180
-
181
- export const PROMISE_MESSAGE_EXPECTATION_RESULT = 'toResult expected a Promise';
182
-
183
- export const PROMISE_MESSAGE_EXPECTATION_TIMED = 'Timed function expected a Promise';
184
-
185
- export const PROMISE_MESSAGE_TIMEOUT = 'Promise timed out';
186
-
187
- export const PROMISE_STRATEGY_ALL = new Set<PromiseStrategy>(['complete', 'first']);
188
-
189
- export const PROMISE_STRATEGY_DEFAULT: PromiseStrategy = 'complete';
190
-
191
- export const PROMISE_TYPE_FULFILLED = 'fulfilled';
192
-
193
- export const PROMISE_TYPE_REJECTED = 'rejected';
194
-
195
- // #endregion
@@ -1,91 +0,0 @@
1
- import {getTimer, TIMER_WAIT} from '../internal/function/timer';
2
- import type {RequiredKeys} from '../models';
3
- import {getPromiseOptions} from './helpers';
4
- import {settlePromise} from './misc';
5
- import {
6
- PROMISE_ABORT_OPTIONS,
7
- PROMISE_ABORT_EVENT,
8
- PROMISE_MESSAGE_EXPECTATION_TIMED,
9
- PromiseTimeoutError,
10
- type PromiseOptions,
11
- } from './models';
12
-
13
- // #region Functions
14
-
15
- export async function getTimedPromise<Value>(
16
- promise: Promise<Value>,
17
- time: number,
18
- signal?: AbortSignal,
19
- ): Promise<Value> {
20
- function abort(): void {
21
- timer.cancel();
22
-
23
- rejector(signal!.reason);
24
- }
25
-
26
- signal?.addEventListener(PROMISE_ABORT_EVENT, abort, PROMISE_ABORT_OPTIONS);
27
-
28
- const timer = getTimer(
29
- TIMER_WAIT,
30
- () => {
31
- settlePromise(abort, rejector, new PromiseTimeoutError(), signal);
32
- },
33
- time,
34
- );
35
-
36
- let rejector: (reason: unknown) => void;
37
-
38
- return Promise.race<Value>([
39
- promise,
40
- new Promise((_, reject) => {
41
- rejector = reject;
42
-
43
- timer();
44
- }),
45
- ]).then(value => {
46
- timer.cancel();
47
-
48
- rejector(undefined);
49
-
50
- signal?.removeEventListener(PROMISE_ABORT_EVENT, abort);
51
-
52
- return value;
53
- });
54
- }
55
-
56
- /**
57
- * Create a _Promise_ that should be settled within a certain amount of time
58
- *
59
- * @param promise _Promise_ to settle
60
- * @param options Timed options
61
- * @returns Timed _Promise_
62
- */
63
- export async function timed<Value>(
64
- promise: Promise<Value>,
65
- options: RequiredKeys<PromiseOptions, 'time'>,
66
- ): Promise<Value>;
67
-
68
- /**
69
- * Create a _Promise_ that should be settled within a certain amount of time
70
- *
71
- * @param promise _Promise_ to settle
72
- * @param time How long to wait for _(in milliseconds; defaults to `0`)_
73
- * @returns Timed _Promise_
74
- */
75
- export async function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
76
-
77
- export async function timed<Value>(promise: Promise<Value>, options: unknown): Promise<Value> {
78
- if (!(promise instanceof Promise)) {
79
- return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
80
- }
81
-
82
- const {signal, time} = getPromiseOptions(options);
83
-
84
- if (signal?.aborted ?? false) {
85
- return Promise.reject(signal!.reason);
86
- }
87
-
88
- return time > 0 ? getTimedPromise(promise, time, signal) : promise;
89
- }
90
-
91
- // #endregion