@byloth/core 2.0.0 → 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 +885 -182
- 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 +10 -9
- package/src/index.ts +1 -1
- package/src/models/aggregators/aggregated-async-iterator.ts +156 -1
- package/src/models/aggregators/aggregated-iterator.ts +141 -1
- package/src/models/aggregators/reduced-iterator.ts +130 -3
- package/src/models/callbacks/publisher.ts +21 -0
- package/src/models/callbacks/switchable-callback.ts +94 -21
- package/src/models/exceptions/core.ts +20 -0
- package/src/models/exceptions/index.ts +65 -0
- package/src/models/iterators/smart-async-iterator.ts +140 -0
- package/src/models/iterators/smart-iterator.ts +125 -0
- package/src/models/json/json-storage.ts +120 -0
- package/src/models/promises/deferred-promise.ts +10 -0
- package/src/models/promises/smart-promise.ts +40 -0
- package/src/models/promises/timed-promise.ts +5 -0
- package/src/models/timers/clock.ts +18 -0
- package/src/models/timers/countdown.ts +25 -0
- package/src/models/timers/game-loop.ts +23 -0
- package/src/utils/curve.ts +10 -0
- package/src/utils/random.ts +30 -0
|
@@ -42,10 +42,15 @@ export default class JSONStorage
|
|
|
42
42
|
* Initializes a new instance of the {@link JSONStorage} class.
|
|
43
43
|
* It cannot be instantiated outside of a browser environment or an {@link EnvironmentException} is thrown.
|
|
44
44
|
*
|
|
45
|
+
* ---
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
45
48
|
* ```ts
|
|
46
49
|
* const jsonStorage = new JSONStorage();
|
|
47
50
|
* ```
|
|
48
51
|
*
|
|
52
|
+
* ---
|
|
53
|
+
*
|
|
49
54
|
* @param preferPersistence
|
|
50
55
|
* Whether to prefer the {@link localStorage} over the {@link sessionStorage} when calling an ambivalent method.
|
|
51
56
|
* If omitted, it defaults to `true` to prefer the persistent storage.
|
|
@@ -106,10 +111,15 @@ export default class JSONStorage
|
|
|
106
111
|
/**
|
|
107
112
|
* Retrieves the value with the specified key from the default storage.
|
|
108
113
|
*
|
|
114
|
+
* ---
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
109
117
|
* ```ts
|
|
110
118
|
* const value: TValue = jsonStorage.get<TValue>("key");
|
|
111
119
|
* ```
|
|
112
120
|
*
|
|
121
|
+
* ---
|
|
122
|
+
*
|
|
113
123
|
* @template T The type of the value to retrieve.
|
|
114
124
|
*
|
|
115
125
|
* @param key The key of the value to retrieve.
|
|
@@ -121,10 +131,15 @@ export default class JSONStorage
|
|
|
121
131
|
/**
|
|
122
132
|
* Retrieves the value with the specified key from the default storage.
|
|
123
133
|
*
|
|
134
|
+
* ---
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
124
137
|
* ```ts
|
|
125
138
|
* const value: TValue = jsonStorage.get<TValue>("key", defaultValue);
|
|
126
139
|
* ```
|
|
127
140
|
*
|
|
141
|
+
* ---
|
|
142
|
+
*
|
|
128
143
|
* @template T The type of the value to retrieve.
|
|
129
144
|
*
|
|
130
145
|
* @param key The key of the value to retrieve.
|
|
@@ -140,10 +155,15 @@ export default class JSONStorage
|
|
|
140
155
|
/**
|
|
141
156
|
* Retrieves the value with the specified key from the default storage.
|
|
142
157
|
*
|
|
158
|
+
* ---
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
143
161
|
* ```ts
|
|
144
162
|
* const value: TValue = jsonStorage.get<TValue>("key", obj?.value);
|
|
145
163
|
* ```
|
|
146
164
|
*
|
|
165
|
+
* ---
|
|
166
|
+
*
|
|
147
167
|
* @template T The type of the value to retrieve.
|
|
148
168
|
*
|
|
149
169
|
* @param key The key of the value to retrieve.
|
|
@@ -166,10 +186,15 @@ export default class JSONStorage
|
|
|
166
186
|
/**
|
|
167
187
|
* Retrieves the value with the specified key from the volatile {@link sessionStorage}.
|
|
168
188
|
*
|
|
189
|
+
* ---
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
169
192
|
* ```ts
|
|
170
193
|
* const value: TValue = jsonStorage.recall<TValue>("key");
|
|
171
194
|
* ```
|
|
172
195
|
*
|
|
196
|
+
* ---
|
|
197
|
+
*
|
|
173
198
|
* @template T The type of the value to retrieve.
|
|
174
199
|
*
|
|
175
200
|
* @param key The key of the value to retrieve.
|
|
@@ -181,10 +206,15 @@ export default class JSONStorage
|
|
|
181
206
|
/**
|
|
182
207
|
* Retrieves the value with the specified key from the volatile {@link sessionStorage}.
|
|
183
208
|
*
|
|
209
|
+
* ---
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
184
212
|
* ```ts
|
|
185
213
|
* const value: TValue = jsonStorage.recall<TValue>("key", defaultValue);
|
|
186
214
|
* ```
|
|
187
215
|
*
|
|
216
|
+
* ---
|
|
217
|
+
*
|
|
188
218
|
* @template T The type of the value to retrieve.
|
|
189
219
|
*
|
|
190
220
|
* @param key The key of the value to retrieve.
|
|
@@ -197,10 +227,15 @@ export default class JSONStorage
|
|
|
197
227
|
/**
|
|
198
228
|
* Retrieves the value with the specified key from the volatile {@link sessionStorage}.
|
|
199
229
|
*
|
|
230
|
+
* ---
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
200
233
|
* ```ts
|
|
201
234
|
* const value: TValue = jsonStorage.recall<TValue>("key", obj?.value);
|
|
202
235
|
* ```
|
|
203
236
|
*
|
|
237
|
+
* ---
|
|
238
|
+
*
|
|
204
239
|
* @template T The type of the value to retrieve.
|
|
205
240
|
*
|
|
206
241
|
* @param key The key of the value to retrieve.
|
|
@@ -218,10 +253,15 @@ export default class JSONStorage
|
|
|
218
253
|
* Retrieves the value with the specified key looking first in the volatile
|
|
219
254
|
* {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
|
|
220
255
|
*
|
|
256
|
+
* ---
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
221
259
|
* ```ts
|
|
222
260
|
* const value: TValue = jsonStorage.retrieve<TValue>("key");
|
|
223
261
|
* ```
|
|
224
262
|
*
|
|
263
|
+
* ---
|
|
264
|
+
*
|
|
225
265
|
* @template T The type of the value to retrieve.
|
|
226
266
|
*
|
|
227
267
|
* @param key The key of the value to retrieve.
|
|
@@ -234,10 +274,15 @@ export default class JSONStorage
|
|
|
234
274
|
* Retrieves the value with the specified key looking first in the volatile
|
|
235
275
|
* {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
|
|
236
276
|
*
|
|
277
|
+
* ---
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
237
280
|
* ```ts
|
|
238
281
|
* const value: TValue = jsonStorage.retrieve<TValue>("key", defaultValue);
|
|
239
282
|
* ```
|
|
240
283
|
*
|
|
284
|
+
* ---
|
|
285
|
+
*
|
|
241
286
|
* @template T The type of the value to retrieve.
|
|
242
287
|
*
|
|
243
288
|
* @param key The key of the value to retrieve.
|
|
@@ -251,10 +296,15 @@ export default class JSONStorage
|
|
|
251
296
|
* Retrieves the value with the specified key looking first in the volatile
|
|
252
297
|
* {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
|
|
253
298
|
*
|
|
299
|
+
* ---
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
254
302
|
* ```ts
|
|
255
303
|
* const value: TValue = jsonStorage.retrieve<TValue>("key", obj?.value);
|
|
256
304
|
* ```
|
|
257
305
|
*
|
|
306
|
+
* ---
|
|
307
|
+
*
|
|
258
308
|
* @template T The type of the value to retrieve.
|
|
259
309
|
*
|
|
260
310
|
* @param key The key of the value to retrieve.
|
|
@@ -271,10 +321,15 @@ export default class JSONStorage
|
|
|
271
321
|
/**
|
|
272
322
|
* Retrieves the value with the specified key from the persistent {@link localStorage}.
|
|
273
323
|
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
274
327
|
* ```ts
|
|
275
328
|
* const value: TValue = jsonStorage.read<TValue>("key");
|
|
276
329
|
* ```
|
|
277
330
|
*
|
|
331
|
+
* ---
|
|
332
|
+
*
|
|
278
333
|
* @template T The type of the value to retrieve.
|
|
279
334
|
*
|
|
280
335
|
* @param key The key of the value to retrieve.
|
|
@@ -286,10 +341,15 @@ export default class JSONStorage
|
|
|
286
341
|
/**
|
|
287
342
|
* Retrieves the value with the specified key from the persistent {@link localStorage}.
|
|
288
343
|
*
|
|
344
|
+
* ---
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
289
347
|
* ```ts
|
|
290
348
|
* const value: TValue = jsonStorage.read<TValue>("key", defaultValue);
|
|
291
349
|
* ```
|
|
292
350
|
*
|
|
351
|
+
* ---
|
|
352
|
+
*
|
|
293
353
|
* @template T The type of the value to retrieve.
|
|
294
354
|
*
|
|
295
355
|
* @param key The key of the value to retrieve.
|
|
@@ -302,10 +362,15 @@ export default class JSONStorage
|
|
|
302
362
|
/**
|
|
303
363
|
* Retrieves the value with the specified key from the persistent {@link localStorage}.
|
|
304
364
|
*
|
|
365
|
+
* ---
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
305
368
|
* ```ts
|
|
306
369
|
* const value: TValue = jsonStorage.read<TValue>("key", obj?.value);
|
|
307
370
|
* ```
|
|
308
371
|
*
|
|
372
|
+
* ---
|
|
373
|
+
*
|
|
309
374
|
* @template T The type of the value to retrieve.
|
|
310
375
|
*
|
|
311
376
|
* @param key The key of the value to retrieve.
|
|
@@ -322,6 +387,9 @@ export default class JSONStorage
|
|
|
322
387
|
/**
|
|
323
388
|
* Checks whether the value with the specified key exists within the default storage.
|
|
324
389
|
*
|
|
390
|
+
* ---
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
325
393
|
* ```ts
|
|
326
394
|
* if (jsonStorage.has("key"))
|
|
327
395
|
* {
|
|
@@ -329,6 +397,8 @@ export default class JSONStorage
|
|
|
329
397
|
* }
|
|
330
398
|
* ```
|
|
331
399
|
*
|
|
400
|
+
* ---
|
|
401
|
+
*
|
|
332
402
|
* @param key The key of the value to check.
|
|
333
403
|
* @param persistent
|
|
334
404
|
* Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
|
|
@@ -346,6 +416,9 @@ export default class JSONStorage
|
|
|
346
416
|
/**
|
|
347
417
|
* Checks whether the value with the specified key exists within the volatile {@link sessionStorage}.
|
|
348
418
|
*
|
|
419
|
+
* ---
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
349
422
|
* ```ts
|
|
350
423
|
* if (jsonStorage.knows("key"))
|
|
351
424
|
* {
|
|
@@ -353,6 +426,8 @@ export default class JSONStorage
|
|
|
353
426
|
* }
|
|
354
427
|
* ```
|
|
355
428
|
*
|
|
429
|
+
* ---
|
|
430
|
+
*
|
|
356
431
|
* @param key The key of the value to check.
|
|
357
432
|
*
|
|
358
433
|
* @returns `true` if the key exists, `false` otherwise.
|
|
@@ -366,6 +441,9 @@ export default class JSONStorage
|
|
|
366
441
|
* Checks whether the value with the specified key exists looking first in the
|
|
367
442
|
* volatile {@link sessionStorage} and then, if not found, in the persistent {@link localStorage}.
|
|
368
443
|
*
|
|
444
|
+
* ---
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
369
447
|
* ```ts
|
|
370
448
|
* if (jsonStorage.find("key"))
|
|
371
449
|
* {
|
|
@@ -373,6 +451,8 @@ export default class JSONStorage
|
|
|
373
451
|
* }
|
|
374
452
|
* ```
|
|
375
453
|
*
|
|
454
|
+
* ---
|
|
455
|
+
*
|
|
376
456
|
* @param key The key of the value to check.
|
|
377
457
|
*
|
|
378
458
|
* @returns `true` if the key exists, `false` otherwise.
|
|
@@ -385,6 +465,9 @@ export default class JSONStorage
|
|
|
385
465
|
/**
|
|
386
466
|
* Checks whether the value with the specified key exists within the persistent {@link localStorage}.
|
|
387
467
|
*
|
|
468
|
+
* ---
|
|
469
|
+
*
|
|
470
|
+
* @example
|
|
388
471
|
* ```ts
|
|
389
472
|
* if (jsonStorage.exists("key"))
|
|
390
473
|
* {
|
|
@@ -392,6 +475,8 @@ export default class JSONStorage
|
|
|
392
475
|
* }
|
|
393
476
|
* ```
|
|
394
477
|
*
|
|
478
|
+
* ---
|
|
479
|
+
*
|
|
395
480
|
* @param key The key of the value to check.
|
|
396
481
|
*
|
|
397
482
|
* @returns `true` if the key exists, `false` otherwise.
|
|
@@ -405,12 +490,17 @@ export default class JSONStorage
|
|
|
405
490
|
* Sets the value with the specified key in the default storage.
|
|
406
491
|
* If the value is `undefined` or omitted, the key is removed from the storage.
|
|
407
492
|
*
|
|
493
|
+
* ---
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
408
496
|
* ```ts
|
|
409
497
|
* jsonStorage.set("key");
|
|
410
498
|
* jsonStorage.set("key", value);
|
|
411
499
|
* jsonStorage.set("key", obj?.value);
|
|
412
500
|
* ```
|
|
413
501
|
*
|
|
502
|
+
* ---
|
|
503
|
+
*
|
|
414
504
|
* @template T The type of the value to set.
|
|
415
505
|
*
|
|
416
506
|
* @param key The key of the value to set.
|
|
@@ -430,12 +520,17 @@ export default class JSONStorage
|
|
|
430
520
|
* Sets the value with the specified key in the volatile {@link sessionStorage}.
|
|
431
521
|
* If the value is `undefined` or omitted, the key is removed from the storage.
|
|
432
522
|
*
|
|
523
|
+
* ---
|
|
524
|
+
*
|
|
525
|
+
* @example
|
|
433
526
|
* ```ts
|
|
434
527
|
* jsonStorage.remember("key");
|
|
435
528
|
* jsonStorage.remember("key", value);
|
|
436
529
|
* jsonStorage.remember("key", obj?.value);
|
|
437
530
|
* ```
|
|
438
531
|
*
|
|
532
|
+
* ---
|
|
533
|
+
*
|
|
439
534
|
* @template T The type of the value to set.
|
|
440
535
|
*
|
|
441
536
|
* @param key The key of the value to set.
|
|
@@ -450,12 +545,17 @@ export default class JSONStorage
|
|
|
450
545
|
* Sets the value with the specified key in the persistent {@link localStorage}.
|
|
451
546
|
* If the value is `undefined` or omitted, the key is removed from the storage.
|
|
452
547
|
*
|
|
548
|
+
* ---
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
453
551
|
* ```ts
|
|
454
552
|
* jsonStorage.write("key");
|
|
455
553
|
* jsonStorage.write("key", value);
|
|
456
554
|
* jsonStorage.write("key", obj?.value);
|
|
457
555
|
* ```
|
|
458
556
|
*
|
|
557
|
+
* ---
|
|
558
|
+
*
|
|
459
559
|
* @template T The type of the value to set.
|
|
460
560
|
*
|
|
461
561
|
* @param key The key of the value to set.
|
|
@@ -469,10 +569,15 @@ export default class JSONStorage
|
|
|
469
569
|
/**
|
|
470
570
|
* Removes the value with the specified key from the default storage.
|
|
471
571
|
*
|
|
572
|
+
* ---
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
472
575
|
* ```ts
|
|
473
576
|
* jsonStorage.delete("key");
|
|
474
577
|
* ```
|
|
475
578
|
*
|
|
579
|
+
* ---
|
|
580
|
+
*
|
|
476
581
|
* @param key The key of the value to remove.
|
|
477
582
|
* @param persistent
|
|
478
583
|
* Whether to prefer the persistent {@link localStorage} over the volatile {@link sessionStorage}.
|
|
@@ -488,10 +593,15 @@ export default class JSONStorage
|
|
|
488
593
|
/**
|
|
489
594
|
* Removes the value with the specified key from the volatile {@link sessionStorage}.
|
|
490
595
|
*
|
|
596
|
+
* ---
|
|
597
|
+
*
|
|
598
|
+
* @example
|
|
491
599
|
* ```ts
|
|
492
600
|
* jsonStorage.forget("key");
|
|
493
601
|
* ```
|
|
494
602
|
*
|
|
603
|
+
* ---
|
|
604
|
+
*
|
|
495
605
|
* @param key The key of the value to remove.
|
|
496
606
|
*/
|
|
497
607
|
public forget(key: string): void
|
|
@@ -502,10 +612,15 @@ export default class JSONStorage
|
|
|
502
612
|
/**
|
|
503
613
|
* Removes the value with the specified key from the persistent {@link localStorage}.
|
|
504
614
|
*
|
|
615
|
+
* ---
|
|
616
|
+
*
|
|
617
|
+
* @example
|
|
505
618
|
* ```ts
|
|
506
619
|
* jsonStorage.erase("key");
|
|
507
620
|
* ```
|
|
508
621
|
*
|
|
622
|
+
* ---
|
|
623
|
+
*
|
|
509
624
|
* @param key The key of the value to remove.
|
|
510
625
|
*/
|
|
511
626
|
public erase(key: string): void
|
|
@@ -517,10 +632,15 @@ export default class JSONStorage
|
|
|
517
632
|
* Removes the value with the specified key from both the
|
|
518
633
|
* volatile {@link sessionStorage} and the persistent {@link localStorage}.
|
|
519
634
|
*
|
|
635
|
+
* ---
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
520
638
|
* ```ts
|
|
521
639
|
* jsonStorage.clear("key");
|
|
522
640
|
* ```
|
|
523
641
|
*
|
|
642
|
+
* ---
|
|
643
|
+
*
|
|
524
644
|
* @param key The key of the value to remove.
|
|
525
645
|
*/
|
|
526
646
|
public clear(key: string): void
|
|
@@ -58,10 +58,15 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
|
|
|
58
58
|
/**
|
|
59
59
|
* Initializes a new instance of the {@link DeferredPromise} class.
|
|
60
60
|
*
|
|
61
|
+
* ---
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
61
64
|
* ```ts
|
|
62
65
|
* const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
63
66
|
* ```
|
|
64
67
|
*
|
|
68
|
+
* ---
|
|
69
|
+
*
|
|
65
70
|
* @param onFulfilled The callback to execute once the promise is fulfilled.
|
|
66
71
|
* @param onRejected The callback to execute once the promise is rejected.
|
|
67
72
|
*/
|
|
@@ -88,6 +93,9 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
|
|
|
88
93
|
/**
|
|
89
94
|
* Watches another promise and resolves or rejects this promise when the other one is settled.
|
|
90
95
|
*
|
|
96
|
+
* ---
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
91
99
|
* ```ts
|
|
92
100
|
* const promise = new Promise<string>((resolve) => setTimeout(() => resolve("Hello, World!"), 1_000));
|
|
93
101
|
* const deferred = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
|
|
@@ -96,6 +104,8 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
|
|
|
96
104
|
* deferred.watch(promise);
|
|
97
105
|
* ```
|
|
98
106
|
*
|
|
107
|
+
* ---
|
|
108
|
+
*
|
|
99
109
|
* @param otherPromise The promise to watch.
|
|
100
110
|
*
|
|
101
111
|
* @returns The current instance of the {@link DeferredPromise} class.
|
|
@@ -29,6 +29,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
29
29
|
/**
|
|
30
30
|
* Wraps a new {@link SmartPromise} object around an existing native {@link Promise} object.
|
|
31
31
|
*
|
|
32
|
+
* ---
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
32
35
|
* ```ts
|
|
33
36
|
* const request = fetch("https://api.example.com/data");
|
|
34
37
|
* const smartRequest = SmartPromise.FromPromise(request);
|
|
@@ -40,6 +43,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
40
43
|
* console.log(smartRequest.isFulfilled); // true
|
|
41
44
|
* ```
|
|
42
45
|
*
|
|
46
|
+
* ---
|
|
47
|
+
*
|
|
43
48
|
* @param promise The promise to wrap.
|
|
44
49
|
*
|
|
45
50
|
* @returns A new {@link SmartPromise} object that wraps the provided promise.
|
|
@@ -105,6 +110,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
105
110
|
/**
|
|
106
111
|
* Initializes a new instance of the {@link SmartPromise} class.
|
|
107
112
|
*
|
|
113
|
+
* ---
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
108
116
|
* ```ts
|
|
109
117
|
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
110
118
|
* {
|
|
@@ -112,6 +120,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
112
120
|
* });
|
|
113
121
|
* ```
|
|
114
122
|
*
|
|
123
|
+
* ---
|
|
124
|
+
*
|
|
115
125
|
* @param executor
|
|
116
126
|
* The function responsible for eventually resolving or rejecting the promise.
|
|
117
127
|
* Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
|
|
@@ -144,6 +154,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
144
154
|
/**
|
|
145
155
|
* Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
|
|
146
156
|
*
|
|
157
|
+
* ---
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
147
160
|
* ```ts
|
|
148
161
|
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
149
162
|
* {
|
|
@@ -153,6 +166,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
153
166
|
* console.log(await promise.then()); // "Hello, World!"
|
|
154
167
|
* ```
|
|
155
168
|
*
|
|
169
|
+
* ---
|
|
170
|
+
*
|
|
156
171
|
* @returns A new {@link Promise} identical to the original one.
|
|
157
172
|
*/
|
|
158
173
|
public then(onFulfilled?: null): Promise<T>;
|
|
@@ -163,6 +178,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
163
178
|
* The previous result of the promise is passed as the argument to the callback.
|
|
164
179
|
* The callback's return value is considered the new promise's result instead.
|
|
165
180
|
*
|
|
181
|
+
* ---
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
166
184
|
* ```ts
|
|
167
185
|
* const promise = new SmartPromise<string>((resolve, reject) =>
|
|
168
186
|
* {
|
|
@@ -172,6 +190,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
172
190
|
* promise.then((result) => console.log(result)); // "Hello, World!"
|
|
173
191
|
* ```
|
|
174
192
|
*
|
|
193
|
+
* ---
|
|
194
|
+
*
|
|
175
195
|
* @template F The type of value the new promise will eventually resolve to. Default is `T`.
|
|
176
196
|
*
|
|
177
197
|
* @param onFulfilled The callback to execute once the promise is fulfilled.
|
|
@@ -193,6 +213,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
193
213
|
* The rejection callback's return value is considered the new promise's result.
|
|
194
214
|
* - If the rejection callback throws an error, the new promise is rejected with that error.
|
|
195
215
|
*
|
|
216
|
+
* ---
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
196
219
|
* ```ts
|
|
197
220
|
* const promise = new SmartPromise((resolve, reject) =>
|
|
198
221
|
* {
|
|
@@ -203,6 +226,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
203
226
|
* promise.then(() => console.log("OK!"), () => console.log("KO!")); // "OK!" or "KO!"
|
|
204
227
|
* ```
|
|
205
228
|
*
|
|
229
|
+
* ---
|
|
230
|
+
*
|
|
206
231
|
* @template F The type of value the new promise will eventually resolve to. Default is `T`.
|
|
207
232
|
* @template R The type of value the new promise will eventually resolve to. Default is `never`.
|
|
208
233
|
*
|
|
@@ -223,6 +248,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
223
248
|
/**
|
|
224
249
|
* Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
|
|
225
250
|
*
|
|
251
|
+
* ---
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
226
254
|
* ```ts
|
|
227
255
|
* const promise = new SmartPromise((resolve, reject) =>
|
|
228
256
|
* {
|
|
@@ -232,6 +260,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
232
260
|
* promise.catch(); // Uncaught Error: An unknown error occurred.
|
|
233
261
|
* ```
|
|
234
262
|
*
|
|
263
|
+
* ---
|
|
264
|
+
*
|
|
235
265
|
* @returns A new {@link Promise} identical to the original one.
|
|
236
266
|
*/
|
|
237
267
|
public catch(onRejected?: null): Promise<T>;
|
|
@@ -245,6 +275,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
245
275
|
* The callback's return value is considered the new promise's result.
|
|
246
276
|
* - If the callback throws an error, the new promise is rejected with that error.
|
|
247
277
|
*
|
|
278
|
+
* ---
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
248
281
|
* ```ts
|
|
249
282
|
* const promise = new SmartPromise((resolve, reject) =>
|
|
250
283
|
* {
|
|
@@ -254,6 +287,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
254
287
|
* promise.catch((reason) => console.error(reason)); // "Error: An unknown error occurred."
|
|
255
288
|
* ```
|
|
256
289
|
*
|
|
290
|
+
* ---
|
|
291
|
+
*
|
|
257
292
|
* @template R The type of value the new promise will eventually resolve to. Default is `T`.
|
|
258
293
|
*
|
|
259
294
|
* @param onRejected The callback to execute once the promise is rejected.
|
|
@@ -269,6 +304,9 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
269
304
|
/**
|
|
270
305
|
* Attaches a callback that executes right after the promise is settled, regardless of the outcome.
|
|
271
306
|
*
|
|
307
|
+
* ---
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
272
310
|
* ```ts
|
|
273
311
|
* const promise = new SmartPromise((resolve, reject) =>
|
|
274
312
|
* {
|
|
@@ -283,6 +321,8 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
283
321
|
* .finally(() => console.log("Done!")); // Always logs "Done!".
|
|
284
322
|
* ```
|
|
285
323
|
*
|
|
324
|
+
* ---
|
|
325
|
+
*
|
|
286
326
|
* @param onFinally The callback to execute when once promise is settled.
|
|
287
327
|
*
|
|
288
328
|
* @returns A new {@link Promise} that executes the callback once the promise is settled.
|
|
@@ -28,6 +28,9 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
|
28
28
|
/**
|
|
29
29
|
* Initializes a new instance of the {@link TimedPromise} class.
|
|
30
30
|
*
|
|
31
|
+
* ---
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
31
34
|
* ```ts
|
|
32
35
|
* const promise = new TimedPromise<string>((resolve, reject) =>
|
|
33
36
|
* {
|
|
@@ -36,6 +39,8 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
|
|
|
36
39
|
* }, 5_000);
|
|
37
40
|
* ```
|
|
38
41
|
*
|
|
42
|
+
* ---
|
|
43
|
+
*
|
|
39
44
|
* @param executor
|
|
40
45
|
* The function responsible for eventually resolving or rejecting the promise.
|
|
41
46
|
* Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
|
|
@@ -42,10 +42,15 @@ export default class Clock extends GameLoop
|
|
|
42
42
|
/**
|
|
43
43
|
* Initializes a new instance of the {@link Clock} class.
|
|
44
44
|
*
|
|
45
|
+
* ---
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
45
48
|
* ```ts
|
|
46
49
|
* const clock = new Clock();
|
|
47
50
|
* ```
|
|
48
51
|
*
|
|
52
|
+
* ---
|
|
53
|
+
*
|
|
49
54
|
* @param msIfNotBrowser
|
|
50
55
|
* The interval in milliseconds at which the clock will tick if the environment is not a browser.
|
|
51
56
|
* `TimeUnit.Second` by default.
|
|
@@ -62,11 +67,16 @@ export default class Clock extends GameLoop
|
|
|
62
67
|
*
|
|
63
68
|
* If the clock is already running, a {@link RuntimeException} will be thrown.
|
|
64
69
|
*
|
|
70
|
+
* ---
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
65
73
|
* ```ts
|
|
66
74
|
* clock.onStart(() => { [...] }); // This callback will be executed.
|
|
67
75
|
* clock.start();
|
|
68
76
|
* ```
|
|
69
77
|
*
|
|
78
|
+
* ---
|
|
79
|
+
*
|
|
70
80
|
* @param elapsedTime The elapsed time to set as default when the clock starts. Default is `0`.
|
|
71
81
|
*/
|
|
72
82
|
public override start(elapsedTime = 0): void
|
|
@@ -85,6 +95,9 @@ export default class Clock extends GameLoop
|
|
|
85
95
|
*
|
|
86
96
|
* If the clock hasn't yet started, a {@link RuntimeException} will be thrown.
|
|
87
97
|
*
|
|
98
|
+
* ---
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
88
101
|
* ```ts
|
|
89
102
|
* clock.onStop(() => { [...] }); // This callback will be executed.
|
|
90
103
|
* clock.stop();
|
|
@@ -105,11 +118,16 @@ export default class Clock extends GameLoop
|
|
|
105
118
|
/**
|
|
106
119
|
* Subscribes to the `tick` event of the clock.
|
|
107
120
|
*
|
|
121
|
+
* ---
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
108
124
|
* ```ts
|
|
109
125
|
* clock.onTick((elapsedTime) => { [...] }); // This callback will be executed.
|
|
110
126
|
* clock.start();
|
|
111
127
|
* ```
|
|
112
128
|
*
|
|
129
|
+
* ---
|
|
130
|
+
*
|
|
113
131
|
* @param callback The callback that will be executed when the clock ticks.
|
|
114
132
|
* @param tickStep
|
|
115
133
|
* The minimum time in milliseconds that must pass from the previous execution of the callback to the next one.
|