@byloth/core 2.1.6 → 2.1.8
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 +23 -0
- package/dist/core.cjs +2 -2
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +1247 -1309
- package/dist/core.esm.js.map +1 -1
- package/dist/core.global.js +2 -2
- package/dist/core.global.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +8 -8
- package/src/index.ts +1 -2
- package/src/models/aggregators/aggregated-async-iterator.ts +12 -9
- package/src/models/callbacks/callable-object.ts +4 -4
- package/src/models/callbacks/publisher.ts +115 -103
- package/src/models/callbacks/switchable-callback.ts +1 -2
- package/src/models/callbacks/types.ts +4 -4
- package/src/models/collections/map-view.ts +7 -5
- package/src/models/collections/set-view.ts +4 -3
- package/src/models/collections/types.ts +6 -4
- package/src/models/iterators/smart-async-iterator.ts +0 -3
- package/src/models/json/json-storage.ts +1 -2
- package/src/models/promises/promise-queue.ts +8 -7
- package/src/models/promises/smart-promise.ts +7 -5
- package/src/models/timers/clock.ts +3 -2
- package/src/models/timers/countdown.ts +5 -4
- package/src/models/timers/game-loop.ts +5 -4
- package/src/utils/async.ts +11 -9
- package/src/utils/date.ts +14 -2
- package/src/utils/dom.ts +5 -3
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Callback } from "../types.js";
|
|
1
2
|
import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "./types.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -241,11 +242,12 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
241
242
|
*
|
|
242
243
|
* @returns A new {@link Promise} resolved or rejected based on the callbacks.
|
|
243
244
|
*/
|
|
244
|
-
public then<F = T, R = never>(onFulfilled: FulfilledHandler<T, F>, onRejected: RejectedHandler<unknown, R>)
|
|
245
|
-
: Promise<F | R>;
|
|
246
245
|
public then<F = T, R = never>(
|
|
247
|
-
onFulfilled
|
|
248
|
-
|
|
246
|
+
onFulfilled: FulfilledHandler<T, F>, onRejected: RejectedHandler<unknown, R>
|
|
247
|
+
): Promise<F | R>;
|
|
248
|
+
public then<F = T, R = never>(
|
|
249
|
+
onFulfilled?: FulfilledHandler<T, F> | null, onRejected?: RejectedHandler<unknown, R> | null
|
|
250
|
+
): Promise<F | R>
|
|
249
251
|
{
|
|
250
252
|
return this._promise.then(onFulfilled, onRejected);
|
|
251
253
|
}
|
|
@@ -332,7 +334,7 @@ export default class SmartPromise<T = void> implements Promise<T>
|
|
|
332
334
|
*
|
|
333
335
|
* @returns A new {@link Promise} that executes the callback once the promise is settled.
|
|
334
336
|
*/
|
|
335
|
-
public finally(onFinally?:
|
|
337
|
+
public finally(onFinally?: Callback | null): Promise<T>
|
|
336
338
|
{
|
|
337
339
|
return this._promise.finally(onFinally);
|
|
338
340
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { TimeUnit } from "../../utils/date.js";
|
|
2
2
|
|
|
3
|
-
import Publisher from "../callbacks/publisher.js";
|
|
3
|
+
import type Publisher from "../callbacks/publisher.js";
|
|
4
4
|
import { FatalErrorException, RangeException, RuntimeException } from "../exceptions/index.js";
|
|
5
|
+
import type { Callback } from "../types.js";
|
|
5
6
|
|
|
6
7
|
import GameLoop from "./game-loop.js";
|
|
7
8
|
|
|
@@ -136,7 +137,7 @@ export default class Clock extends GameLoop
|
|
|
136
137
|
*
|
|
137
138
|
* @returns A function that can be used to unsubscribe from the event.
|
|
138
139
|
*/
|
|
139
|
-
public onTick(callback: (elapsedTime: number) => void, tickStep = 0):
|
|
140
|
+
public onTick(callback: (elapsedTime: number) => void, tickStep = 0): Callback
|
|
140
141
|
{
|
|
141
142
|
if (tickStep < 0) { throw new RangeException("The tick step must be a non-negative number."); }
|
|
142
143
|
if (tickStep === 0) { return this._publisher.subscribe("tick", callback); }
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { TimeUnit } from "../../utils/date.js";
|
|
2
2
|
|
|
3
|
-
import Publisher from "../callbacks/publisher.js";
|
|
3
|
+
import type Publisher from "../callbacks/publisher.js";
|
|
4
4
|
import { FatalErrorException, RangeException, RuntimeException } from "../exceptions/index.js";
|
|
5
|
-
import {
|
|
5
|
+
import type { SmartPromise } from "../promises/index.js";
|
|
6
|
+
import { DeferredPromise } from "../promises/index.js";
|
|
6
7
|
import type { Callback } from "../types.js";
|
|
7
8
|
|
|
8
9
|
import GameLoop from "./game-loop.js";
|
|
@@ -227,7 +228,7 @@ export default class Countdown extends GameLoop
|
|
|
227
228
|
*
|
|
228
229
|
* @returns A function that can be used to unsubscribe from the event.
|
|
229
230
|
*/
|
|
230
|
-
public onExpire(callback:
|
|
231
|
+
public onExpire(callback: Callback): Callback
|
|
231
232
|
{
|
|
232
233
|
return this._publisher.subscribe("expire", callback);
|
|
233
234
|
}
|
|
@@ -256,7 +257,7 @@ export default class Countdown extends GameLoop
|
|
|
256
257
|
*
|
|
257
258
|
* @returns A function that can be used to unsubscribe from the event.
|
|
258
259
|
*/
|
|
259
|
-
public onTick(callback: (remainingTime: number) => void, tickStep = 0):
|
|
260
|
+
public onTick(callback: (remainingTime: number) => void, tickStep = 0): Callback
|
|
260
261
|
{
|
|
261
262
|
if (tickStep < 0) { throw new RangeException("The tick step must be a non-negative number."); }
|
|
262
263
|
if (tickStep === 0) { return this._publisher.subscribe("tick", callback); }
|
|
@@ -3,6 +3,7 @@ import { isBrowser } from "../../helpers.js";
|
|
|
3
3
|
|
|
4
4
|
import Publisher from "../callbacks/publisher.js";
|
|
5
5
|
import { FatalErrorException, RuntimeException } from "../exceptions/index.js";
|
|
6
|
+
import type { Callback } from "../types.js";
|
|
6
7
|
|
|
7
8
|
interface GameLoopEventsMap
|
|
8
9
|
{
|
|
@@ -102,7 +103,7 @@ export default class GameLoop
|
|
|
102
103
|
* Depending on the current environment, it could use the
|
|
103
104
|
* {@link requestAnimationFrame} or the {@link setInterval} function.
|
|
104
105
|
*/
|
|
105
|
-
protected readonly _start:
|
|
106
|
+
protected readonly _start: Callback;
|
|
106
107
|
|
|
107
108
|
/**
|
|
108
109
|
* The internal method actually responsible for stopping the game loop.
|
|
@@ -110,7 +111,7 @@ export default class GameLoop
|
|
|
110
111
|
* Depending on the current environment, it could use the
|
|
111
112
|
* {@link cancelAnimationFrame} or the {@link clearInterval} function.
|
|
112
113
|
*/
|
|
113
|
-
protected readonly _stop:
|
|
114
|
+
protected readonly _stop: Callback;
|
|
114
115
|
|
|
115
116
|
/**
|
|
116
117
|
* Initializes a new instance of the {@link GameLoop} class.
|
|
@@ -235,7 +236,7 @@ export default class GameLoop
|
|
|
235
236
|
*
|
|
236
237
|
* @returns A function that can be used to unsubscribe from the event.
|
|
237
238
|
*/
|
|
238
|
-
public onStart(callback:
|
|
239
|
+
public onStart(callback: Callback): Callback
|
|
239
240
|
{
|
|
240
241
|
return this._publisher.subscribe("start", callback);
|
|
241
242
|
}
|
|
@@ -256,7 +257,7 @@ export default class GameLoop
|
|
|
256
257
|
*
|
|
257
258
|
* @returns A function that can be used to unsubscribe from the event.
|
|
258
259
|
*/
|
|
259
|
-
public onStop(callback:
|
|
260
|
+
public onStop(callback: Callback): Callback
|
|
260
261
|
{
|
|
261
262
|
return this._publisher.subscribe("stop", callback);
|
|
262
263
|
}
|
package/src/utils/async.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SmartPromise } from "../models/index.js";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Returns a promise that resolves after a certain number of milliseconds.
|
|
3
5
|
* It can be used to pause or delay the execution of an asynchronous function.
|
|
@@ -15,11 +17,11 @@
|
|
|
15
17
|
*
|
|
16
18
|
* @param milliseconds The number of milliseconds to wait before resolving the promise.
|
|
17
19
|
*
|
|
18
|
-
* @returns A {@link
|
|
20
|
+
* @returns A {@link SmartPromise} that resolves after the specified number of milliseconds.
|
|
19
21
|
*/
|
|
20
|
-
export function delay(milliseconds: number):
|
|
22
|
+
export function delay(milliseconds: number): SmartPromise<void>
|
|
21
23
|
{
|
|
22
|
-
return new
|
|
24
|
+
return new SmartPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
|
@@ -39,11 +41,11 @@ export function delay(milliseconds: number): Promise<void>
|
|
|
39
41
|
*
|
|
40
42
|
* ---
|
|
41
43
|
*
|
|
42
|
-
* @returns A {@link
|
|
44
|
+
* @returns A {@link SmartPromise} that resolves on the next animation frame.
|
|
43
45
|
*/
|
|
44
|
-
export function nextAnimationFrame():
|
|
46
|
+
export function nextAnimationFrame(): SmartPromise<void>
|
|
45
47
|
{
|
|
46
|
-
return new
|
|
48
|
+
return new SmartPromise((resolve) => requestAnimationFrame(() => resolve()));
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
/**
|
|
@@ -64,9 +66,9 @@ export function nextAnimationFrame(): Promise<void>
|
|
|
64
66
|
*
|
|
65
67
|
* ---
|
|
66
68
|
*
|
|
67
|
-
* @returns A {@link
|
|
69
|
+
* @returns A {@link SmartPromise} that resolves on the next microtask.
|
|
68
70
|
*/
|
|
69
|
-
export function yieldToEventLoop():
|
|
71
|
+
export function yieldToEventLoop(): SmartPromise<void>
|
|
70
72
|
{
|
|
71
|
-
return new
|
|
73
|
+
return new SmartPromise((resolve) => setTimeout(resolve));
|
|
72
74
|
}
|
package/src/utils/date.ts
CHANGED
|
@@ -174,8 +174,9 @@ export function dateDifference(start: number | string | Date, end: number | stri
|
|
|
174
174
|
*
|
|
175
175
|
* @returns A {@link SmartIterator} object that generates the dates in the range.
|
|
176
176
|
*/
|
|
177
|
-
export function dateRange(
|
|
178
|
-
:
|
|
177
|
+
export function dateRange(
|
|
178
|
+
start: number | string | Date, end: number | string | Date, step = TimeUnit.Day
|
|
179
|
+
): SmartIterator<Date>
|
|
179
180
|
{
|
|
180
181
|
start = new Date(start);
|
|
181
182
|
end = new Date(end);
|
|
@@ -271,3 +272,14 @@ export function getWeek(date: number | string | Date, firstDay = WeekDay.Sunday)
|
|
|
271
272
|
|
|
272
273
|
return dateRound(new Date(firstDayTime));
|
|
273
274
|
}
|
|
275
|
+
|
|
276
|
+
export function getMonth(date: number | string | Date): Date
|
|
277
|
+
{
|
|
278
|
+
date = new Date(date);
|
|
279
|
+
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth()));
|
|
280
|
+
}
|
|
281
|
+
export function getYear(date: number | string | Date): Date
|
|
282
|
+
{
|
|
283
|
+
date = new Date(date);
|
|
284
|
+
return new Date(Date.UTC(date.getUTCFullYear()));
|
|
285
|
+
}
|
package/src/utils/dom.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { SmartPromise } from "../models/index.js";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Appends a script element to the document body.
|
|
3
5
|
* It can be used to load external scripts dynamically.
|
|
@@ -15,11 +17,11 @@
|
|
|
15
17
|
* @param scriptType The type of the script to load. Default is `"text/javascript"`.
|
|
16
18
|
*
|
|
17
19
|
* @returns
|
|
18
|
-
* A {@link
|
|
20
|
+
* A {@link SmartPromise} that resolves when the script has been loaded successfully or rejects if an error occurs.
|
|
19
21
|
*/
|
|
20
|
-
export function loadScript(scriptUrl: string, scriptType = "text/javascript"):
|
|
22
|
+
export function loadScript(scriptUrl: string, scriptType = "text/javascript"): SmartPromise<void>
|
|
21
23
|
{
|
|
22
|
-
return new
|
|
24
|
+
return new SmartPromise<void>((resolve, reject) =>
|
|
23
25
|
{
|
|
24
26
|
const script = document.createElement("script");
|
|
25
27
|
|