@oscarpalmer/atoms 0.149.0 → 0.150.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 +172 -142
- package/dist/index.js +9 -3
- package/dist/promise/delay.js +28 -0
- package/dist/promise/helpers.js +51 -0
- package/dist/promise/index.js +73 -0
- package/dist/promise/misc.js +36 -0
- package/dist/promise/models.js +37 -0
- package/dist/promise/timed.js +33 -0
- package/dist/result/index.js +4 -27
- package/dist/result/misc.js +40 -0
- package/package.json +3 -3
- package/src/index.ts +1 -1
- package/src/promise/delay.ts +63 -0
- package/src/promise/helpers.ts +91 -0
- package/src/promise/index.ts +230 -0
- package/src/promise/misc.ts +89 -0
- package/src/promise/models.ts +131 -0
- package/src/promise/timed.ts +90 -0
- package/src/result/index.ts +4 -66
- package/src/result/misc.ts +115 -0
- package/types/index.d.ts +1 -1
- package/types/promise/delay.d.ts +13 -0
- package/types/promise/helpers.d.ts +17 -0
- package/types/promise/index.d.ts +53 -0
- package/types/promise/misc.d.ts +22 -0
- package/types/promise/models.d.ts +81 -0
- package/types/promise/timed.d.ts +17 -0
- package/types/result/index.d.ts +3 -34
- package/types/result/misc.d.ts +51 -0
- package/dist/promise.js +0 -221
- package/src/promise.ts +0 -538
- package/types/promise.d.ts +0 -139
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// #region Types
|
|
2
|
+
|
|
3
|
+
export class CancelablePromise<Value = void> extends Promise<Value> {
|
|
4
|
+
#rejector!: (reason: unknown) => void;
|
|
5
|
+
|
|
6
|
+
constructor(
|
|
7
|
+
executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void,
|
|
8
|
+
) {
|
|
9
|
+
let rejector: (reason: unknown) => void;
|
|
10
|
+
|
|
11
|
+
super((resolve, reject) => {
|
|
12
|
+
rejector = reject;
|
|
13
|
+
|
|
14
|
+
executor(resolve, reject);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
this.#rejector = rejector!;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Cancel the promise, rejecting it with an optional reason
|
|
22
|
+
* @param reason Optional reason for canceling the promise
|
|
23
|
+
*/
|
|
24
|
+
cancel(reason?: unknown): void {
|
|
25
|
+
this.#rejector(reason);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type FulfilledPromise<Value> = {
|
|
30
|
+
status: typeof PROMISE_TYPE_FULFILLED;
|
|
31
|
+
value: Value;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type PromiseData<Items extends unknown[]> = {
|
|
35
|
+
last: number;
|
|
36
|
+
result: Items | PromisesResult<Items>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type PromiseHandlers<Items extends unknown[]> = {
|
|
40
|
+
resolve: (value: Items | PromisesResult<Items>) => void;
|
|
41
|
+
reject: (reason: unknown) => void;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type PromiseOptions = {
|
|
45
|
+
/**
|
|
46
|
+
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
47
|
+
*/
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
/**
|
|
50
|
+
* How long to wait for (in milliseconds; defaults to `0`)
|
|
51
|
+
*/
|
|
52
|
+
time?: number;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type PromiseParameters<Items extends unknown[]> = {
|
|
56
|
+
abort: () => void;
|
|
57
|
+
complete: boolean;
|
|
58
|
+
data: PromiseData<Items>;
|
|
59
|
+
handlers: PromiseHandlers<Items>;
|
|
60
|
+
index: number;
|
|
61
|
+
signal?: AbortSignal;
|
|
62
|
+
value?: unknown;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Promise handling strategy
|
|
67
|
+
*
|
|
68
|
+
* - `complete`: wait for all promises to settle, then return the results
|
|
69
|
+
* - Returns an array of fulfilled and/or rejected results
|
|
70
|
+
* - `first`: rejects on the first rejected promise
|
|
71
|
+
* - Returns an array of values
|
|
72
|
+
*/
|
|
73
|
+
export type PromiseStrategy = 'complete' | 'first';
|
|
74
|
+
|
|
75
|
+
export class PromiseTimeoutError extends Error {
|
|
76
|
+
constructor() {
|
|
77
|
+
super(PROMISE_MESSAGE_TIMEOUT);
|
|
78
|
+
|
|
79
|
+
this.name = PROMISE_ERROR_NAME;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export type Promises<Items extends unknown[]> = {
|
|
84
|
+
[K in keyof Items]: Promise<Items[K]>;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type PromisesOptions = {
|
|
88
|
+
signal?: AbortSignal;
|
|
89
|
+
strategy?: PromiseStrategy;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type PromisesResult<Items extends unknown[]> = {
|
|
93
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesResultItem<Value> : never;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type PromisesResultItem<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
97
|
+
|
|
98
|
+
export type RejectedPromise = {
|
|
99
|
+
status: typeof PROMISE_TYPE_REJECTED;
|
|
100
|
+
reason: unknown;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// #endregion
|
|
104
|
+
|
|
105
|
+
// #region Variables
|
|
106
|
+
|
|
107
|
+
export const PROMISE_ABORT_OPTIONS = {once: true};
|
|
108
|
+
|
|
109
|
+
export const PROMISE_ERROR_NAME = 'PromiseTimeoutError';
|
|
110
|
+
|
|
111
|
+
export const PROMISE_EVENT_NAME = 'abort';
|
|
112
|
+
|
|
113
|
+
export const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = 'Attempt expected a function or a promise';
|
|
114
|
+
|
|
115
|
+
export const PROMISE_MESSAGE_EXPECTATION_PROMISES = 'Promises expected an array of promises';
|
|
116
|
+
|
|
117
|
+
export const PROMISE_MESSAGE_EXPECTATION_RESULT = 'toResult expected a Promise';
|
|
118
|
+
|
|
119
|
+
export const PROMISE_MESSAGE_EXPECTATION_TIMED = 'Timed function expected a Promise';
|
|
120
|
+
|
|
121
|
+
export const PROMISE_MESSAGE_TIMEOUT = 'Promise timed out';
|
|
122
|
+
|
|
123
|
+
export const PROMISE_STRATEGY_ALL = new Set<PromiseStrategy>(['complete', 'first']);
|
|
124
|
+
|
|
125
|
+
export const PROMISE_STRATEGY_DEFAULT: PromiseStrategy = 'complete';
|
|
126
|
+
|
|
127
|
+
export const PROMISE_TYPE_FULFILLED = 'fulfilled';
|
|
128
|
+
|
|
129
|
+
export const PROMISE_TYPE_REJECTED = 'rejected';
|
|
130
|
+
|
|
131
|
+
// #endregion
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type {RequiredKeys} from '../models';
|
|
2
|
+
import {getPromiseOptions} from './helpers';
|
|
3
|
+
import {settlePromise} from './misc';
|
|
4
|
+
import {
|
|
5
|
+
PROMISE_ABORT_OPTIONS,
|
|
6
|
+
PROMISE_EVENT_NAME,
|
|
7
|
+
PROMISE_MESSAGE_EXPECTATION_TIMED,
|
|
8
|
+
PromiseTimeoutError,
|
|
9
|
+
type PromiseOptions,
|
|
10
|
+
} from './models';
|
|
11
|
+
|
|
12
|
+
// #region Functions
|
|
13
|
+
|
|
14
|
+
export async function getTimedPromise<Value>(
|
|
15
|
+
promise: Promise<Value>,
|
|
16
|
+
time: number,
|
|
17
|
+
signal?: AbortSignal,
|
|
18
|
+
): Promise<Value> {
|
|
19
|
+
function abort(): void {
|
|
20
|
+
cancelAnimationFrame(frame);
|
|
21
|
+
|
|
22
|
+
rejector(signal!.reason);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function run(now: DOMHighResTimeStamp): void {
|
|
26
|
+
start ??= now;
|
|
27
|
+
|
|
28
|
+
if (time === 0 || now - start >= time - 5) {
|
|
29
|
+
settlePromise(abort, rejector, new PromiseTimeoutError(), signal);
|
|
30
|
+
} else {
|
|
31
|
+
frame = requestAnimationFrame(run);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
signal?.addEventListener(PROMISE_EVENT_NAME, abort, PROMISE_ABORT_OPTIONS);
|
|
36
|
+
|
|
37
|
+
let frame: DOMHighResTimeStamp;
|
|
38
|
+
let rejector: (reason: unknown) => void;
|
|
39
|
+
let start: DOMHighResTimeStamp;
|
|
40
|
+
|
|
41
|
+
return Promise.race<Value>([
|
|
42
|
+
promise,
|
|
43
|
+
new Promise((_, reject) => {
|
|
44
|
+
rejector = reject;
|
|
45
|
+
|
|
46
|
+
frame = requestAnimationFrame(run);
|
|
47
|
+
}),
|
|
48
|
+
]).then(value => {
|
|
49
|
+
cancelAnimationFrame(frame);
|
|
50
|
+
|
|
51
|
+
signal?.removeEventListener(PROMISE_EVENT_NAME, abort);
|
|
52
|
+
|
|
53
|
+
return value;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Create a promise that should be settled within a certain amount of time
|
|
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
|
+
* @param promise Promise to settle
|
|
71
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
72
|
+
* @returns Timed promise
|
|
73
|
+
*/
|
|
74
|
+
export async function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
|
|
75
|
+
|
|
76
|
+
export async function timed<Value>(promise: Promise<Value>, options: unknown): Promise<Value> {
|
|
77
|
+
if (!(promise instanceof Promise)) {
|
|
78
|
+
return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const {signal, time} = getPromiseOptions(options);
|
|
82
|
+
|
|
83
|
+
if (signal?.aborted ?? false) {
|
|
84
|
+
return Promise.reject(signal!.reason);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// #endregion
|
package/src/result/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {isOk} from '../internal/result';
|
|
2
1
|
import {attemptPromise} from '../promise';
|
|
3
2
|
import {matchResult} from './match';
|
|
4
|
-
import
|
|
3
|
+
import {getError, ok} from './misc';
|
|
4
|
+
import type {ExtendedErr, ExtendedResult, Result} from './models';
|
|
5
5
|
import {attemptFlow} from './work/flow';
|
|
6
6
|
import {attemptPipe} from './work/pipe';
|
|
7
7
|
|
|
@@ -94,75 +94,13 @@ attempt.match = matchResult;
|
|
|
94
94
|
attempt.pipe = attemptPipe;
|
|
95
95
|
attempt.promise = attemptPromise;
|
|
96
96
|
|
|
97
|
-
/**
|
|
98
|
-
* Creates an extended error result
|
|
99
|
-
* @param error Error value
|
|
100
|
-
* @param original Original error
|
|
101
|
-
* @returns Error result
|
|
102
|
-
*/
|
|
103
|
-
export function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Creates an error result
|
|
107
|
-
* @param error Error value
|
|
108
|
-
* @returns Error result
|
|
109
|
-
*/
|
|
110
|
-
export function error<E>(value: E): Err<E>;
|
|
111
|
-
|
|
112
|
-
export function error<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
113
|
-
return getError(value, original);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
117
|
-
const errorResult: Err<E> | ExtendedErr<E> = {
|
|
118
|
-
error: value,
|
|
119
|
-
ok: false,
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
if (original instanceof Error) {
|
|
123
|
-
(errorResult as ExtendedErr<E>).original = original;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return errorResult;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Creates an ok result
|
|
131
|
-
* @param value Value
|
|
132
|
-
* @returns Ok result
|
|
133
|
-
*/
|
|
134
|
-
export function ok<Value>(value: Value): Ok<Value> {
|
|
135
|
-
return {
|
|
136
|
-
ok: true,
|
|
137
|
-
value,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Gets the value of an ok result _(or a default value)_
|
|
143
|
-
* @param value Result to unwrap
|
|
144
|
-
* @param defaultValue Default value
|
|
145
|
-
* @returns Value of the result _(or the default value)_
|
|
146
|
-
*/
|
|
147
|
-
export function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Gets the value of an ok result _(or a default value)_
|
|
151
|
-
* @param value Result to unwrap
|
|
152
|
-
* @param defaultValue Default value
|
|
153
|
-
* @returns Value of the result _(or the default value)_
|
|
154
|
-
*/
|
|
155
|
-
export function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
156
|
-
|
|
157
|
-
export function unwrap(value: unknown, defaultValue: unknown): unknown {
|
|
158
|
-
return isOk(value) ? value.value : defaultValue;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
97
|
// #endregion
|
|
162
98
|
|
|
163
99
|
// #region Exports
|
|
164
100
|
|
|
165
101
|
export {isError, isOk, isResult} from '../internal/result';
|
|
102
|
+
export {toResult as fromPromise} from '../promise/misc';
|
|
103
|
+
export {error, ok, toPromise, unwrap} from './misc';
|
|
166
104
|
export type {Err, ExtendedErr, ExtendedResult, Ok, Result} from './models';
|
|
167
105
|
|
|
168
106
|
// #endregion
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {isOk, isResult} from '../internal/result';
|
|
2
|
+
import type {AnyResult, Err, ExtendedErr, Ok, Result} from './models';
|
|
3
|
+
|
|
4
|
+
// #region Functions
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates an extended error result
|
|
8
|
+
* @param error Error value
|
|
9
|
+
* @param original Original error
|
|
10
|
+
* @returns Error result
|
|
11
|
+
*/
|
|
12
|
+
export function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Creates an error result
|
|
16
|
+
* @param error Error value
|
|
17
|
+
* @returns Error result
|
|
18
|
+
*/
|
|
19
|
+
export function error<E>(value: E): Err<E>;
|
|
20
|
+
|
|
21
|
+
export function error<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
22
|
+
return getError(value, original);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E> {
|
|
26
|
+
const errorResult: Err<E> | ExtendedErr<E> = {
|
|
27
|
+
error: value,
|
|
28
|
+
ok: false,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (original instanceof Error) {
|
|
32
|
+
(errorResult as ExtendedErr<E>).original = original;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return errorResult;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates an ok result
|
|
40
|
+
* @param value Value
|
|
41
|
+
* @returns Ok result
|
|
42
|
+
*/
|
|
43
|
+
export function ok<Value>(value: Value): Ok<Value> {
|
|
44
|
+
return {
|
|
45
|
+
ok: true,
|
|
46
|
+
value,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Converts a result to a promise
|
|
52
|
+
*
|
|
53
|
+
* Resolves if ok, rejects for error
|
|
54
|
+
* @param result Result to convert
|
|
55
|
+
* @returns Promised result
|
|
56
|
+
*/
|
|
57
|
+
export async function toPromise<Value, E = Error>(
|
|
58
|
+
callback: () => AnyResult<Value, E>,
|
|
59
|
+
): Promise<Value>;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Converts a result to a promise
|
|
63
|
+
*
|
|
64
|
+
* Resolves if ok, rejects for error
|
|
65
|
+
* @param result Result to convert
|
|
66
|
+
* @returns Promised result
|
|
67
|
+
*/
|
|
68
|
+
export async function toPromise<Value, E = Error>(result: AnyResult<Value, E>): Promise<Value>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Converts a result to a promise
|
|
72
|
+
*
|
|
73
|
+
* Resolves if ok, rejects for error
|
|
74
|
+
* @param result Result to convert
|
|
75
|
+
* @returns Promised result
|
|
76
|
+
*/
|
|
77
|
+
export async function toPromise<Value, E = Error>(
|
|
78
|
+
result: AnyResult<Value, E> | (() => AnyResult<Value, E>),
|
|
79
|
+
): Promise<Value> {
|
|
80
|
+
const actual = typeof result === 'function' ? result() : result;
|
|
81
|
+
|
|
82
|
+
if (!isResult(actual)) {
|
|
83
|
+
return Promise.reject(new Error(MESSAGE_PROMISE_RESULT));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Gets the value of an ok result _(or a default value)_
|
|
91
|
+
* @param value Result to unwrap
|
|
92
|
+
* @param defaultValue Default value
|
|
93
|
+
* @returns Value of the result _(or the default value)_
|
|
94
|
+
*/
|
|
95
|
+
export function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets the value of an ok result _(or a default value)_
|
|
99
|
+
* @param value Result to unwrap
|
|
100
|
+
* @param defaultValue Default value
|
|
101
|
+
* @returns Value of the result _(or the default value)_
|
|
102
|
+
*/
|
|
103
|
+
export function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
104
|
+
|
|
105
|
+
export function unwrap(value: unknown, defaultValue: unknown): unknown {
|
|
106
|
+
return isOk(value) ? value.value : defaultValue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// #endregion
|
|
110
|
+
|
|
111
|
+
// #region Variables
|
|
112
|
+
|
|
113
|
+
const MESSAGE_PROMISE_RESULT = 'toPromise expected to receive a Result';
|
|
114
|
+
|
|
115
|
+
// #endregion
|
package/types/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export * from './logger';
|
|
|
32
32
|
export * from './math';
|
|
33
33
|
export * from './models';
|
|
34
34
|
export * from './number';
|
|
35
|
-
export * from './promise';
|
|
35
|
+
export * from './promise/index';
|
|
36
36
|
export * from './query';
|
|
37
37
|
export * from './queue';
|
|
38
38
|
export * from './random';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type PromiseOptions } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Create a delayed promise that resolves after a certain amount of time, or rejects if aborted
|
|
4
|
+
* @param options Options for the delay
|
|
5
|
+
* @returns Delayed promise
|
|
6
|
+
*/
|
|
7
|
+
export declare function delay(options?: PromiseOptions): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Create a delayed promise that resolves after a certain amount of time
|
|
10
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
11
|
+
* @returns Delayed promise
|
|
12
|
+
*/
|
|
13
|
+
export declare function delay(time?: number): Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RequiredKeys } from '../models';
|
|
2
|
+
import { type FulfilledPromise, type PromiseOptions, type PromisesOptions, type PromiseStrategy, type RejectedPromise } from './models';
|
|
3
|
+
export declare function getPromiseOptions(input: unknown): RequiredKeys<PromiseOptions, 'time'>;
|
|
4
|
+
export declare function getPromisesOptions(input: unknown): RequiredKeys<PromisesOptions, 'strategy'>;
|
|
5
|
+
export declare function getStrategyOrDefault(value: unknown): PromiseStrategy;
|
|
6
|
+
/**
|
|
7
|
+
* Is the value a fulfilled promise result?
|
|
8
|
+
* @param value Value to check
|
|
9
|
+
* @returns `true` if the value is a fulfilled promise result, `false` otherwise
|
|
10
|
+
*/
|
|
11
|
+
export declare function isFulfilled<Value>(value: unknown): value is FulfilledPromise<Value>;
|
|
12
|
+
/**
|
|
13
|
+
* Is the value a rejected promise result?
|
|
14
|
+
* @param value Value to check
|
|
15
|
+
* @returns `true` if the value is a rejected promise result, `false` otherwise
|
|
16
|
+
*/
|
|
17
|
+
export declare function isRejected(value: unknown): value is RejectedPromise;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type PromiseOptions, type Promises, type PromisesOptions, type PromisesResult } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Wrap a promise with safety handlers, with optional abort capabilities and timeout
|
|
4
|
+
* @param promise Promise to wrap
|
|
5
|
+
* @param options Options for the promise
|
|
6
|
+
* @returns Wrapped promise
|
|
7
|
+
*/
|
|
8
|
+
export declare function attemptPromise<Value>(promise: Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
9
|
+
/**
|
|
10
|
+
* Wrap a promise-returning callback with safety handlers, with optional abort capabilities and timeout
|
|
11
|
+
* @param callback Callback to wrap
|
|
12
|
+
* @param options Options for the promise
|
|
13
|
+
* @returns Promise-wrapped callback
|
|
14
|
+
*/
|
|
15
|
+
export declare function attemptPromise<Value>(callback: () => Promise<Value>, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
16
|
+
/**
|
|
17
|
+
* Wrap a callback with a promise and safety handlers, with optional abort capabilities and timeout
|
|
18
|
+
* @param callback Callback to wrap
|
|
19
|
+
* @param options Options for the promise
|
|
20
|
+
* @returns Promise-wrapped callback
|
|
21
|
+
*/
|
|
22
|
+
export declare function attemptPromise<Value>(callback: () => Value, options?: PromiseOptions | AbortSignal | number): Promise<Value>;
|
|
23
|
+
/**
|
|
24
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
25
|
+
*
|
|
26
|
+
* Depending on the strategy, the function will either reject on the first error encountered or return an array of rejected and resolved results
|
|
27
|
+
* @param items List of promises
|
|
28
|
+
* @param options Options for handling the promises
|
|
29
|
+
* @returns List of results
|
|
30
|
+
*/
|
|
31
|
+
export declare function promises<Items extends unknown[], Options extends PromisesOptions>(items: Promises<Items>, options?: Options): Promise<Options['strategy'] extends 'first' ? Items : PromisesResult<Items>>;
|
|
32
|
+
/**
|
|
33
|
+
* Handle a list of promises, returning their results in an ordered array.
|
|
34
|
+
*
|
|
35
|
+
* If any promise in the list is rejected, the whole function will reject
|
|
36
|
+
* @param items List of promises
|
|
37
|
+
* @param strategy Strategy for handling the promises; rejects on the first error encountered
|
|
38
|
+
* @returns List of results
|
|
39
|
+
*/
|
|
40
|
+
export declare function promises<Items extends unknown[]>(items: Promises<Items>, strategy: 'first'): Promise<Items>;
|
|
41
|
+
/**
|
|
42
|
+
* Handle a list of promises, returning their results in an ordered array of rejected and resolved results
|
|
43
|
+
* @param items List of promises
|
|
44
|
+
* @param signal AbortSignal for aborting the operation _(when aborted, the promise will reject with the reason of the signal)_
|
|
45
|
+
* @returns List of results
|
|
46
|
+
*/
|
|
47
|
+
export declare function promises<Items extends unknown[]>(items: Promises<Items>, signal?: AbortSignal): Promise<PromisesResult<Items>>;
|
|
48
|
+
export { toPromise as fromResult } from '../result/misc';
|
|
49
|
+
export { delay } from './delay';
|
|
50
|
+
export { isFulfilled, isRejected } from './helpers';
|
|
51
|
+
export { cancelable, toResult } from './misc';
|
|
52
|
+
export { CancelablePromise, PromiseTimeoutError, type FulfilledPromise, type RejectedPromise, type PromiseOptions, type PromiseStrategy, type PromisesOptions, type PromisesResult, type PromisesResultItem, } from './models';
|
|
53
|
+
export { timed } from './timed';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Result } from '../result/models';
|
|
2
|
+
import { CancelablePromise, type PromiseParameters } from './models';
|
|
3
|
+
/**
|
|
4
|
+
* Create a cancelable promise
|
|
5
|
+
* @param executor Executor function for the promise
|
|
6
|
+
* @returns Cancelable promise
|
|
7
|
+
*/
|
|
8
|
+
export declare function cancelable<Value>(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void): CancelablePromise<Value>;
|
|
9
|
+
export declare function handleResult<Items extends unknown[]>(status: string, parameters: PromiseParameters<Items>): void;
|
|
10
|
+
export declare function settlePromise(aborter: () => void, settler: (value: any) => void, value: unknown, signal?: AbortSignal): void;
|
|
11
|
+
/**
|
|
12
|
+
* Converts a promise to a promised result
|
|
13
|
+
* @param callback Promise callback
|
|
14
|
+
* @returns Promised result
|
|
15
|
+
*/
|
|
16
|
+
export declare function toResult<Value>(callback: () => Promise<Value>): Promise<Result<Value>>;
|
|
17
|
+
/**
|
|
18
|
+
* Converts a promise to a promised result
|
|
19
|
+
* @param promise Promise to convert
|
|
20
|
+
* @returns Promised result
|
|
21
|
+
*/
|
|
22
|
+
export declare function toResult<Value>(promise: Promise<Value>): Promise<Result<Value>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
4
|
+
/**
|
|
5
|
+
* Cancel the promise, rejecting it with an optional reason
|
|
6
|
+
* @param reason Optional reason for canceling the promise
|
|
7
|
+
*/
|
|
8
|
+
cancel(reason?: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
export type FulfilledPromise<Value> = {
|
|
11
|
+
status: typeof PROMISE_TYPE_FULFILLED;
|
|
12
|
+
value: Value;
|
|
13
|
+
};
|
|
14
|
+
export type PromiseData<Items extends unknown[]> = {
|
|
15
|
+
last: number;
|
|
16
|
+
result: Items | PromisesResult<Items>;
|
|
17
|
+
};
|
|
18
|
+
export type PromiseHandlers<Items extends unknown[]> = {
|
|
19
|
+
resolve: (value: Items | PromisesResult<Items>) => void;
|
|
20
|
+
reject: (reason: unknown) => void;
|
|
21
|
+
};
|
|
22
|
+
export type PromiseOptions = {
|
|
23
|
+
/**
|
|
24
|
+
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
25
|
+
*/
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
/**
|
|
28
|
+
* How long to wait for (in milliseconds; defaults to `0`)
|
|
29
|
+
*/
|
|
30
|
+
time?: number;
|
|
31
|
+
};
|
|
32
|
+
export type PromiseParameters<Items extends unknown[]> = {
|
|
33
|
+
abort: () => void;
|
|
34
|
+
complete: boolean;
|
|
35
|
+
data: PromiseData<Items>;
|
|
36
|
+
handlers: PromiseHandlers<Items>;
|
|
37
|
+
index: number;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
value?: unknown;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Promise handling strategy
|
|
43
|
+
*
|
|
44
|
+
* - `complete`: wait for all promises to settle, then return the results
|
|
45
|
+
* - Returns an array of fulfilled and/or rejected results
|
|
46
|
+
* - `first`: rejects on the first rejected promise
|
|
47
|
+
* - Returns an array of values
|
|
48
|
+
*/
|
|
49
|
+
export type PromiseStrategy = 'complete' | 'first';
|
|
50
|
+
export declare class PromiseTimeoutError extends Error {
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
53
|
+
export type Promises<Items extends unknown[]> = {
|
|
54
|
+
[K in keyof Items]: Promise<Items[K]>;
|
|
55
|
+
};
|
|
56
|
+
export type PromisesOptions = {
|
|
57
|
+
signal?: AbortSignal;
|
|
58
|
+
strategy?: PromiseStrategy;
|
|
59
|
+
};
|
|
60
|
+
export type PromisesResult<Items extends unknown[]> = {
|
|
61
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesResultItem<Value> : never;
|
|
62
|
+
};
|
|
63
|
+
export type PromisesResultItem<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
64
|
+
export type RejectedPromise = {
|
|
65
|
+
status: typeof PROMISE_TYPE_REJECTED;
|
|
66
|
+
reason: unknown;
|
|
67
|
+
};
|
|
68
|
+
export declare const PROMISE_ABORT_OPTIONS: {
|
|
69
|
+
once: boolean;
|
|
70
|
+
};
|
|
71
|
+
export declare const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
72
|
+
export declare const PROMISE_EVENT_NAME = "abort";
|
|
73
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
74
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
|
|
75
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
76
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
77
|
+
export declare const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
78
|
+
export declare const PROMISE_STRATEGY_ALL: Set<PromiseStrategy>;
|
|
79
|
+
export declare const PROMISE_STRATEGY_DEFAULT: PromiseStrategy;
|
|
80
|
+
export declare const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
81
|
+
export declare const PROMISE_TYPE_REJECTED = "rejected";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RequiredKeys } from '../models';
|
|
2
|
+
import { type PromiseOptions } from './models';
|
|
3
|
+
export declare function getTimedPromise<Value>(promise: Promise<Value>, time: number, signal?: AbortSignal): Promise<Value>;
|
|
4
|
+
/**
|
|
5
|
+
* Create a promise that should be settled within a certain amount of time
|
|
6
|
+
* @param promise Promise to settle
|
|
7
|
+
* @param options Timed options
|
|
8
|
+
* @returns Timed promise
|
|
9
|
+
*/
|
|
10
|
+
export declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<PromiseOptions, 'time'>): Promise<Value>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a promise that should be settled within a certain amount of time
|
|
13
|
+
* @param promise Promise to settle
|
|
14
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
15
|
+
* @returns Timed promise
|
|
16
|
+
*/
|
|
17
|
+
export declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
|
package/types/result/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { attemptPromise } from '../promise';
|
|
2
2
|
import { matchResult } from './match';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ExtendedResult, Result } from './models';
|
|
4
4
|
import { attemptFlow } from './work/flow';
|
|
5
5
|
import { attemptPipe } from './work/pipe';
|
|
6
6
|
/**
|
|
@@ -49,38 +49,7 @@ export declare namespace attempt {
|
|
|
49
49
|
var pipe: typeof attemptPipe;
|
|
50
50
|
var promise: typeof attemptPromise;
|
|
51
51
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Creates an extended error result
|
|
54
|
-
* @param error Error value
|
|
55
|
-
* @param original Original error
|
|
56
|
-
* @returns Error result
|
|
57
|
-
*/
|
|
58
|
-
export declare function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
59
|
-
/**
|
|
60
|
-
* Creates an error result
|
|
61
|
-
* @param error Error value
|
|
62
|
-
* @returns Error result
|
|
63
|
-
*/
|
|
64
|
-
export declare function error<E>(value: E): Err<E>;
|
|
65
|
-
/**
|
|
66
|
-
* Creates an ok result
|
|
67
|
-
* @param value Value
|
|
68
|
-
* @returns Ok result
|
|
69
|
-
*/
|
|
70
|
-
export declare function ok<Value>(value: Value): Ok<Value>;
|
|
71
|
-
/**
|
|
72
|
-
* Gets the value of an ok result _(or a default value)_
|
|
73
|
-
* @param value Result to unwrap
|
|
74
|
-
* @param defaultValue Default value
|
|
75
|
-
* @returns Value of the result _(or the default value)_
|
|
76
|
-
*/
|
|
77
|
-
export declare function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
78
|
-
/**
|
|
79
|
-
* Gets the value of an ok result _(or a default value)_
|
|
80
|
-
* @param value Result to unwrap
|
|
81
|
-
* @param defaultValue Default value
|
|
82
|
-
* @returns Value of the result _(or the default value)_
|
|
83
|
-
*/
|
|
84
|
-
export declare function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
85
52
|
export { isError, isOk, isResult } from '../internal/result';
|
|
53
|
+
export { toResult as fromPromise } from '../promise/misc';
|
|
54
|
+
export { error, ok, toPromise, unwrap } from './misc';
|
|
86
55
|
export type { Err, ExtendedErr, ExtendedResult, Ok, Result } from './models';
|