@byloth/core 2.0.1 → 2.1.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/README.md +1 -0
- package/dist/core.cjs +6 -0
- package/dist/core.cjs.map +1 -0
- package/dist/{core.js → core.esm.js} +1098 -779
- package/dist/core.esm.js.map +1 -0
- package/dist/core.global.js +6 -0
- package/dist/core.global.js.map +1 -0
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +15 -11
- package/src/core/types.ts +43 -10
- package/src/index.ts +9 -2
- package/src/models/aggregators/aggregated-async-iterator.ts +5 -0
- package/src/models/aggregators/aggregated-iterator.ts +5 -0
- package/src/models/aggregators/reduced-iterator.ts +18 -5
- package/src/models/aggregators/types.ts +35 -0
- package/src/models/callbacks/callable-object.ts +7 -0
- package/src/models/callbacks/publisher.ts +16 -12
- package/src/models/callbacks/switchable-callback.ts +9 -1
- package/src/models/callbacks/types.ts +35 -0
- package/src/models/collections/index.ts +4 -0
- package/src/models/collections/map-view.ts +206 -0
- package/src/models/collections/set-view.ts +204 -0
- package/src/models/collections/types.ts +25 -0
- package/src/models/exceptions/core.ts +10 -1
- package/src/models/exceptions/index.ts +40 -1
- package/src/models/index.ts +1 -0
- package/src/models/iterators/smart-async-iterator.ts +5 -0
- package/src/models/iterators/smart-iterator.ts +5 -0
- package/src/models/iterators/types.ts +79 -1
- package/src/models/json/json-storage.ts +4 -1
- package/src/models/json/types.ts +1 -1
- package/src/models/promises/deferred-promise.ts +5 -0
- package/src/models/promises/smart-promise.ts +5 -0
- package/src/models/promises/timed-promise.ts +5 -0
- package/src/models/promises/types.ts +30 -0
- package/src/models/timers/clock.ts +3 -0
- package/src/models/timers/countdown.ts +5 -0
- package/src/models/timers/game-loop.ts +3 -0
- package/src/models/types.ts +3 -8
- package/src/utils/async.ts +15 -0
- package/src/utils/curve.ts +1 -1
- package/src/utils/date.ts +36 -6
- package/src/utils/dom.ts +5 -0
- package/src/utils/iterator.ts +43 -3
- package/src/utils/math.ts +15 -0
- package/src/utils/random.ts +4 -0
- package/src/utils/string.ts +5 -0
- package/dist/core.js.map +0 -1
|
@@ -3,6 +3,9 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* An union type that represents an iterable object that can be either synchronous or asynchronous.
|
|
5
5
|
*
|
|
6
|
+
* ---
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
6
9
|
* ```ts
|
|
7
10
|
* const iterable: MaybeAsyncIterable<number> = [...];
|
|
8
11
|
* for await (const value of iterable)
|
|
@@ -11,6 +14,8 @@ import type { MaybePromise } from "../promises/types.js";
|
|
|
11
14
|
* }
|
|
12
15
|
* ```
|
|
13
16
|
*
|
|
17
|
+
* ---
|
|
18
|
+
*
|
|
14
19
|
* @template T The type of the elements in the iterable.
|
|
15
20
|
*/
|
|
16
21
|
export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
|
|
@@ -18,6 +23,9 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
|
|
|
18
23
|
/**
|
|
19
24
|
* An union type that represents an iterator object that can be either synchronous or asynchronous.
|
|
20
25
|
*
|
|
26
|
+
* ---
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
21
29
|
* ```ts
|
|
22
30
|
* const iterator: MaybeAsyncIterator<number> = { ... };
|
|
23
31
|
* for await (const value of iterator)
|
|
@@ -26,6 +34,8 @@ export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> |
|
|
|
26
34
|
* }
|
|
27
35
|
* ```
|
|
28
36
|
*
|
|
37
|
+
* ---
|
|
38
|
+
*
|
|
29
39
|
* @template T The type of the elements in the iterator.
|
|
30
40
|
*/
|
|
31
41
|
export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
|
|
@@ -33,6 +43,9 @@ export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> |
|
|
|
33
43
|
/**
|
|
34
44
|
* An union type that represents a generator object that can be either synchronous or asynchronous.
|
|
35
45
|
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
36
49
|
* ```ts
|
|
37
50
|
* const generator: MaybeAsyncGenerator<number> = [async] function*() { ... };
|
|
38
51
|
* for await (const value of generator)
|
|
@@ -46,6 +59,9 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
|
|
|
46
59
|
* An utility type that represents a function that returns a generator object.
|
|
47
60
|
* It differs from the native `GeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
48
61
|
*
|
|
62
|
+
* ---
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
49
65
|
* ```ts
|
|
50
66
|
* const generatorFn: GeneratorFunction<number> = function*() { ... };
|
|
51
67
|
* const generator: Generator<number> = generatorFn();
|
|
@@ -56,6 +72,8 @@ export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N>
|
|
|
56
72
|
* }
|
|
57
73
|
* ```
|
|
58
74
|
*
|
|
75
|
+
* ---
|
|
76
|
+
*
|
|
59
77
|
* @template T The type of the elements generated by the generator.
|
|
60
78
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
61
79
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -66,6 +84,9 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
|
|
|
66
84
|
* An utility type that represents a function that returns an asynchronous generator object.
|
|
67
85
|
* It differs from the native `AsyncGeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
68
86
|
*
|
|
87
|
+
* ---
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
69
90
|
* ```ts
|
|
70
91
|
* const asyncGeneratorFn: AsyncGeneratorFunction<number> = async function*() { ... };
|
|
71
92
|
* const generator: AsyncGenerator<number> = asyncGeneratorFn();
|
|
@@ -75,6 +96,8 @@ export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R
|
|
|
75
96
|
* }
|
|
76
97
|
* ```
|
|
77
98
|
*
|
|
99
|
+
* ---
|
|
100
|
+
*
|
|
78
101
|
* @template T The type of the elements generated by the generator.
|
|
79
102
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
80
103
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -85,6 +108,9 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
|
|
|
85
108
|
* An utility type that represents a function that returns a
|
|
86
109
|
* generator object that can be either synchronous or asynchronous.
|
|
87
110
|
*
|
|
111
|
+
* ---
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
88
114
|
* ```ts
|
|
89
115
|
* const generatorFn: MaybeAsyncGeneratorFunction<number> = [async] function*() { ... };
|
|
90
116
|
* const generator: MaybeAsyncGenerator<number> = generatorFn();
|
|
@@ -94,6 +120,8 @@ export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGene
|
|
|
94
120
|
* }
|
|
95
121
|
* ```
|
|
96
122
|
*
|
|
123
|
+
* ---
|
|
124
|
+
*
|
|
97
125
|
* @template T The type of the elements generated by the generator.
|
|
98
126
|
* @template R The type of the return value of the generator. Default is `void`.
|
|
99
127
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -105,13 +133,18 @@ export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => Mayb
|
|
|
105
133
|
* {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
106
134
|
* It can be used to transform the elements of an iterable.
|
|
107
135
|
*
|
|
136
|
+
* ---
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
108
139
|
* ```ts
|
|
109
140
|
* const iteratee: Iteratee<number, string> = (value: number) => `${value}`;
|
|
110
141
|
* const values: string[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
111
|
-
*
|
|
142
|
+
*
|
|
112
143
|
* console.log(values); // ["1", "2", "3", "4", "5"]
|
|
113
144
|
* ```
|
|
114
145
|
*
|
|
146
|
+
* ---
|
|
147
|
+
*
|
|
115
148
|
* @template T The type of the elements in the iterable.
|
|
116
149
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
117
150
|
*/
|
|
@@ -121,6 +154,9 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
|
|
|
121
154
|
* An utility type that represents an asynchronous {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
122
155
|
* It can be used to transform the elements of an iterable asynchronously.
|
|
123
156
|
*
|
|
157
|
+
* ---
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
124
160
|
* ```ts
|
|
125
161
|
* const iteratee: AsyncIteratee<number, string> = async (value: number) => `${value}`;
|
|
126
162
|
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
@@ -130,6 +166,8 @@ export type Iteratee<T, R = void> = (value: T, index: number) => R;
|
|
|
130
166
|
* }
|
|
131
167
|
* ```
|
|
132
168
|
*
|
|
169
|
+
* ---
|
|
170
|
+
*
|
|
133
171
|
* @template T The type of the elements in the iterable.
|
|
134
172
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
135
173
|
*/
|
|
@@ -140,6 +178,9 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
|
|
|
140
178
|
* function that can be either synchronous or asynchronous.
|
|
141
179
|
* It can be used to transform the elements of an iterable.
|
|
142
180
|
*
|
|
181
|
+
* ---
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
143
184
|
* ```ts
|
|
144
185
|
* const iteratee: MaybeAsyncIteratee<number, string> = [async] (value: number) => `${value}`;
|
|
145
186
|
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
@@ -149,6 +190,8 @@ export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>
|
|
|
149
190
|
* }
|
|
150
191
|
* ```
|
|
151
192
|
*
|
|
193
|
+
* ---
|
|
194
|
+
*
|
|
152
195
|
* @template T The type of the elements in the iterable.
|
|
153
196
|
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
154
197
|
*/
|
|
@@ -161,6 +204,9 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
|
|
|
161
204
|
* It can be used to ensure the type of the elements of an iterable
|
|
162
205
|
* while allowing the type-system to infer them correctly.
|
|
163
206
|
*
|
|
207
|
+
* ---
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
164
210
|
* ```ts
|
|
165
211
|
* const iteratee: TypeGuardPredicate<number | string, string> = (value): value is string => typeof value === "string";
|
|
166
212
|
* const values: string[] = [1, "2", 3, "4", 5].filter(iteratee);
|
|
@@ -170,6 +216,8 @@ export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => Maybe
|
|
|
170
216
|
* }
|
|
171
217
|
* ```
|
|
172
218
|
*
|
|
219
|
+
* ---
|
|
220
|
+
*
|
|
173
221
|
* @template T The type of the elements in the iterable.
|
|
174
222
|
* @template R
|
|
175
223
|
* The type of the elements that pass the type guard.
|
|
@@ -186,6 +234,9 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
|
|
|
186
234
|
* An utility type that represents a reducer function.
|
|
187
235
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
188
236
|
*
|
|
237
|
+
* ---
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
189
240
|
* ```ts
|
|
190
241
|
* const sum: Reducer<number, number> = (accumulator, value) => accumulator + value;
|
|
191
242
|
* const total: number = [1, 2, 3, 4, 5].reduce(sum);
|
|
@@ -193,6 +244,8 @@ export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => va
|
|
|
193
244
|
* console.log(total); // 15
|
|
194
245
|
* ```
|
|
195
246
|
*
|
|
247
|
+
* ---
|
|
248
|
+
*
|
|
196
249
|
* @template T The type of the elements in the iterable.
|
|
197
250
|
* @template A The type of the accumulator.
|
|
198
251
|
*/
|
|
@@ -202,6 +255,9 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
|
|
|
202
255
|
* An utility type that represents an asynchronous reducer function.
|
|
203
256
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
204
257
|
*
|
|
258
|
+
* ---
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
205
261
|
* ```ts
|
|
206
262
|
* const sum: AsyncReducer<number, number> = async (accumulator, value) => accumulator + value;
|
|
207
263
|
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
@@ -209,6 +265,8 @@ export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
|
|
|
209
265
|
* console.log(result); // 15
|
|
210
266
|
* ```
|
|
211
267
|
*
|
|
268
|
+
* ---
|
|
269
|
+
*
|
|
212
270
|
* @template T The type of the elements in the iterable.
|
|
213
271
|
* @template A The type of the accumulator.
|
|
214
272
|
*/
|
|
@@ -218,6 +276,9 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
|
|
|
218
276
|
* An utility type that represents a reducer function that can be either synchronous or asynchronous.
|
|
219
277
|
* It can be used to reduce the elements of an iterable into a single value.
|
|
220
278
|
*
|
|
279
|
+
* ---
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
221
282
|
* ```ts
|
|
222
283
|
* const sum: MaybeAsyncReducer<number, number> = [async] (accumulator, value) => accumulator + value;
|
|
223
284
|
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
@@ -225,6 +286,8 @@ export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Pr
|
|
|
225
286
|
* console.log(result); // 15
|
|
226
287
|
* ```
|
|
227
288
|
*
|
|
289
|
+
* ---
|
|
290
|
+
*
|
|
228
291
|
* @template T The type of the elements in the iterable.
|
|
229
292
|
* @template A The type of the accumulator.
|
|
230
293
|
*/
|
|
@@ -234,6 +297,9 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
|
|
|
234
297
|
* An union type that represents either an iterable or an iterator object.
|
|
235
298
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
236
299
|
*
|
|
300
|
+
* ---
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
237
303
|
* ```ts
|
|
238
304
|
* const elements: IteratorLike<number> = { ... };
|
|
239
305
|
* const iterator: SmartIterator<number> = new SmartIterator(elements);
|
|
@@ -243,6 +309,8 @@ export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number)
|
|
|
243
309
|
* }
|
|
244
310
|
* ```
|
|
245
311
|
*
|
|
312
|
+
* ---
|
|
313
|
+
*
|
|
246
314
|
* @template T The type of the elements in the iterable.
|
|
247
315
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
248
316
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -253,6 +321,9 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
|
|
|
253
321
|
* An union type that represents either an iterable or an iterator object that can be asynchronous.
|
|
254
322
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
255
323
|
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
256
327
|
* ```ts
|
|
257
328
|
* const elements: AsyncIteratorLike<number> = { ... };
|
|
258
329
|
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
@@ -262,6 +333,8 @@ export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Itera
|
|
|
262
333
|
* }
|
|
263
334
|
* ```
|
|
264
335
|
*
|
|
336
|
+
* ---
|
|
337
|
+
*
|
|
265
338
|
* @template T The type of the elements in the iterable.
|
|
266
339
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
267
340
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -273,6 +346,9 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
|
|
|
273
346
|
* object that can be either synchronous or asynchronous.
|
|
274
347
|
* More in general, it represents an object that can be looped over in one way or another.
|
|
275
348
|
*
|
|
349
|
+
* ---
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
276
352
|
* ```ts
|
|
277
353
|
* const elements: MaybeAsyncIteratorLike<number> = { ... };
|
|
278
354
|
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
@@ -282,6 +358,8 @@ export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R,
|
|
|
282
358
|
* }
|
|
283
359
|
* ```
|
|
284
360
|
*
|
|
361
|
+
* ---
|
|
362
|
+
*
|
|
285
363
|
* @template T The type of the elements in the iterable.
|
|
286
364
|
* @template R The type of the return value of the iterator. Default is `void`.
|
|
287
365
|
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
@@ -4,12 +4,15 @@ import { EnvironmentException } from "../exceptions/index.js";
|
|
|
4
4
|
import type { JSONValue } from "./types.js";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* A wrapper around the
|
|
7
|
+
* A wrapper around the {@link Storage} API to better store and easily retrieve
|
|
8
8
|
* typed JSON values using the classical key-value pair storage system.
|
|
9
9
|
*
|
|
10
10
|
* It allows to handle either the volatile {@link sessionStorage} or the persistent
|
|
11
11
|
* {@link localStorage} at the same time, depending on what's your required use case.
|
|
12
12
|
*
|
|
13
|
+
* ---
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
13
16
|
* ```ts
|
|
14
17
|
* const jsonStorage = new JSONStorage();
|
|
15
18
|
*
|
package/src/models/json/types.ts
CHANGED
|
@@ -6,7 +6,7 @@ export type JSONArray = JSONValue[];
|
|
|
6
6
|
/**
|
|
7
7
|
* A type that represents a JSON object.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
9
|
+
export type JSONObject<T extends object = object> = { [K in keyof T]: JSONValue };
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A type that represents all the possible values of a JSON value.
|
|
@@ -12,6 +12,9 @@ import SmartPromise from "./smart-promise.js";
|
|
|
12
12
|
* This is a change in the approach to promises: instead of defining how the promise will be resolved (or rejected),
|
|
13
13
|
* you define how to handle the resolution (or rejection) when it occurs.
|
|
14
14
|
*
|
|
15
|
+
* ---
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
15
18
|
* ```ts
|
|
16
19
|
* const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
17
20
|
*
|
|
@@ -19,6 +22,8 @@ import SmartPromise from "./smart-promise.js";
|
|
|
19
22
|
* promise.resolve("Hello, World!");
|
|
20
23
|
* ```
|
|
21
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
22
27
|
* @template T The type of value the promise expects to initially be resolved with. Default is `void`.
|
|
23
28
|
* @template F
|
|
24
29
|
* The type of value returned by the `onFulfilled` callback.
|
|
@@ -7,6 +7,9 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
|
|
|
7
7
|
* The state can be either `pending`, `fulfilled` or `rejected` and is accessible through
|
|
8
8
|
* the {@link SmartPromise.isPending}, {@link SmartPromise.isFulfilled} & {@link SmartPromise.isRejected} properties.
|
|
9
9
|
*
|
|
10
|
+
* ---
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
10
13
|
* ```ts
|
|
11
14
|
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
12
15
|
* {
|
|
@@ -22,6 +25,8 @@ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types
|
|
|
22
25
|
* console.log(promise.isFulfilled); // true
|
|
23
26
|
* ```
|
|
24
27
|
*
|
|
28
|
+
* ---
|
|
29
|
+
*
|
|
25
30
|
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
26
31
|
*/
|
|
27
32
|
export default class SmartPromise<T = void> implements Promise<T>
|
|
@@ -9,6 +9,9 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
|
9
9
|
*
|
|
10
10
|
* If the operation takes longer than the specified time, the promise is rejected with a {@link TimeoutException}.
|
|
11
11
|
*
|
|
12
|
+
* ---
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
12
15
|
* ```ts
|
|
13
16
|
* const promise = new TimedPromise<string>((resolve, reject) =>
|
|
14
17
|
* {
|
|
@@ -21,6 +24,8 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
|
|
|
21
24
|
* .catch((error) => console.error(error)); // TimeoutException: The operation has timed out.
|
|
22
25
|
* ```
|
|
23
26
|
*
|
|
27
|
+
* ---
|
|
28
|
+
*
|
|
24
29
|
* @template T The type of value the promise will eventually resolve to. Default is `void`.
|
|
25
30
|
*/
|
|
26
31
|
export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* An union type that represents a value that can be either a value or a promise of that value.
|
|
3
3
|
* This is useful when you want to handle both synchronous and asynchronous values in the same way.
|
|
4
4
|
*
|
|
5
|
+
* ---
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
5
8
|
* ```ts
|
|
6
9
|
* async function splitWords(value: MaybePromise<string>): Promise<string[]>
|
|
7
10
|
* {
|
|
@@ -9,6 +12,8 @@
|
|
|
9
12
|
* }
|
|
10
13
|
* ```
|
|
11
14
|
*
|
|
15
|
+
* ---
|
|
16
|
+
*
|
|
12
17
|
* @template T The type of the value.
|
|
13
18
|
*/
|
|
14
19
|
export type MaybePromise<T> = T | PromiseLike<T>;
|
|
@@ -17,6 +22,9 @@ export type MaybePromise<T> = T | PromiseLike<T>;
|
|
|
17
22
|
* An utility type that represents the callback that is executed when a promise is fulfilled.
|
|
18
23
|
* It's compatible with the `onFulfilled` parameter of the `then` method of the native {@link Promise} object.
|
|
19
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
20
28
|
* ```ts
|
|
21
29
|
* const onFulfilled: FulfilledHandler<string, string[]> = (value) => value.split(" ");
|
|
22
30
|
*
|
|
@@ -24,6 +32,8 @@ export type MaybePromise<T> = T | PromiseLike<T>;
|
|
|
24
32
|
* .then(onFulfilled);
|
|
25
33
|
* ```
|
|
26
34
|
*
|
|
35
|
+
* ---
|
|
36
|
+
*
|
|
27
37
|
* @template T The type of value accepted by the function. Default is `void`.
|
|
28
38
|
* @template R The type of value returned by the function. Default is `T`.
|
|
29
39
|
*/
|
|
@@ -33,6 +43,9 @@ export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
|
33
43
|
* An utility type that represents the callback that is executed when a promise is rejected.
|
|
34
44
|
* It's compatible with the `onRejected` parameter of the `then`/`catch` methods of the native {@link Promise} object.
|
|
35
45
|
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
36
49
|
* ```ts
|
|
37
50
|
* const onRejected: RejectedHandler<unknown, string> = (reason) => String(reason);
|
|
38
51
|
*
|
|
@@ -40,6 +53,8 @@ export type FulfilledHandler<T = void, R = T> = (value: T) => MaybePromise<R>;
|
|
|
40
53
|
* .catch(onRejected);
|
|
41
54
|
* ```
|
|
42
55
|
*
|
|
56
|
+
* ---
|
|
57
|
+
*
|
|
43
58
|
* @template E The type of value accepted by the function. Default is `unknown`.
|
|
44
59
|
* @template R The type of value returned by the function. Default is `never`.
|
|
45
60
|
*/
|
|
@@ -49,12 +64,17 @@ export type RejectedHandler<E = unknown, R = never> = (reason: E) => MaybePromis
|
|
|
49
64
|
* An utility type that represents a function that can be used to resolve a promise.
|
|
50
65
|
* It's compatible with the `resolve` parameter of the native {@link Promise} executor.
|
|
51
66
|
*
|
|
67
|
+
* ---
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
52
70
|
* ```ts
|
|
53
71
|
* let _resolve: PromiseResolver<string> = (result) => console.log(result);
|
|
54
72
|
*
|
|
55
73
|
* await new Promise<string>((resolve) => { _resolve = resolve; });
|
|
56
74
|
* ```
|
|
57
75
|
*
|
|
76
|
+
* ---
|
|
77
|
+
*
|
|
58
78
|
* @template T The type of the value accepted by the function. Default is `void`.
|
|
59
79
|
*/
|
|
60
80
|
export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
@@ -63,12 +83,17 @@ export type PromiseResolver<T = void> = (result: MaybePromise<T>) => void;
|
|
|
63
83
|
* An utility type that represents a function that can be used to reject a promise.
|
|
64
84
|
* It's compatible with the `reject` parameter of the native {@link Promise} executor.
|
|
65
85
|
*
|
|
86
|
+
* ---
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
66
89
|
* ```ts
|
|
67
90
|
* let _reject: PromiseRejecter<string> = (reason) => console.error(reason);
|
|
68
91
|
*
|
|
69
92
|
* await new Promise<string>((_, reject) => { _reject = reject; });
|
|
70
93
|
* ```
|
|
71
94
|
*
|
|
95
|
+
* ---
|
|
96
|
+
*
|
|
72
97
|
* @template E The type of the value accepted by the function. Default is `unknown`.
|
|
73
98
|
*/
|
|
74
99
|
export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
@@ -77,6 +102,9 @@ export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
|
77
102
|
* An utility type that represents the function that will be executed by the promise.
|
|
78
103
|
* It's compatible with the `executor` parameter of the native {@link Promise} object.
|
|
79
104
|
*
|
|
105
|
+
* ---
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
80
108
|
* ```ts
|
|
81
109
|
* const executor: PromiseExecutor<string> = (resolve, reject) =>
|
|
82
110
|
* {
|
|
@@ -86,6 +114,8 @@ export type PromiseRejecter<E = unknown> = (reason?: MaybePromise<E>) => void;
|
|
|
86
114
|
* await new Promise<string>(executor);
|
|
87
115
|
* ```
|
|
88
116
|
*
|
|
117
|
+
* ---
|
|
118
|
+
*
|
|
89
119
|
* @template T The type of value accepted by the `resolve` function. Default is `void`.
|
|
90
120
|
* @template E The type of value accepted by the `reject` function. Default is `unknown`.
|
|
91
121
|
*/
|
|
@@ -22,6 +22,9 @@ interface ClockEventMap
|
|
|
22
22
|
* It can be started, stopped and, when running, it ticks at a specific frame rate.
|
|
23
23
|
* It's possible to subscribe to these events to receive notifications when they occur.
|
|
24
24
|
*
|
|
25
|
+
* ---
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
25
28
|
* ```ts
|
|
26
29
|
* const clock = new Clock();
|
|
27
30
|
*
|
|
@@ -24,6 +24,9 @@ interface CountdownEventMap
|
|
|
24
24
|
* It can be started, stopped, when running it ticks at a specific frame rate and it expires when the time's up.
|
|
25
25
|
* It's possible to subscribe to these events to receive notifications when they occur.
|
|
26
26
|
*
|
|
27
|
+
* ---
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
27
30
|
* ```ts
|
|
28
31
|
* const countdown = new Countdown(10_000);
|
|
29
32
|
*
|
|
@@ -119,6 +122,8 @@ export default class Countdown extends GameLoop
|
|
|
119
122
|
* The internal method actually responsible for stopping the
|
|
120
123
|
* countdown and resolving or rejecting the {@link Countdown._deferrer} promise.
|
|
121
124
|
*
|
|
125
|
+
* ---
|
|
126
|
+
*
|
|
122
127
|
* @param reason
|
|
123
128
|
* The reason why the countdown has stopped.
|
|
124
129
|
*
|
|
@@ -27,6 +27,9 @@ interface GameLoopEventMap
|
|
|
27
27
|
* elapsed time since the start of the game loop.
|
|
28
28
|
* It's also possible to subscribe to the `start` & `stop` events to receive notifications when they occur.
|
|
29
29
|
*
|
|
30
|
+
* ---
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
30
33
|
* ```ts
|
|
31
34
|
* const loop = new GameLoop((elapsedTime: number) =>
|
|
32
35
|
* {
|
package/src/models/types.ts
CHANGED
|
@@ -9,6 +9,7 @@ export type {
|
|
|
9
9
|
|
|
10
10
|
} from "./aggregators/types.js";
|
|
11
11
|
|
|
12
|
+
export type { MapViewEventsMap, ReadonlyMapView, SetViewEventsMap, ReadonlySetView } from "./collections/types.js";
|
|
12
13
|
export type {
|
|
13
14
|
GeneratorFunction,
|
|
14
15
|
AsyncGeneratorFunction,
|
|
@@ -26,13 +27,7 @@ export type {
|
|
|
26
27
|
|
|
27
28
|
} from "./iterators/types.js";
|
|
28
29
|
|
|
29
|
-
export type {
|
|
30
|
-
JSONArray,
|
|
31
|
-
JSONObject,
|
|
32
|
-
JSONValue
|
|
33
|
-
|
|
34
|
-
} from "./json/types.js";
|
|
35
|
-
|
|
30
|
+
export type { JSONArray, JSONObject, JSONValue } from "./json/types.js";
|
|
36
31
|
export type {
|
|
37
32
|
MaybePromise,
|
|
38
33
|
FulfilledHandler,
|
|
@@ -43,4 +38,4 @@ export type {
|
|
|
43
38
|
|
|
44
39
|
} from "./promises/types.js";
|
|
45
40
|
|
|
46
|
-
export type { Callback } from "./callbacks/types.js";
|
|
41
|
+
export type { Callback, CallbackMap } from "./callbacks/types.js";
|
package/src/utils/async.ts
CHANGED
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
* Returns a promise that resolves after a certain number of milliseconds.
|
|
3
3
|
* It can be used to pause or delay the execution of an asynchronous function.
|
|
4
4
|
*
|
|
5
|
+
* ---
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
5
8
|
* ```ts
|
|
6
9
|
* doSomething();
|
|
7
10
|
* await delay(1_000);
|
|
8
11
|
* doSomethingElse();
|
|
9
12
|
* ```
|
|
10
13
|
*
|
|
14
|
+
* ---
|
|
15
|
+
*
|
|
11
16
|
* @param milliseconds The number of milliseconds to wait before resolving the promise.
|
|
12
17
|
*
|
|
13
18
|
* @returns A {@link Promise} that resolves after the specified number of milliseconds.
|
|
@@ -21,6 +26,9 @@ export function delay(milliseconds: number): Promise<void>
|
|
|
21
26
|
* Returns a promise that resolves on the next animation frame.
|
|
22
27
|
* It can be used to synchronize operations with the browser's rendering cycle.
|
|
23
28
|
*
|
|
29
|
+
* ---
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
24
32
|
* ```ts
|
|
25
33
|
* const $el = document.querySelector(".element");
|
|
26
34
|
*
|
|
@@ -29,6 +37,8 @@ export function delay(milliseconds: number): Promise<void>
|
|
|
29
37
|
* $el.style.opacity = "1";
|
|
30
38
|
* ```
|
|
31
39
|
*
|
|
40
|
+
* ---
|
|
41
|
+
*
|
|
32
42
|
* @returns A {@link Promise} that resolves on the next animation frame.
|
|
33
43
|
*/
|
|
34
44
|
export function nextAnimationFrame(): Promise<void>
|
|
@@ -40,6 +50,9 @@ export function nextAnimationFrame(): Promise<void>
|
|
|
40
50
|
* Returns a promise that resolves on the next microtask.
|
|
41
51
|
* It can be used to yield to the event loop in long-running operations to prevent blocking the main thread.
|
|
42
52
|
*
|
|
53
|
+
* ---
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
43
56
|
* ```ts
|
|
44
57
|
* for (let i = 0; i < 100_000_000; i += 1)
|
|
45
58
|
* {
|
|
@@ -49,6 +62,8 @@ export function nextAnimationFrame(): Promise<void>
|
|
|
49
62
|
* }
|
|
50
63
|
* ```
|
|
51
64
|
*
|
|
65
|
+
* ---
|
|
66
|
+
*
|
|
52
67
|
* @returns A {@link Promise} that resolves on the next microtask.
|
|
53
68
|
*/
|
|
54
69
|
export function yieldToEventLoop(): Promise<void>
|
package/src/utils/curve.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SmartIterator, ValueException } from "../models/index.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* An utility class that provides a set of methods to generate sequences of numbers following specific curves.
|
|
5
5
|
* It can be used to generate sequences of values that can be
|
|
6
6
|
* used in animations, transitions and other different scenarios.
|
|
7
7
|
*
|