@byloth/core 2.0.0-rc.9 → 2.0.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/core.js +3371 -608
- 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 +13 -10
- 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 +765 -21
- package/src/models/aggregators/aggregated-iterator.ts +698 -22
- package/src/models/aggregators/reduced-iterator.ts +699 -10
- 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 +139 -4
- package/src/models/callbacks/switchable-callback.ts +138 -4
- package/src/models/callbacks/types.ts +16 -0
- package/src/models/exceptions/core.ts +112 -3
- package/src/models/exceptions/index.ts +340 -13
- package/src/models/index.ts +4 -8
- package/src/models/iterators/smart-async-iterator.ts +687 -22
- package/src/models/iterators/smart-iterator.ts +631 -21
- package/src/models/iterators/types.ts +268 -9
- package/src/models/json/json-storage.ts +388 -110
- package/src/models/json/types.ts +10 -1
- package/src/models/promises/deferred-promise.ts +75 -5
- package/src/models/promises/index.ts +1 -3
- package/src/models/promises/smart-promise.ts +232 -4
- package/src/models/promises/timed-promise.ts +38 -1
- package/src/models/promises/types.ts +84 -2
- package/src/models/timers/clock.ts +91 -19
- package/src/models/timers/countdown.ts +152 -22
- package/src/models/timers/game-loop.ts +243 -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 +75 -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 +109 -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
|
@@ -1,30 +1,289 @@
|
|
|
1
|
-
|
|
2
1
|
import type { MaybePromise } from "../promises/types.js";
|
|
3
2
|
|
|
3
|
+
/**
|
|
4
|
+
* An union type that represents an iterable object that can be either synchronous or asynchronous.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const iterable: MaybeAsyncIterable<number> = [...];
|
|
8
|
+
* for await (const value of iterable)
|
|
9
|
+
* {
|
|
10
|
+
* console.log(value);
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @template T The type of the elements in the iterable.
|
|
15
|
+
*/
|
|
4
16
|
export type MaybeAsyncIterable<T, R = void, N = undefined> = Iterable<T, R, N> | AsyncIterable<T, R, N>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* An union type that represents an iterator object that can be either synchronous or asynchronous.
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const iterator: MaybeAsyncIterator<number> = { ... };
|
|
23
|
+
* for await (const value of iterator)
|
|
24
|
+
* {
|
|
25
|
+
* console.log(value);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @template T The type of the elements in the iterator.
|
|
30
|
+
*/
|
|
5
31
|
export type MaybeAsyncIterator<T, R = void, N = undefined> = Iterator<T, R, N> | AsyncIterator<T, R, N>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An union type that represents a generator object that can be either synchronous or asynchronous.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const generator: MaybeAsyncGenerator<number> = [async] function*() { ... };
|
|
38
|
+
* for await (const value of generator)
|
|
39
|
+
* {
|
|
40
|
+
* console.log(value);
|
|
41
|
+
* }
|
|
42
|
+
*/
|
|
6
43
|
export type MaybeAsyncGenerator<T, R = void, N = undefined> = Generator<T, R, N> | AsyncGenerator<T, R, N>;
|
|
7
44
|
|
|
45
|
+
/**
|
|
46
|
+
* An utility type that represents a function that returns a generator object.
|
|
47
|
+
* It differs from the native `GeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* const generatorFn: GeneratorFunction<number> = function*() { ... };
|
|
51
|
+
* const generator: Generator<number> = generatorFn();
|
|
52
|
+
*
|
|
53
|
+
* for (const value of generator)
|
|
54
|
+
* {
|
|
55
|
+
* console.log(value);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @template T The type of the elements generated by the generator.
|
|
60
|
+
* @template R The type of the return value of the generator. Default is `void`.
|
|
61
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
62
|
+
*/
|
|
8
63
|
export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* An utility type that represents a function that returns an asynchronous generator object.
|
|
67
|
+
* It differs from the native `AsyncGeneratorFunction` type by allowing to specify the types of the returned generator.
|
|
68
|
+
*
|
|
69
|
+
* ```ts
|
|
70
|
+
* const asyncGeneratorFn: AsyncGeneratorFunction<number> = async function*() { ... };
|
|
71
|
+
* const generator: AsyncGenerator<number> = asyncGeneratorFn();
|
|
72
|
+
* for await (const value of generator)
|
|
73
|
+
* {
|
|
74
|
+
* console.log(value);
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @template T The type of the elements generated by the generator.
|
|
79
|
+
* @template R The type of the return value of the generator. Default is `void`.
|
|
80
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
81
|
+
*/
|
|
9
82
|
export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGenerator<T, R, N>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* An utility type that represents a function that returns a
|
|
86
|
+
* generator object that can be either synchronous or asynchronous.
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* const generatorFn: MaybeAsyncGeneratorFunction<number> = [async] function*() { ... };
|
|
90
|
+
* const generator: MaybeAsyncGenerator<number> = generatorFn();
|
|
91
|
+
* for await (const value of generator)
|
|
92
|
+
* {
|
|
93
|
+
* console.log(value);
|
|
94
|
+
* }
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @template T The type of the elements generated by the generator.
|
|
98
|
+
* @template R The type of the return value of the generator. Default is `void`.
|
|
99
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
100
|
+
*/
|
|
10
101
|
export type MaybeAsyncGeneratorFunction<T, R = void, N = undefined> = () => MaybeAsyncGenerator<T, R, N>;
|
|
11
102
|
|
|
103
|
+
/**
|
|
104
|
+
* An utility type that represents the standard JavaScript's
|
|
105
|
+
* {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
106
|
+
* It can be used to transform the elements of an iterable.
|
|
107
|
+
*
|
|
108
|
+
* ```ts
|
|
109
|
+
* const iteratee: Iteratee<number, string> = (value: number) => `${value}`;
|
|
110
|
+
* const values: string[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
111
|
+
*
|
|
112
|
+
* console.log(values); // ["1", "2", "3", "4", "5"]
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @template T The type of the elements in the iterable.
|
|
116
|
+
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
117
|
+
*/
|
|
12
118
|
export type Iteratee<T, R = void> = (value: T, index: number) => R;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* An utility type that represents an asynchronous {@link https://en.wikipedia.org/wiki/Iteratee|iteratee} function.
|
|
122
|
+
* It can be used to transform the elements of an iterable asynchronously.
|
|
123
|
+
*
|
|
124
|
+
* ```ts
|
|
125
|
+
* const iteratee: AsyncIteratee<number, string> = async (value: number) => `${value}`;
|
|
126
|
+
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
127
|
+
* for await (const value of values)
|
|
128
|
+
* {
|
|
129
|
+
* console.log(value); // "1", "2", "3", "4", "5"
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @template T The type of the elements in the iterable.
|
|
134
|
+
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
135
|
+
*/
|
|
13
136
|
export type AsyncIteratee<T, R = void> = (value: T, index: number) => Promise<R>;
|
|
14
|
-
export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => MaybePromise<R>;
|
|
15
137
|
|
|
16
|
-
|
|
138
|
+
/**
|
|
139
|
+
* An utility type that represents an {@link https://en.wikipedia.org/wiki/Iteratee|iteratee}
|
|
140
|
+
* function that can be either synchronous or asynchronous.
|
|
141
|
+
* It can be used to transform the elements of an iterable.
|
|
142
|
+
*
|
|
143
|
+
* ```ts
|
|
144
|
+
* const iteratee: MaybeAsyncIteratee<number, string> = [async] (value: number) => `${value}`;
|
|
145
|
+
* const values: Promise<string>[] = [1, 2, 3, 4, 5].map(iteratee);
|
|
146
|
+
* for await (const value of values)
|
|
147
|
+
* {
|
|
148
|
+
* console.log(value); // "1", "2", "3", "4", "5"
|
|
149
|
+
* }
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @template T The type of the elements in the iterable.
|
|
153
|
+
* @template R The type of the return value of the iteratee. Default is `void`.
|
|
154
|
+
*/
|
|
155
|
+
export type MaybeAsyncIteratee<T, R = void> = (value: T, index: number) => MaybePromise<R>;
|
|
17
156
|
|
|
18
|
-
|
|
19
|
-
|
|
157
|
+
/**
|
|
158
|
+
* An utility type that represents a {@link https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)|predicate}
|
|
159
|
+
* function which acts as a
|
|
160
|
+
* {@link https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates|type guard}.
|
|
161
|
+
* It can be used to ensure the type of the elements of an iterable
|
|
162
|
+
* while allowing the type-system to infer them correctly.
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* const iteratee: TypeGuardPredicate<number | string, string> = (value): value is string => typeof value === "string";
|
|
166
|
+
* const values: string[] = [1, "2", 3, "4", 5].filter(iteratee);
|
|
167
|
+
* for (const value of values)
|
|
168
|
+
* {
|
|
169
|
+
* console.log(value); // "2", "4"
|
|
170
|
+
* }
|
|
171
|
+
* ```
|
|
172
|
+
*
|
|
173
|
+
* @template T The type of the elements in the iterable.
|
|
174
|
+
* @template R
|
|
175
|
+
* The type of the elements that pass the type guard.
|
|
176
|
+
* It must be a subtype of `T`. Default is `T`.
|
|
177
|
+
*/
|
|
178
|
+
export type TypeGuardPredicate<T, R extends T> = (value: T, index: number) => value is R;
|
|
20
179
|
|
|
21
|
-
//
|
|
22
|
-
|
|
180
|
+
// These types need this Issue to be solved: https://github.com/microsoft/TypeScript/issues/37681
|
|
181
|
+
//
|
|
182
|
+
// export type AsyncTypeGuardPredicate<T, R extends T> = (value: T, index: number) => value is Promise<R>;
|
|
183
|
+
// export type MaybeAsyncTypeGuardPredicate<T, R extends T> = (value: T, index: number) => value is MaybePromise<R>;
|
|
23
184
|
|
|
185
|
+
/**
|
|
186
|
+
* An utility type that represents a reducer function.
|
|
187
|
+
* It can be used to reduce the elements of an iterable into a single value.
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* const sum: Reducer<number, number> = (accumulator, value) => accumulator + value;
|
|
191
|
+
* const total: number = [1, 2, 3, 4, 5].reduce(sum);
|
|
192
|
+
*
|
|
193
|
+
* console.log(total); // 15
|
|
194
|
+
* ```
|
|
195
|
+
*
|
|
196
|
+
* @template T The type of the elements in the iterable.
|
|
197
|
+
* @template A The type of the accumulator.
|
|
198
|
+
*/
|
|
24
199
|
export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* An utility type that represents an asynchronous reducer function.
|
|
203
|
+
* It can be used to reduce the elements of an iterable into a single value.
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* const sum: AsyncReducer<number, number> = async (accumulator, value) => accumulator + value;
|
|
207
|
+
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
208
|
+
*
|
|
209
|
+
* console.log(result); // 15
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @template T The type of the elements in the iterable.
|
|
213
|
+
* @template A The type of the accumulator.
|
|
214
|
+
*/
|
|
25
215
|
export type AsyncReducer<T, A> = (accumulator: A, value: T, index: number) => Promise<A>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* An utility type that represents a reducer function that can be either synchronous or asynchronous.
|
|
219
|
+
* It can be used to reduce the elements of an iterable into a single value.
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
* const sum: MaybeAsyncReducer<number, number> = [async] (accumulator, value) => accumulator + value;
|
|
223
|
+
* const result = await new SmartAsyncIterator<number>([1, 2, 3, 4, 5]).reduce(sum);
|
|
224
|
+
*
|
|
225
|
+
* console.log(result); // 15
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @template T The type of the elements in the iterable.
|
|
229
|
+
* @template A The type of the accumulator.
|
|
230
|
+
*/
|
|
26
231
|
export type MaybeAsyncReducer<T, A> = (accumulator: A, value: T, index: number) => MaybePromise<A>;
|
|
27
232
|
|
|
28
|
-
|
|
29
|
-
|
|
233
|
+
/**
|
|
234
|
+
* An union type that represents either an iterable or an iterator object.
|
|
235
|
+
* More in general, it represents an object that can be looped over in one way or another.
|
|
236
|
+
*
|
|
237
|
+
* ```ts
|
|
238
|
+
* const elements: IteratorLike<number> = { ... };
|
|
239
|
+
* const iterator: SmartIterator<number> = new SmartIterator(elements);
|
|
240
|
+
* for (const value of iterator)
|
|
241
|
+
* {
|
|
242
|
+
* console.log(value);
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
245
|
+
*
|
|
246
|
+
* @template T The type of the elements in the iterable.
|
|
247
|
+
* @template R The type of the return value of the iterator. Default is `void`.
|
|
248
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
249
|
+
*/
|
|
250
|
+
export type IteratorLike<T, R = void, N = undefined> = Iterable<T, R, N> | Iterator<T, R, N>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* An union type that represents either an iterable or an iterator object that can be asynchronous.
|
|
254
|
+
* More in general, it represents an object that can be looped over in one way or another.
|
|
255
|
+
*
|
|
256
|
+
* ```ts
|
|
257
|
+
* const elements: AsyncIteratorLike<number> = { ... };
|
|
258
|
+
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
259
|
+
* for await (const value of iterator)
|
|
260
|
+
* {
|
|
261
|
+
* console.log(value);
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
264
|
+
*
|
|
265
|
+
* @template T The type of the elements in the iterable.
|
|
266
|
+
* @template R The type of the return value of the iterator. Default is `void`.
|
|
267
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
268
|
+
*/
|
|
269
|
+
export type AsyncIteratorLike<T, R = void, N = undefined> = AsyncIterable<T, R, N> | AsyncIterator<T, R, N>;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* An union type that represents either an iterable or an iterator
|
|
273
|
+
* object that can be either synchronous or asynchronous.
|
|
274
|
+
* More in general, it represents an object that can be looped over in one way or another.
|
|
275
|
+
*
|
|
276
|
+
* ```ts
|
|
277
|
+
* const elements: MaybeAsyncIteratorLike<number> = { ... };
|
|
278
|
+
* const iterator: SmartAsyncIterator<number> = new SmartAsyncIterator(elements);
|
|
279
|
+
* for await (const value of iterator)
|
|
280
|
+
* {
|
|
281
|
+
* console.log(value);
|
|
282
|
+
* }
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* @template T The type of the elements in the iterable.
|
|
286
|
+
* @template R The type of the return value of the iterator. Default is `void`.
|
|
287
|
+
* @template N The type of the `next` method argument. Default is `undefined`.
|
|
288
|
+
*/
|
|
30
289
|
export type MaybeAsyncIteratorLike<T, R = void, N = undefined> = IteratorLike<T, R, N> | AsyncIteratorLike<T, R, N>;
|