@byloth/core 2.0.0-rc.9 → 2.0.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/core.js +4087 -621
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +17 -13
- package/src/core/types.ts +41 -0
- package/src/helpers.ts +11 -2
- package/src/index.ts +12 -9
- package/src/models/aggregators/aggregated-async-iterator.ts +920 -21
- package/src/models/aggregators/aggregated-iterator.ts +838 -22
- package/src/models/aggregators/reduced-iterator.ts +827 -11
- package/src/models/aggregators/types.ts +153 -10
- package/src/models/callbacks/callable-object.ts +42 -6
- package/src/models/callbacks/index.ts +2 -2
- package/src/models/callbacks/publisher.ts +160 -4
- package/src/models/callbacks/switchable-callback.ts +230 -23
- package/src/models/callbacks/types.ts +16 -0
- package/src/models/exceptions/core.ts +132 -3
- package/src/models/exceptions/index.ts +405 -13
- package/src/models/index.ts +4 -8
- package/src/models/iterators/smart-async-iterator.ts +827 -22
- package/src/models/iterators/smart-iterator.ts +755 -20
- package/src/models/iterators/types.ts +268 -9
- package/src/models/json/json-storage.ts +508 -110
- package/src/models/json/types.ts +10 -1
- package/src/models/promises/deferred-promise.ts +85 -5
- package/src/models/promises/index.ts +1 -3
- package/src/models/promises/smart-promise.ts +272 -4
- package/src/models/promises/timed-promise.ts +43 -1
- package/src/models/promises/types.ts +84 -2
- package/src/models/timers/clock.ts +109 -19
- package/src/models/timers/countdown.ts +176 -21
- package/src/models/timers/game-loop.ts +266 -0
- package/src/models/timers/index.ts +2 -1
- package/src/models/types.ts +6 -5
- package/src/utils/async.ts +43 -0
- package/src/utils/curve.ts +85 -0
- package/src/utils/date.ts +204 -10
- package/src/utils/dom.ts +16 -2
- package/src/utils/index.ts +3 -2
- package/src/utils/iterator.ts +200 -17
- package/src/utils/math.ts +55 -3
- package/src/utils/random.ts +139 -2
- package/src/utils/string.ts +11 -0
- package/src/models/game-loop.ts +0 -83
- package/src/models/promises/long-running-task.ts +0 -294
- package/src/models/promises/thenable.ts +0 -97
package/src/models/json/types.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A type that represents a JSON array.
|
|
3
|
+
*/
|
|
1
4
|
export type JSONArray = JSONValue[];
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
/**
|
|
7
|
+
* A type that represents a JSON object.
|
|
8
|
+
*/
|
|
4
9
|
export interface JSONObject { [key: string]: JSONValue }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A type that represents all the possible values of a JSON value.
|
|
13
|
+
*/
|
|
5
14
|
export type JSONValue = boolean | number | string | null | JSONObject | JSONArray;
|
|
@@ -2,11 +2,74 @@ import type { PromiseResolver, PromiseRejecter, FulfilledHandler, RejectedHandle
|
|
|
2
2
|
|
|
3
3
|
import SmartPromise from "./smart-promise.js";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A class representing a {@link SmartPromise} that can be resolved or rejected from the "outside".
|
|
7
|
+
* The `resolve` and `reject` methods are exposed to allow the promise to be settled from another context.
|
|
8
|
+
*
|
|
9
|
+
* It's particularly useful in scenarios where the promise is created and needs to be awaited in one place,
|
|
10
|
+
* while being resolved or rejected in another (e.g. an event handler for an user interaction).
|
|
11
|
+
*
|
|
12
|
+
* This is a change in the approach to promises: instead of defining how the promise will be resolved (or rejected),
|
|
13
|
+
* you define how to handle the resolution (or rejection) when it occurs.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
17
|
+
*
|
|
18
|
+
* promise.then((result) => console.log(result)); // ["Hello,", "World!"]
|
|
19
|
+
* promise.resolve("Hello, World!");
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @template T The type of value the promise expects to initially be resolved with. Default is `void`.
|
|
23
|
+
* @template F
|
|
24
|
+
* The type of value returned by the `onFulfilled` callback.
|
|
25
|
+
* This will be the actual type of value the promise will eventually resolve to. Default is `T`.
|
|
26
|
+
* @template R
|
|
27
|
+
* The type of value possibly returned by the `onRejected` callback.
|
|
28
|
+
* This will be coupled with the type of value the promise will eventually resolve to, if provided. Default is `never`.
|
|
29
|
+
*/
|
|
5
30
|
export default class DeferredPromise<T = void, F = T, R = never> extends SmartPromise<F | R>
|
|
6
31
|
{
|
|
32
|
+
/**
|
|
33
|
+
* The exposed function that allows to resolve the promise.
|
|
34
|
+
*
|
|
35
|
+
* This protected property is the only one that can be modified directly by the derived classes.
|
|
36
|
+
* If you're looking for the public and readonly property, use the {@link DeferredPromise.resolve} getter instead.
|
|
37
|
+
*/
|
|
7
38
|
protected _resolve: PromiseResolver<T>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The exposed function that allows to reject the promise.
|
|
42
|
+
*/
|
|
43
|
+
public get resolve(): PromiseResolver<T> { return this._resolve; }
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The exposed function that allows to reject the promise.
|
|
47
|
+
*
|
|
48
|
+
* This protected property is the only one that can be modified directly by the derived classes.
|
|
49
|
+
* If you're looking for the public and readonly property, use the {@link DeferredPromise.reject} getter instead.
|
|
50
|
+
*/
|
|
8
51
|
protected _reject: PromiseRejecter;
|
|
9
52
|
|
|
53
|
+
/**
|
|
54
|
+
* The exposed function that allows to reject the promise.
|
|
55
|
+
*/
|
|
56
|
+
public get reject(): PromiseRejecter { return this._reject; }
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Initializes a new instance of the {@link DeferredPromise} class.
|
|
60
|
+
*
|
|
61
|
+
* ---
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* ---
|
|
69
|
+
*
|
|
70
|
+
* @param onFulfilled The callback to execute once the promise is fulfilled.
|
|
71
|
+
* @param onRejected The callback to execute once the promise is rejected.
|
|
72
|
+
*/
|
|
10
73
|
public constructor(onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<unknown, R> | null)
|
|
11
74
|
{
|
|
12
75
|
let _resolve: PromiseResolver<T>;
|
|
@@ -21,15 +84,32 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
|
|
|
21
84
|
_reject = reject;
|
|
22
85
|
});
|
|
23
86
|
|
|
24
|
-
this._promise.then(onFulfilled as FulfilledHandler<F | R>, onRejected);
|
|
87
|
+
this._promise = this._promise.then(onFulfilled as FulfilledHandler<F | R>, onRejected);
|
|
25
88
|
|
|
26
89
|
this._resolve = _resolve!;
|
|
27
90
|
this._reject = _reject!;
|
|
28
91
|
}
|
|
29
92
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Watches another promise and resolves or rejects this promise when the other one is settled.
|
|
95
|
+
*
|
|
96
|
+
* ---
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const promise = new Promise<string>((resolve) => setTimeout(() => resolve("Hello, World!"), 1_000));
|
|
101
|
+
* const deferred = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
102
|
+
*
|
|
103
|
+
* deferred.then((result) => console.log(result)); // ["Hello,", "World!"]
|
|
104
|
+
* deferred.watch(promise);
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* ---
|
|
108
|
+
*
|
|
109
|
+
* @param otherPromise The promise to watch.
|
|
110
|
+
*
|
|
111
|
+
* @returns The current instance of the {@link DeferredPromise} class.
|
|
112
|
+
*/
|
|
33
113
|
public watch(otherPromise: PromiseLike<T>): this
|
|
34
114
|
{
|
|
35
115
|
otherPromise.then(this.resolve, this.reject);
|
|
@@ -37,5 +117,5 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
|
|
|
37
117
|
return this;
|
|
38
118
|
}
|
|
39
119
|
|
|
40
|
-
public readonly [Symbol.toStringTag]: string = "DeferredPromise";
|
|
120
|
+
public override readonly [Symbol.toStringTag]: string = "DeferredPromise";
|
|
41
121
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import DeferredPromise from "./deferred-promise.js";
|
|
2
|
-
import LongRunningTask from "./long-running-task.js";
|
|
3
2
|
import SmartPromise from "./smart-promise.js";
|
|
4
|
-
import Thenable from "./thenable.js";
|
|
5
3
|
import TimedPromise from "./timed-promise.js";
|
|
6
4
|
|
|
7
|
-
export { DeferredPromise,
|
|
5
|
+
export { DeferredPromise, SmartPromise, TimedPromise };
|
|
@@ -1,18 +1,131 @@
|
|
|
1
1
|
import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types.js";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* A wrapper class representing an enhanced version of the native {@link Promise} object.
|
|
5
|
+
*
|
|
6
|
+
* It provides additional properties to check the state of the promise itself.
|
|
7
|
+
* The state can be either `pending`, `fulfilled` or `rejected` and is accessible through
|
|
8
|
+
* the {@link SmartPromise.isPending}, {@link SmartPromise.isFulfilled} & {@link SmartPromise.isRejected} properties.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
12
|
+
* {
|
|
13
|
+
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* console.log(promise.isPending); // true
|
|
17
|
+
* console.log(promise.isFulfilled); // false
|
|
18
|
+
*
|
|
19
|
+
* console.log(await promise); // "Hello, World!"
|
|
20
|
+
*
|
|
21
|
+
* console.log(promise.isPending); // false
|
|
22
|
+
* console.log(promise.isFulfilled); // true
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
26
|
+
*/
|
|
3
27
|
export default class SmartPromise<T = void> implements Promise<T>
|
|
4
28
|
{
|
|
29
|
+
/**
|
|
30
|
+
* Wraps a new {@link SmartPromise} object around an existing native {@link Promise} object.
|
|
31
|
+
*
|
|
32
|
+
* ---
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const request = fetch("https://api.example.com/data");
|
|
37
|
+
* const smartRequest = SmartPromise.FromPromise(request);
|
|
38
|
+
*
|
|
39
|
+
* console.log(request.isPending); // Throws an error: `isPending` is not a property of `Promise`.
|
|
40
|
+
* console.log(smartRequest.isPending); // true
|
|
41
|
+
*
|
|
42
|
+
* const response = await request;
|
|
43
|
+
* console.log(smartRequest.isFulfilled); // true
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
48
|
+
* @param promise The promise to wrap.
|
|
49
|
+
*
|
|
50
|
+
* @returns A new {@link SmartPromise} object that wraps the provided promise.
|
|
51
|
+
*/
|
|
5
52
|
public static FromPromise<T>(promise: Promise<T>): SmartPromise<T>
|
|
6
53
|
{
|
|
7
54
|
return new SmartPromise((resolve, reject) => promise.then(resolve, reject));
|
|
8
55
|
}
|
|
9
56
|
|
|
57
|
+
/**
|
|
58
|
+
* A flag indicating whether the promise is still pending or not.
|
|
59
|
+
*
|
|
60
|
+
* The protected property is the only one that can be modified directly by the derived classes.
|
|
61
|
+
* If you're looking for the public and readonly property, use the {@link SmartPromise.isPending} getter instead.
|
|
62
|
+
*/
|
|
10
63
|
protected _isPending: boolean;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A flag indicating whether the promise is still pending or not.
|
|
67
|
+
*/
|
|
68
|
+
public get isPending(): boolean
|
|
69
|
+
{
|
|
70
|
+
return this._isPending;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* A flag indicating whether the promise has been fulfilled or not.
|
|
75
|
+
*
|
|
76
|
+
* The protected property is the only one that can be modified directly by the derived classes.
|
|
77
|
+
* If you're looking for the public and readonly property, use the {@link SmartPromise.isFulfilled} getter instead.
|
|
78
|
+
*/
|
|
11
79
|
protected _isFulfilled: boolean;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* A flag indicating whether the promise has been fulfilled or not.
|
|
83
|
+
*/
|
|
84
|
+
public get isFulfilled(): boolean
|
|
85
|
+
{
|
|
86
|
+
return this._isFulfilled;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A flag indicating whether the promise has been rejected or not.
|
|
91
|
+
*
|
|
92
|
+
* The protected property is the only one that can be modified directly by the derived classes.
|
|
93
|
+
* If you're looking for the public and readonly property, use the {@link SmartPromise.isRejected} getter instead.
|
|
94
|
+
*/
|
|
12
95
|
protected _isRejected: boolean;
|
|
13
96
|
|
|
97
|
+
/**
|
|
98
|
+
* A flag indicating whether the promise has been rejected or not.
|
|
99
|
+
*/
|
|
100
|
+
public get isRejected(): boolean
|
|
101
|
+
{
|
|
102
|
+
return this._isRejected;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The native {@link Promise} object wrapped by this instance.
|
|
107
|
+
*/
|
|
14
108
|
protected _promise: Promise<T>;
|
|
15
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Initializes a new instance of the {@link SmartPromise} class.
|
|
112
|
+
*
|
|
113
|
+
* ---
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
118
|
+
* {
|
|
119
|
+
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* ---
|
|
124
|
+
*
|
|
125
|
+
* @param executor
|
|
126
|
+
* The function responsible for eventually resolving or rejecting the promise.
|
|
127
|
+
* Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
|
|
128
|
+
*/
|
|
16
129
|
public constructor(executor: PromiseExecutor<T>)
|
|
17
130
|
{
|
|
18
131
|
this._isPending = true;
|
|
@@ -38,12 +151,91 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
38
151
|
.then(_onFulfilled, _onRejected);
|
|
39
152
|
}
|
|
40
153
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
|
|
156
|
+
*
|
|
157
|
+
* ---
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
162
|
+
* {
|
|
163
|
+
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
164
|
+
* });
|
|
165
|
+
*
|
|
166
|
+
* console.log(await promise.then()); // "Hello, World!"
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* ---
|
|
170
|
+
*
|
|
171
|
+
* @returns A new {@link Promise} identical to the original one.
|
|
172
|
+
*/
|
|
45
173
|
public then(onFulfilled?: null): Promise<T>;
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Attaches a callback that executes right after the promise is fulfilled.
|
|
177
|
+
*
|
|
178
|
+
* The previous result of the promise is passed as the argument to the callback.
|
|
179
|
+
* The callback's return value is considered the new promise's result instead.
|
|
180
|
+
*
|
|
181
|
+
* ---
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
186
|
+
* {
|
|
187
|
+
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
188
|
+
* });
|
|
189
|
+
*
|
|
190
|
+
* promise.then((result) => console.log(result)); // "Hello, World!"
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* ---
|
|
194
|
+
*
|
|
195
|
+
* @template F The type of value the new promise will eventually resolve to. Default is `T`.
|
|
196
|
+
*
|
|
197
|
+
* @param onFulfilled The callback to execute once the promise is fulfilled.
|
|
198
|
+
*
|
|
199
|
+
* @returns A new {@link Promise} resolved with the return value of the callback.
|
|
200
|
+
*/
|
|
46
201
|
public then<F = T>(onFulfilled: FulfilledHandler<T, F>, onRejected?: null): Promise<F>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Attaches callbacks that executes right after the promise is fulfilled or rejected.
|
|
205
|
+
*
|
|
206
|
+
* The previous result of the promise is passed as the argument to the fulfillment callback.
|
|
207
|
+
* The fulfillment callback's return value is considered the new promise's result instead.
|
|
208
|
+
*
|
|
209
|
+
* If an error is thrown during execution, the rejection callback is then executed instead.
|
|
210
|
+
*
|
|
211
|
+
* Also note that:
|
|
212
|
+
* - If the rejection callback runs properly, the error is considered handled.
|
|
213
|
+
* The rejection callback's return value is considered the new promise's result.
|
|
214
|
+
* - If the rejection callback throws an error, the new promise is rejected with that error.
|
|
215
|
+
*
|
|
216
|
+
* ---
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* const promise = new SmartPromise((resolve, reject) =>
|
|
221
|
+
* {
|
|
222
|
+
* setTimeout(resolve, Math.random() * 1_000);
|
|
223
|
+
* setTimeout(reject, Math.random() * 1_000);
|
|
224
|
+
* });
|
|
225
|
+
*
|
|
226
|
+
* promise.then(() => console.log("OK!"), () => console.log("KO!")); // "OK!" or "KO!"
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* ---
|
|
230
|
+
*
|
|
231
|
+
* @template F The type of value the new promise will eventually resolve to. Default is `T`.
|
|
232
|
+
* @template R The type of value the new promise will eventually resolve to. Default is `never`.
|
|
233
|
+
*
|
|
234
|
+
* @param onFulfilled The callback to execute once the promise is fulfilled.
|
|
235
|
+
* @param onRejected The callback to execute once the promise is rejected.
|
|
236
|
+
*
|
|
237
|
+
* @returns A new {@link Promise} resolved or rejected based on the callbacks.
|
|
238
|
+
*/
|
|
47
239
|
public then<F = T, R = never>(onFulfilled: FulfilledHandler<T, F>, onRejected: RejectedHandler<unknown, R>)
|
|
48
240
|
: Promise<F | R>;
|
|
49
241
|
public then<F = T, R = never>(
|
|
@@ -53,12 +245,88 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
53
245
|
return this._promise.then(onFulfilled, onRejected);
|
|
54
246
|
}
|
|
55
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
|
|
250
|
+
*
|
|
251
|
+
* ---
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* const promise = new SmartPromise((resolve, reject) =>
|
|
256
|
+
* {
|
|
257
|
+
* setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
|
|
258
|
+
* });
|
|
259
|
+
*
|
|
260
|
+
* promise.catch(); // Uncaught Error: An unknown error occurred.
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* ---
|
|
264
|
+
*
|
|
265
|
+
* @returns A new {@link Promise} identical to the original one.
|
|
266
|
+
*/
|
|
56
267
|
public catch(onRejected?: null): Promise<T>;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Attaches a callback to handle the potential rejection of the promise.
|
|
271
|
+
* If it happens, the callback is then executed.
|
|
272
|
+
*
|
|
273
|
+
* Also note that:
|
|
274
|
+
* - If the callback runs properly, the error is considered handled.
|
|
275
|
+
* The callback's return value is considered the new promise's result.
|
|
276
|
+
* - If the callback throws an error, the new promise is rejected with that error.
|
|
277
|
+
*
|
|
278
|
+
* ---
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* const promise = new SmartPromise((resolve, reject) =>
|
|
283
|
+
* {
|
|
284
|
+
* setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
|
|
285
|
+
* });
|
|
286
|
+
*
|
|
287
|
+
* promise.catch((reason) => console.error(reason)); // "Error: An unknown error occurred."
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* ---
|
|
291
|
+
*
|
|
292
|
+
* @template R The type of value the new promise will eventually resolve to. Default is `T`.
|
|
293
|
+
*
|
|
294
|
+
* @param onRejected The callback to execute once the promise is rejected.
|
|
295
|
+
*
|
|
296
|
+
* @returns A new {@link Promise} able to catch and handle the potential error.
|
|
297
|
+
*/
|
|
57
298
|
public catch<R = never>(onRejected: RejectedHandler<unknown, R>): Promise<T | R>;
|
|
58
299
|
public catch<R = never>(onRejected?: RejectedHandler<unknown, R> | null): Promise<T | R>
|
|
59
300
|
{
|
|
60
301
|
return this._promise.catch(onRejected);
|
|
61
302
|
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Attaches a callback that executes right after the promise is settled, regardless of the outcome.
|
|
306
|
+
*
|
|
307
|
+
* ---
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```ts
|
|
311
|
+
* const promise = new SmartPromise((resolve, reject) =>
|
|
312
|
+
* {
|
|
313
|
+
* setTimeout(resolve, Math.random() * 1_000);
|
|
314
|
+
* setTimeout(reject, Math.random() * 1_000);
|
|
315
|
+
* });
|
|
316
|
+
*
|
|
317
|
+
*
|
|
318
|
+
* promise
|
|
319
|
+
* .then(() => console.log("OK!")) // Logs "OK!" if the promise is fulfilled.
|
|
320
|
+
* .catch(() => console.log("KO!")) // Logs "KO!" if the promise is rejected.
|
|
321
|
+
* .finally(() => console.log("Done!")); // Always logs "Done!".
|
|
322
|
+
* ```
|
|
323
|
+
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
326
|
+
* @param onFinally The callback to execute when once promise is settled.
|
|
327
|
+
*
|
|
328
|
+
* @returns A new {@link Promise} that executes the callback once the promise is settled.
|
|
329
|
+
*/
|
|
62
330
|
public finally(onFinally?: (() => void) | null): Promise<T>
|
|
63
331
|
{
|
|
64
332
|
return this._promise.finally(onFinally);
|
|
@@ -3,8 +3,50 @@ import { TimeoutException } from "../exceptions/index.js";
|
|
|
3
3
|
import SmartPromise from "./smart-promise.js";
|
|
4
4
|
import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A class representing a {@link SmartPromise} that rejects automatically after a given time.
|
|
8
|
+
* It's useful for operations that must be completed within a certain time frame.
|
|
9
|
+
*
|
|
10
|
+
* If the operation takes longer than the specified time, the promise is rejected with a {@link TimeoutException}.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const promise = new TimedPromise<string>((resolve, reject) =>
|
|
14
|
+
* {
|
|
15
|
+
* setTimeout(() => resolve("Hello, World!"), Math.random() * 10_000);
|
|
16
|
+
*
|
|
17
|
+
* }, 5_000);
|
|
18
|
+
*
|
|
19
|
+
* promise
|
|
20
|
+
* .then((result) => console.log(result)) // "Hello, World!"
|
|
21
|
+
* .catch((error) => console.error(error)); // TimeoutException: The operation has timed out.
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
25
|
+
*/
|
|
6
26
|
export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
7
27
|
{
|
|
28
|
+
/**
|
|
29
|
+
* Initializes a new instance of the {@link TimedPromise} class.
|
|
30
|
+
*
|
|
31
|
+
* ---
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const promise = new TimedPromise<string>((resolve, reject) =>
|
|
36
|
+
* {
|
|
37
|
+
* setTimeout(() => resolve("Hello, World!"), Math.random() * 10_000);
|
|
38
|
+
*
|
|
39
|
+
* }, 5_000);
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ---
|
|
43
|
+
*
|
|
44
|
+
* @param executor
|
|
45
|
+
* The function responsible for eventually resolving or rejecting the promise.
|
|
46
|
+
* Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
|
|
47
|
+
*
|
|
48
|
+
* @param timeout The maximum time in milliseconds that the operation can take before timing out.
|
|
49
|
+
*/
|
|
8
50
|
public constructor(executor: PromiseExecutor<T>, timeout?: number)
|
|
9
51
|
{
|
|
10
52
|
super((resolve, reject) =>
|
|
@@ -27,5 +69,5 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
|
27
69
|
});
|
|
28
70
|
}
|
|
29
71
|
|
|
30
|
-
public readonly [Symbol.toStringTag]: string = "TimedPromise";
|
|
72
|
+
public override readonly [Symbol.toStringTag]: string = "TimedPromise";
|
|
31
73
|
}
|
|
@@ -1,10 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* An union type that represents a value that can be either a value or a promise of that value.
|
|
3
|
+
* This is useful when you want to handle both synchronous and asynchronous values in the same way.
|
|
4
|
+
*
|
|
5
|
+
* ```ts
|
|
6
|
+
* async function splitWords(value: MaybePromise<string>): Promise<string[]>
|
|
7
|
+
* {
|
|
8
|
+
* return (await value).split(" ");
|
|
9
|
+
* }
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @template T The type of the value.
|
|
13
|
+
*/
|
|
3
14
|
export type MaybePromise<T> = T | PromiseLike<T>;
|
|
4
15
|
|
|
16
|
+
/**
|
|
17
|
+
* An utility type that represents the callback that is executed when a promise is fulfilled.
|
|
18
|
+
* It's compatible with the `onFulfilled` parameter of the `then` method of the native {@link Promise} object.
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const onFulfilled: FulfilledHandler<string, string[]> = (value) => value.split(" ");
|
|
22
|
+
*
|
|
23
|
+
* await new Promise<string>((resolve) => resolve("Hello, World!"))
|
|
24
|
+
* .then(onFulfilled);
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @template T The type of value accepted by the function. Default is `void`.
|
|
28
|
+
* @template R The type of value returned by the function. Default is `T`.
|
|
29
|
+
*/
|
|
5
30
|
export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* An utility type that represents the callback that is executed when a promise is rejected.
|
|
34
|
+
* It's compatible with the `onRejected` parameter of the `then`/`catch` methods of the native {@link Promise} object.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const onRejected: RejectedHandler<unknown, string> = (reason) => String(reason);
|
|
38
|
+
*
|
|
39
|
+
* await new Promise<string>((_, reject) => reject(new Error("An error occurred.")))
|
|
40
|
+
* .catch(onRejected);
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @template E The type of value accepted by the function. Default is `unknown`.
|
|
44
|
+
* @template R The type of value returned by the function. Default is `never`.
|
|
45
|
+
*/
|
|
6
46
|
export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromise<R>;
|
|
7
47
|
|
|
48
|
+
/**
|
|
49
|
+
* An utility type that represents a function that can be used to resolve a promise.
|
|
50
|
+
* It's compatible with the `resolve` parameter of the native {@link Promise} executor.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* let _resolve: PromiseResolver<string> = (result) => console.log(result);
|
|
54
|
+
*
|
|
55
|
+
* await new Promise<string>((resolve) => { _resolve = resolve; });
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @template T The type of the value accepted by the function. Default is `void`.
|
|
59
|
+
*/
|
|
8
60
|
export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* An utility type that represents a function that can be used to reject a promise.
|
|
64
|
+
* It's compatible with the `reject` parameter of the native {@link Promise} executor.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* let _reject: PromiseRejecter<string> = (reason) => console.error(reason);
|
|
68
|
+
*
|
|
69
|
+
* await new Promise<string>((_, reject) => { _reject = reject; });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @template E The type of the value accepted by the function. Default is `unknown`.
|
|
73
|
+
*/
|
|
9
74
|
export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* An utility type that represents the function that will be executed by the promise.
|
|
78
|
+
* It's compatible with the `executor` parameter of the native {@link Promise} object.
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* const executor: PromiseExecutor<string> = (resolve, reject) =>
|
|
82
|
+
* {
|
|
83
|
+
* setTimeout(() => resolve("Hello, World!"), 1_000);
|
|
84
|
+
* };
|
|
85
|
+
*
|
|
86
|
+
* await new Promise<string>(executor);
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @template T The type of value accepted by the `resolve` function. Default is `void`.
|
|
90
|
+
* @template E The type of value accepted by the `reject` function. Default is `unknown`.
|
|
91
|
+
*/
|
|
10
92
|
export type PromiseExecutor<T = void, E = unknown> = (resolve: PromiseResolver<T>, reject: PromiseRejecter<E>) => void;
|