@byloth/core 2.0.0 → 2.0.2

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.
Files changed (39) hide show
  1. package/README.md +1 -0
  2. package/dist/core.js +900 -187
  3. package/dist/core.js.map +1 -1
  4. package/dist/core.umd.cjs +2 -2
  5. package/dist/core.umd.cjs.map +1 -1
  6. package/package.json +13 -10
  7. package/src/core/types.ts +43 -10
  8. package/src/index.ts +3 -2
  9. package/src/models/aggregators/aggregated-async-iterator.ts +161 -1
  10. package/src/models/aggregators/aggregated-iterator.ts +146 -1
  11. package/src/models/aggregators/reduced-iterator.ts +148 -8
  12. package/src/models/aggregators/types.ts +35 -0
  13. package/src/models/callbacks/callable-object.ts +7 -0
  14. package/src/models/callbacks/publisher.ts +33 -8
  15. package/src/models/callbacks/switchable-callback.ts +102 -21
  16. package/src/models/callbacks/types.ts +32 -0
  17. package/src/models/exceptions/core.ts +29 -0
  18. package/src/models/exceptions/index.ts +105 -1
  19. package/src/models/iterators/smart-async-iterator.ts +145 -0
  20. package/src/models/iterators/smart-iterator.ts +130 -0
  21. package/src/models/iterators/types.ts +79 -1
  22. package/src/models/json/json-storage.ts +123 -0
  23. package/src/models/json/types.ts +1 -1
  24. package/src/models/promises/deferred-promise.ts +15 -0
  25. package/src/models/promises/smart-promise.ts +45 -0
  26. package/src/models/promises/timed-promise.ts +10 -0
  27. package/src/models/promises/types.ts +30 -0
  28. package/src/models/timers/clock.ts +21 -0
  29. package/src/models/timers/countdown.ts +30 -0
  30. package/src/models/timers/game-loop.ts +26 -0
  31. package/src/models/types.ts +1 -1
  32. package/src/utils/async.ts +15 -0
  33. package/src/utils/curve.ts +11 -1
  34. package/src/utils/date.ts +36 -6
  35. package/src/utils/dom.ts +5 -0
  36. package/src/utils/iterator.ts +40 -0
  37. package/src/utils/math.ts +15 -0
  38. package/src/utils/random.ts +34 -0
  39. package/src/utils/string.ts +5 -0
@@ -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.
@@ -58,10 +63,15 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
58
63
  /**
59
64
  * Initializes a new instance of the {@link DeferredPromise} class.
60
65
  *
66
+ * ---
67
+ *
68
+ * @example
61
69
  * ```ts
62
70
  * const promise = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
63
71
  * ```
64
72
  *
73
+ * ---
74
+ *
65
75
  * @param onFulfilled The callback to execute once the promise is fulfilled.
66
76
  * @param onRejected The callback to execute once the promise is rejected.
67
77
  */
@@ -88,6 +98,9 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
88
98
  /**
89
99
  * Watches another promise and resolves or rejects this promise when the other one is settled.
90
100
  *
101
+ * ---
102
+ *
103
+ * @example
91
104
  * ```ts
92
105
  * const promise = new Promise<string>((resolve) => setTimeout(() => resolve("Hello, World!"), 1_000));
93
106
  * const deferred = new DeferredPromise<string, string[]>((value: string) => value.split(" "));
@@ -96,6 +109,8 @@ export default class DeferredPromise<T = void, F = T, R = never> extends SmartPr
96
109
  * deferred.watch(promise);
97
110
  * ```
98
111
  *
112
+ * ---
113
+ *
99
114
  * @param otherPromise The promise to watch.
100
115
  *
101
116
  * @returns The current instance of the {@link DeferredPromise} class.
@@ -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>
@@ -29,6 +34,9 @@ export default class SmartPromise<T = void> implements Promise<T>
29
34
  /**
30
35
  * Wraps a new {@link SmartPromise} object around an existing native {@link Promise} object.
31
36
  *
37
+ * ---
38
+ *
39
+ * @example
32
40
  * ```ts
33
41
  * const request = fetch("https://api.example.com/data");
34
42
  * const smartRequest = SmartPromise.FromPromise(request);
@@ -40,6 +48,8 @@ export default class SmartPromise<T = void> implements Promise<T>
40
48
  * console.log(smartRequest.isFulfilled); // true
41
49
  * ```
42
50
  *
51
+ * ---
52
+ *
43
53
  * @param promise The promise to wrap.
44
54
  *
45
55
  * @returns A new {@link SmartPromise} object that wraps the provided promise.
@@ -105,6 +115,9 @@ export default class SmartPromise<T = void> implements Promise<T>
105
115
  /**
106
116
  * Initializes a new instance of the {@link SmartPromise} class.
107
117
  *
118
+ * ---
119
+ *
120
+ * @example
108
121
  * ```ts
109
122
  * const promise = new SmartPromise<string>((resolve, reject) =>
110
123
  * {
@@ -112,6 +125,8 @@ export default class SmartPromise<T = void> implements Promise<T>
112
125
  * });
113
126
  * ```
114
127
  *
128
+ * ---
129
+ *
115
130
  * @param executor
116
131
  * The function responsible for eventually resolving or rejecting the promise.
117
132
  * Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
@@ -144,6 +159,9 @@ export default class SmartPromise<T = void> implements Promise<T>
144
159
  /**
145
160
  * Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
146
161
  *
162
+ * ---
163
+ *
164
+ * @example
147
165
  * ```ts
148
166
  * const promise = new SmartPromise<string>((resolve, reject) =>
149
167
  * {
@@ -153,6 +171,8 @@ export default class SmartPromise<T = void> implements Promise<T>
153
171
  * console.log(await promise.then()); // "Hello, World!"
154
172
  * ```
155
173
  *
174
+ * ---
175
+ *
156
176
  * @returns A new {@link Promise} identical to the original one.
157
177
  */
158
178
  public then(onFulfilled?: null): Promise<T>;
@@ -163,6 +183,9 @@ export default class SmartPromise<T = void> implements Promise<T>
163
183
  * The previous result of the promise is passed as the argument to the callback.
164
184
  * The callback's return value is considered the new promise's result instead.
165
185
  *
186
+ * ---
187
+ *
188
+ * @example
166
189
  * ```ts
167
190
  * const promise = new SmartPromise<string>((resolve, reject) =>
168
191
  * {
@@ -172,6 +195,8 @@ export default class SmartPromise<T = void> implements Promise<T>
172
195
  * promise.then((result) => console.log(result)); // "Hello, World!"
173
196
  * ```
174
197
  *
198
+ * ---
199
+ *
175
200
  * @template F The type of value the new promise will eventually resolve to. Default is `T`.
176
201
  *
177
202
  * @param onFulfilled The callback to execute once the promise is fulfilled.
@@ -193,6 +218,9 @@ export default class SmartPromise<T = void> implements Promise<T>
193
218
  * The rejection callback's return value is considered the new promise's result.
194
219
  * - If the rejection callback throws an error, the new promise is rejected with that error.
195
220
  *
221
+ * ---
222
+ *
223
+ * @example
196
224
  * ```ts
197
225
  * const promise = new SmartPromise((resolve, reject) =>
198
226
  * {
@@ -203,6 +231,8 @@ export default class SmartPromise<T = void> implements Promise<T>
203
231
  * promise.then(() => console.log("OK!"), () => console.log("KO!")); // "OK!" or "KO!"
204
232
  * ```
205
233
  *
234
+ * ---
235
+ *
206
236
  * @template F The type of value the new promise will eventually resolve to. Default is `T`.
207
237
  * @template R The type of value the new promise will eventually resolve to. Default is `never`.
208
238
  *
@@ -223,6 +253,9 @@ export default class SmartPromise<T = void> implements Promise<T>
223
253
  /**
224
254
  * Creates a new {@link Promise} identical to the one wrapped by this instance, with a different reference.
225
255
  *
256
+ * ---
257
+ *
258
+ * @example
226
259
  * ```ts
227
260
  * const promise = new SmartPromise((resolve, reject) =>
228
261
  * {
@@ -232,6 +265,8 @@ export default class SmartPromise<T = void> implements Promise<T>
232
265
  * promise.catch(); // Uncaught Error: An unknown error occurred.
233
266
  * ```
234
267
  *
268
+ * ---
269
+ *
235
270
  * @returns A new {@link Promise} identical to the original one.
236
271
  */
237
272
  public catch(onRejected?: null): Promise<T>;
@@ -245,6 +280,9 @@ export default class SmartPromise<T = void> implements Promise<T>
245
280
  * The callback's return value is considered the new promise's result.
246
281
  * - If the callback throws an error, the new promise is rejected with that error.
247
282
  *
283
+ * ---
284
+ *
285
+ * @example
248
286
  * ```ts
249
287
  * const promise = new SmartPromise((resolve, reject) =>
250
288
  * {
@@ -254,6 +292,8 @@ export default class SmartPromise<T = void> implements Promise<T>
254
292
  * promise.catch((reason) => console.error(reason)); // "Error: An unknown error occurred."
255
293
  * ```
256
294
  *
295
+ * ---
296
+ *
257
297
  * @template R The type of value the new promise will eventually resolve to. Default is `T`.
258
298
  *
259
299
  * @param onRejected The callback to execute once the promise is rejected.
@@ -269,6 +309,9 @@ export default class SmartPromise<T = void> implements Promise<T>
269
309
  /**
270
310
  * Attaches a callback that executes right after the promise is settled, regardless of the outcome.
271
311
  *
312
+ * ---
313
+ *
314
+ * @example
272
315
  * ```ts
273
316
  * const promise = new SmartPromise((resolve, reject) =>
274
317
  * {
@@ -283,6 +326,8 @@ export default class SmartPromise<T = void> implements Promise<T>
283
326
  * .finally(() => console.log("Done!")); // Always logs "Done!".
284
327
  * ```
285
328
  *
329
+ * ---
330
+ *
286
331
  * @param onFinally The callback to execute when once promise is settled.
287
332
  *
288
333
  * @returns A new {@link Promise} that executes the callback once the promise is settled.
@@ -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>
@@ -28,6 +33,9 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
28
33
  /**
29
34
  * Initializes a new instance of the {@link TimedPromise} class.
30
35
  *
36
+ * ---
37
+ *
38
+ * @example
31
39
  * ```ts
32
40
  * const promise = new TimedPromise<string>((resolve, reject) =>
33
41
  * {
@@ -36,6 +44,8 @@ export default class TimedPromise<T = void> extends SmartPromise<T>
36
44
  * }, 5_000);
37
45
  * ```
38
46
  *
47
+ * ---
48
+ *
39
49
  * @param executor
40
50
  * The function responsible for eventually resolving or rejecting the promise.
41
51
  * Similarly to the native {@link Promise} object, it's immediately executed after the promise is created.
@@ -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
  *
@@ -42,10 +45,15 @@ export default class Clock extends GameLoop
42
45
  /**
43
46
  * Initializes a new instance of the {@link Clock} class.
44
47
  *
48
+ * ---
49
+ *
50
+ * @example
45
51
  * ```ts
46
52
  * const clock = new Clock();
47
53
  * ```
48
54
  *
55
+ * ---
56
+ *
49
57
  * @param msIfNotBrowser
50
58
  * The interval in milliseconds at which the clock will tick if the environment is not a browser.
51
59
  * `TimeUnit.Second` by default.
@@ -62,11 +70,16 @@ export default class Clock extends GameLoop
62
70
  *
63
71
  * If the clock is already running, a {@link RuntimeException} will be thrown.
64
72
  *
73
+ * ---
74
+ *
75
+ * @example
65
76
  * ```ts
66
77
  * clock.onStart(() => { [...] }); // This callback will be executed.
67
78
  * clock.start();
68
79
  * ```
69
80
  *
81
+ * ---
82
+ *
70
83
  * @param elapsedTime The elapsed time to set as default when the clock starts. Default is `0`.
71
84
  */
72
85
  public override start(elapsedTime = 0): void
@@ -85,6 +98,9 @@ export default class Clock extends GameLoop
85
98
  *
86
99
  * If the clock hasn't yet started, a {@link RuntimeException} will be thrown.
87
100
  *
101
+ * ---
102
+ *
103
+ * @example
88
104
  * ```ts
89
105
  * clock.onStop(() => { [...] }); // This callback will be executed.
90
106
  * clock.stop();
@@ -105,11 +121,16 @@ export default class Clock extends GameLoop
105
121
  /**
106
122
  * Subscribes to the `tick` event of the clock.
107
123
  *
124
+ * ---
125
+ *
126
+ * @example
108
127
  * ```ts
109
128
  * clock.onTick((elapsedTime) => { [...] }); // This callback will be executed.
110
129
  * clock.start();
111
130
  * ```
112
131
  *
132
+ * ---
133
+ *
113
134
  * @param callback The callback that will be executed when the clock ticks.
114
135
  * @param tickStep
115
136
  * The minimum time in milliseconds that must pass from the previous execution of the callback to the next one.
@@ -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
  *
@@ -75,10 +78,15 @@ export default class Countdown extends GameLoop
75
78
  /**
76
79
  * Initializes a new instance of the {@link Countdown} class.
77
80
  *
81
+ * ---
82
+ *
83
+ * @example
78
84
  * ```ts
79
85
  * const countdown = new Countdown(10_000);
80
86
  * ```
81
87
  *
88
+ * ---
89
+ *
82
90
  * @param duration
83
91
  * The total duration of the countdown in milliseconds.
84
92
  *
@@ -114,6 +122,8 @@ export default class Countdown extends GameLoop
114
122
  * The internal method actually responsible for stopping the
115
123
  * countdown and resolving or rejecting the {@link Countdown._deferrer} promise.
116
124
  *
125
+ * ---
126
+ *
117
127
  * @param reason
118
128
  * The reason why the countdown has stopped.
119
129
  *
@@ -140,11 +150,16 @@ export default class Countdown extends GameLoop
140
150
  *
141
151
  * If the countdown is already running, a {@link RuntimeException} will be thrown.
142
152
  *
153
+ * ---
154
+ *
155
+ * @example
143
156
  * ```ts
144
157
  * countdown.onStart(() => { [...] }); // This callback will be executed.
145
158
  * countdown.start();
146
159
  * ```
147
160
  *
161
+ * ---
162
+ *
148
163
  * @param remainingTime
149
164
  * The remaining time to set as default when the countdown starts.
150
165
  * Default is the {@link Countdown.duration} itself.
@@ -169,11 +184,16 @@ export default class Countdown extends GameLoop
169
184
  *
170
185
  * If the countdown hasn't yet started, a {@link RuntimeException} will be thrown.
171
186
  *
187
+ * ---
188
+ *
189
+ * @example
172
190
  * ```ts
173
191
  * countdown.onStop(() => { [...] }); // This callback will be executed.
174
192
  * countdown.stop();
175
193
  * ```
176
194
  *
195
+ * ---
196
+ *
177
197
  * @param reason
178
198
  * The reason why the countdown has stopped.
179
199
  *
@@ -194,11 +214,16 @@ export default class Countdown extends GameLoop
194
214
  /**
195
215
  * Subscribes to the `expire` event of the countdown.
196
216
  *
217
+ * ---
218
+ *
219
+ * @example
197
220
  * ```ts
198
221
  * countdown.onExpire(() => { [...] }); // This callback will be executed once the countdown has expired.
199
222
  * countdown.start();
200
223
  * ```
201
224
  *
225
+ * ---
226
+ *
202
227
  * @param callback The callback that will be executed when the countdown expires.
203
228
  *
204
229
  * @returns A function that can be used to unsubscribe from the event.
@@ -211,11 +236,16 @@ export default class Countdown extends GameLoop
211
236
  /**
212
237
  * Subscribes to the `tick` event of the countdown.
213
238
  *
239
+ * ---
240
+ *
241
+ * @example
214
242
  * ```ts
215
243
  * countdown.onTick((remainingTime) => { [...] }); // This callback will be executed.
216
244
  * countdown.start();
217
245
  * ```
218
246
  *
247
+ * ---
248
+ *
219
249
  * @param callback The callback that will be executed when the countdown ticks.
220
250
  * @param tickStep
221
251
  * The minimum time in milliseconds that must pass from the previous execution of the callback to the next one.
@@ -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
  * {
@@ -116,10 +119,15 @@ export default class GameLoop
116
119
  /**
117
120
  * Initializes a new instance of the {@link GameLoop} class.
118
121
  *
122
+ * ---
123
+ *
124
+ * @example
119
125
  * ```ts
120
126
  * const loop = new GameLoop((elapsedTime: number) => { [...] });
121
127
  * ```
122
128
  *
129
+ * ---
130
+ *
123
131
  * @param callback The function that will be executed at each iteration of the game loop.
124
132
  * @param msIfNotBrowser
125
133
  * The interval in milliseconds that will be used if the current environment isn't a browser. Default is `40`.
@@ -164,11 +172,16 @@ export default class GameLoop
164
172
  *
165
173
  * If the game loop is already running, a {@link RuntimeException} will be thrown.
166
174
  *
175
+ * ---
176
+ *
177
+ * @example
167
178
  * ```ts
168
179
  * loop.onStart(() => { [...] }); // This callback will be executed.
169
180
  * loop.start();
170
181
  * ```
171
182
  *
183
+ * ---
184
+ *
172
185
  * @param elapsedTime The elapsed time to set as default when the game loop starts. Default is `0`.
173
186
  */
174
187
  public start(elapsedTime = 0): void
@@ -187,6 +200,9 @@ export default class GameLoop
187
200
  *
188
201
  * If the game loop hasn't yet started, a {@link RuntimeException} will be thrown.
189
202
  *
203
+ * ---
204
+ *
205
+ * @example
190
206
  * ```ts
191
207
  * loop.onStop(() => { [...] }); // This callback will be executed.
192
208
  * loop.stop();
@@ -210,10 +226,15 @@ export default class GameLoop
210
226
  /**
211
227
  * Subscribes to the `start` event of the game loop.
212
228
  *
229
+ * ---
230
+ *
231
+ * @example
213
232
  * ```ts
214
233
  * loop.onStart(() => { console.log("The game loop has started."); });
215
234
  * ```
216
235
  *
236
+ * ---
237
+ *
217
238
  * @param callback The function that will be executed when the game loop starts.
218
239
  *
219
240
  * @returns A function that can be used to unsubscribe from the event.
@@ -226,10 +247,15 @@ export default class GameLoop
226
247
  /**
227
248
  * Subscribes to the `stop` event of the game loop.
228
249
  *
250
+ * ---
251
+ *
252
+ * @example
229
253
  * ```ts
230
254
  * loop.onStop(() => { console.log("The game loop has stopped."); });
231
255
  * ```
232
256
  *
257
+ * ---
258
+ *
233
259
  * @param callback The function that will be executed when the game loop stops.
234
260
  *
235
261
  * @returns A function that can be used to unsubscribe from the event.
@@ -43,4 +43,4 @@ export type {
43
43
 
44
44
  } from "./promises/types.js";
45
45
 
46
- export type { Callback } from "./callbacks/types.js";
46
+ export type { Callback, CallbackMap } from "./callbacks/types.js";
@@ -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>