@byloth/core 2.1.7 → 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 +1243 -1306
- 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 +2 -2
- package/src/models/callbacks/publisher.ts +99 -37
- package/src/models/callbacks/switchable-callback.ts +0 -1
- package/src/models/callbacks/types.ts +3 -3
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.8",
|
|
4
4
|
"description": "An unopinionated collection of useful functions and classes that I use widely in all my projects. 🔧",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Core",
|
|
@@ -49,15 +49,15 @@
|
|
|
49
49
|
},
|
|
50
50
|
"types": "src/index.ts",
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@byloth/eslint-config-typescript": "^3.
|
|
53
|
-
"@eslint/compat": "^1.
|
|
54
|
-
"@types/node": "^22.
|
|
52
|
+
"@byloth/eslint-config-typescript": "^3.2.2",
|
|
53
|
+
"@eslint/compat": "^1.4.1",
|
|
54
|
+
"@types/node": "^22.19.0",
|
|
55
55
|
"@vitest/coverage-v8": "^3.2.4",
|
|
56
|
-
"eslint": "^9.
|
|
56
|
+
"eslint": "^9.39.1",
|
|
57
57
|
"husky": "^9.1.7",
|
|
58
|
-
"jsdom": "^
|
|
59
|
-
"typescript": "^5.
|
|
60
|
-
"vite": "^
|
|
58
|
+
"jsdom": "^27.1.0",
|
|
59
|
+
"typescript": "^5.9.3",
|
|
60
|
+
"vite": "^7.2.1",
|
|
61
61
|
"vitest": "^3.2.4"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
export const VERSION = "2.1.
|
|
1
|
+
export const VERSION = "2.1.8";
|
|
2
2
|
|
|
3
3
|
export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
|
|
4
4
|
|
|
5
5
|
export { isBrowser, isNode, isWorker } from "./helpers.js";
|
|
6
|
-
|
|
7
6
|
export {
|
|
8
7
|
AggregatedIterator,
|
|
9
8
|
AggregatedAsyncIterator,
|
|
@@ -507,8 +507,9 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
507
507
|
* @returns
|
|
508
508
|
* A {@link Promise} resolving to a new {@link ReducedIterator} containing the reduced results for each group.
|
|
509
509
|
*/
|
|
510
|
-
public async reduce<A extends PropertyKey>(
|
|
511
|
-
:
|
|
510
|
+
public async reduce<A extends PropertyKey>(
|
|
511
|
+
reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue: MaybePromise<A>
|
|
512
|
+
): Promise<ReducedIterator<K, A>>;
|
|
512
513
|
|
|
513
514
|
/**
|
|
514
515
|
* Reduces the elements of the iterator using a given reducer function.
|
|
@@ -545,8 +546,9 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
545
546
|
* @returns
|
|
546
547
|
* A {@link Promise} resolving to a new {@link ReducedIterator} containing the reduced results for each group.
|
|
547
548
|
*/
|
|
548
|
-
public async reduce<A>(
|
|
549
|
-
:
|
|
549
|
+
public async reduce<A>(
|
|
550
|
+
reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue: (key: K) => MaybePromise<A>
|
|
551
|
+
): Promise<ReducedIterator<K, A>>;
|
|
550
552
|
public async reduce<A>(
|
|
551
553
|
reducer: MaybeAsyncKeyedReducer<K, T, A>, initialValue?: MaybePromise<A> | ((key: K) => MaybePromise<A>)
|
|
552
554
|
): Promise<ReducedIterator<K, A>>
|
|
@@ -810,9 +812,9 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
810
812
|
* A {@link Promise} resolving to a new {@link ReducedIterator} containing
|
|
811
813
|
* the first element that satisfies the condition for each group.
|
|
812
814
|
*/
|
|
813
|
-
public async find<S extends T>(
|
|
814
|
-
:
|
|
815
|
-
|
|
815
|
+
public async find<S extends T>(
|
|
816
|
+
predicate: MaybeAsyncKeyedIteratee<K, T, boolean>
|
|
817
|
+
): Promise<ReducedIterator<K, S | undefined>>;
|
|
816
818
|
public async find(predicate: MaybeAsyncKeyedIteratee<K, T, boolean>): Promise<ReducedIterator<K, T | undefined>>
|
|
817
819
|
{
|
|
818
820
|
const values = new Map<K, [number, T | undefined]>();
|
|
@@ -1020,8 +1022,9 @@ export default class AggregatedAsyncIterator<K extends PropertyKey, T>
|
|
|
1020
1022
|
*
|
|
1021
1023
|
* @returns A new {@link AggregatedAsyncIterator} containing the elements reorganized by the new keys.
|
|
1022
1024
|
*/
|
|
1023
|
-
public reorganizeBy<J extends PropertyKey>(
|
|
1024
|
-
:
|
|
1025
|
+
public reorganizeBy<J extends PropertyKey>(
|
|
1026
|
+
iteratee: MaybeAsyncKeyedIteratee<K, T, J>
|
|
1027
|
+
): AggregatedAsyncIterator<J, T>
|
|
1025
1028
|
{
|
|
1026
1029
|
const elements = this._elements;
|
|
1027
1030
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Callback } from "./types.js";
|
|
2
2
|
|
|
3
3
|
const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R = void>(...args: string[])
|
|
4
|
-
|
|
4
|
+
=> Callback<A, R>;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* An abstract class that can be used to implement callable objects.
|
|
@@ -41,7 +41,7 @@ export default abstract class CallableObject<T extends Callback<any[], any> = Ca
|
|
|
41
41
|
*/
|
|
42
42
|
public constructor()
|
|
43
43
|
{
|
|
44
|
-
super(
|
|
44
|
+
super("return this._invoke(...arguments);");
|
|
45
45
|
|
|
46
46
|
const self = this.bind(this);
|
|
47
47
|
Object.setPrototypeOf(this, self);
|
|
@@ -68,33 +68,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
68
68
|
this._subscribers = new Map();
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
/**
|
|
72
|
-
* Unsubscribes all the subscribers from all the events.
|
|
73
|
-
*
|
|
74
|
-
* ---
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```ts
|
|
78
|
-
* publisher.subscribe("player:spawn", (evt) => { [...] });
|
|
79
|
-
* publisher.subscribe("player:move", (coords) => { [...] });
|
|
80
|
-
* publisher.subscribe("player:move", () => { [...] });
|
|
81
|
-
* publisher.subscribe("player:move", ({ x, y }) => { [...] });
|
|
82
|
-
* publisher.subscribe("player:death", () => { [...] });
|
|
83
|
-
*
|
|
84
|
-
* // All these subscribers are working fine...
|
|
85
|
-
*
|
|
86
|
-
* publisher.clear();
|
|
87
|
-
*
|
|
88
|
-
* // ... but now they're all gone!
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
public clear(): void
|
|
92
|
-
{
|
|
93
|
-
this.publish("__internals__:clear");
|
|
94
|
-
|
|
95
|
-
this._subscribers.clear();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
71
|
/**
|
|
99
72
|
* Creates a new scoped instance of the {@link Publisher} class,
|
|
100
73
|
* which can be used to publish and subscribe events within a specific context.
|
|
@@ -110,8 +83,8 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
110
83
|
* const publisher = new Publisher();
|
|
111
84
|
* const context = publisher.createScope();
|
|
112
85
|
*
|
|
113
|
-
* publisher.subscribe("player:death", () => console.log(
|
|
114
|
-
* context.subscribe("player:spawn", () => console.log(
|
|
86
|
+
* publisher.subscribe("player:death", () => console.log("Player has died."));
|
|
87
|
+
* context.subscribe("player:spawn", () => console.log("Player has spawned."));
|
|
115
88
|
*
|
|
116
89
|
* publisher.publish("player:spawn"); // "Player has spawned."
|
|
117
90
|
* context.publish("player:death"); // * no output *
|
|
@@ -239,7 +212,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
239
212
|
*
|
|
240
213
|
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
241
214
|
*/
|
|
242
|
-
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]):
|
|
215
|
+
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): Callback;
|
|
243
216
|
|
|
244
217
|
/**
|
|
245
218
|
* Subscribes to the wildcard event to listen to all published events.
|
|
@@ -253,21 +226,21 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
253
226
|
* ```ts
|
|
254
227
|
* publisher.subscribe("*", (type, ...args) =>
|
|
255
228
|
* {
|
|
256
|
-
* console.log(`Event
|
|
229
|
+
* console.log(`Event "${type}" was fired with args:`, args);
|
|
257
230
|
* });
|
|
258
231
|
* ```
|
|
259
232
|
*
|
|
260
233
|
* ---
|
|
261
234
|
*
|
|
262
|
-
* @template K The key of the wildcard events map (always
|
|
235
|
+
* @template K The key of the wildcard events map (always `*`).
|
|
263
236
|
*
|
|
264
|
-
* @param event The wildcard event name (
|
|
237
|
+
* @param event The wildcard event name (`*`).
|
|
265
238
|
* @param subscriber The subscriber to execute for all published events.
|
|
266
239
|
*
|
|
267
240
|
* @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
|
|
268
241
|
*/
|
|
269
|
-
public subscribe<K extends keyof S>(event: K & string, subscriber: S[K]):
|
|
270
|
-
public subscribe(event: string, subscriber: Callback<unknown[], unknown>):
|
|
242
|
+
public subscribe<K extends keyof S>(event: K & string, subscriber: S[K]): Callback;
|
|
243
|
+
public subscribe(event: string, subscriber: Callback<unknown[], unknown>): Callback
|
|
271
244
|
{
|
|
272
245
|
const subscribers = this._subscribers.get(event) ?? [];
|
|
273
246
|
subscribers.push(subscriber);
|
|
@@ -326,9 +299,9 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
326
299
|
*
|
|
327
300
|
* ---
|
|
328
301
|
*
|
|
329
|
-
* @template K The key of the wildcard events map (always
|
|
302
|
+
* @template K The key of the wildcard events map (always `*`).
|
|
330
303
|
*
|
|
331
|
-
* @param event The wildcard event name (
|
|
304
|
+
* @param event The wildcard event name (`*`).
|
|
332
305
|
* @param subscriber The wildcard subscriber to remove.
|
|
333
306
|
*/
|
|
334
307
|
public unsubscribe<K extends keyof S>(event: K & string, subscriber: S[K]): void;
|
|
@@ -352,5 +325,94 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
352
325
|
if (subscribers.length === 0) { this._subscribers.delete(event); }
|
|
353
326
|
}
|
|
354
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Unsubscribes all subscribers from a specific event and removes
|
|
330
|
+
* them from being executed when the event is published.
|
|
331
|
+
*
|
|
332
|
+
* ---
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* publisher.subscribe("player:spawn", (evt) => { [...] });
|
|
337
|
+
* publisher.subscribe("player:move", (coords) => { [...] });
|
|
338
|
+
* publisher.subscribe("player:move", () => { [...] });
|
|
339
|
+
* publisher.subscribe("player:move", ({ x, y }) => { [...] });
|
|
340
|
+
* publisher.subscribe("player:death", () => { [...] });
|
|
341
|
+
*
|
|
342
|
+
* // All these subscribers are working fine...
|
|
343
|
+
*
|
|
344
|
+
* publisher.unsubscribeAll("player:move");
|
|
345
|
+
*
|
|
346
|
+
* // ... but now "player:move" subscribers are gone!
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
349
|
+
* ---
|
|
350
|
+
*
|
|
351
|
+
* @template K The key of the map containing the event to clear.
|
|
352
|
+
*
|
|
353
|
+
* @param event The name of the event to unsubscribe all subscribers from.
|
|
354
|
+
*/
|
|
355
|
+
public unsubscribeAll<K extends keyof T>(event: K & string): void;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Unsubscribes all subscribers from the wildcard event and removes
|
|
359
|
+
* them from being executed for all published events.
|
|
360
|
+
*
|
|
361
|
+
* ---
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* publisher.subscribe("player:spawn", (evt) => { [...] });
|
|
366
|
+
* publisher.subscribe("*", (type, ...args) => { [...] });
|
|
367
|
+
* publisher.subscribe("*", (type, arg1, arg2, arg3) => { [...] });
|
|
368
|
+
* publisher.subscribe("*", (_, arg, ...rest) => { [...] });
|
|
369
|
+
* publisher.subscribe("player:death", () => { [...] });
|
|
370
|
+
*
|
|
371
|
+
* // All these subscribers are working fine...
|
|
372
|
+
*
|
|
373
|
+
* publisher.unsubscribeAll("*");
|
|
374
|
+
*
|
|
375
|
+
* // ... but now wildcard subscribers are gone!
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* ---
|
|
379
|
+
*
|
|
380
|
+
* @template K The key of the wildcard events map (`*`).
|
|
381
|
+
*
|
|
382
|
+
* @param event The wildcard event name (`*`).
|
|
383
|
+
*/
|
|
384
|
+
public unsubscribeAll<K extends keyof S>(event: K & string): void;
|
|
385
|
+
public unsubscribeAll(event: string): void
|
|
386
|
+
{
|
|
387
|
+
this._subscribers.delete(event);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Unsubscribes all the subscribers from all the events.
|
|
392
|
+
*
|
|
393
|
+
* ---
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```ts
|
|
397
|
+
* publisher.subscribe("player:spawn", (evt) => { [...] });
|
|
398
|
+
* publisher.subscribe("player:move", (coords) => { [...] });
|
|
399
|
+
* publisher.subscribe("*", () => { [...] });
|
|
400
|
+
* publisher.subscribe("player:move", ({ x, y }) => { [...] });
|
|
401
|
+
* publisher.subscribe("player:death", () => { [...] });
|
|
402
|
+
*
|
|
403
|
+
* // All these subscribers are working fine...
|
|
404
|
+
*
|
|
405
|
+
* publisher.clear();
|
|
406
|
+
*
|
|
407
|
+
* // ... but now they're all gone!
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
public clear(): void
|
|
411
|
+
{
|
|
412
|
+
this.publish("__internals__:clear");
|
|
413
|
+
|
|
414
|
+
this._subscribers.clear();
|
|
415
|
+
}
|
|
416
|
+
|
|
355
417
|
public readonly [Symbol.toStringTag]: string = "Publisher";
|
|
356
418
|
}
|
|
@@ -87,14 +87,14 @@ export type InternalsEventsMap = Record<`__${string}__:${string}`, Callback<unkn
|
|
|
87
87
|
*
|
|
88
88
|
* publisher.subscribe("*", (type: string, ...args: unknown[]) =>
|
|
89
89
|
* {
|
|
90
|
-
* console.log(`Event
|
|
90
|
+
* console.log(`Event "${type}" was fired with args:`, args));
|
|
91
91
|
* });
|
|
92
92
|
*
|
|
93
93
|
* publisher.publish("player:move", { x: 10, y: 20 }); // "Event `player:move` was fired with args: [{ x: 10, y: 20 }]"
|
|
94
94
|
* publisher.publish("player:death"); // "Event `player:death` was fired with args: []"
|
|
95
95
|
* ```
|
|
96
96
|
*/
|
|
97
|
-
export interface WildcardEventsMap { "*": (type: string, ...args: unknown[]) => void
|
|
97
|
+
export interface WildcardEventsMap { "*": (type: string, ...args: unknown[]) => void }
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* An utility type that represents a {@link Publisher} object that can be published to.
|
|
@@ -178,7 +178,7 @@ export interface Subscribable<T extends CallbackMap<T> = CallbackMap>
|
|
|
178
178
|
*
|
|
179
179
|
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
180
180
|
*/
|
|
181
|
-
subscribe<K extends keyof T>(event: K & string, subscriber: T[K]):
|
|
181
|
+
subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): Callback;
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Unsubscribes from an event and removes a subscriber from being executed when the event is published.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Publisher from "../callbacks/publisher.js";
|
|
2
|
-
import type { Subscribable } from "../types.js";
|
|
2
|
+
import type { Callback, Subscribable } from "../types.js";
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
5
|
import type SetView from "./set-view.js";
|
|
@@ -173,8 +173,9 @@ export default class MapView<K, V> extends Map<K, V> implements Subscribable<Map
|
|
|
173
173
|
*
|
|
174
174
|
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
175
175
|
*/
|
|
176
|
-
public subscribe<T extends keyof MapViewEventsMap<K, V>>(
|
|
177
|
-
:
|
|
176
|
+
public subscribe<T extends keyof MapViewEventsMap<K, V>>(
|
|
177
|
+
event: T & string, subscriber: MapViewEventsMap<K, V>[T]
|
|
178
|
+
): Callback
|
|
178
179
|
{
|
|
179
180
|
return this._publisher.subscribe(event, subscriber);
|
|
180
181
|
}
|
|
@@ -203,8 +204,9 @@ export default class MapView<K, V> extends Map<K, V> implements Subscribable<Map
|
|
|
203
204
|
* @param event The name of the event to unsubscribe from.
|
|
204
205
|
* @param subscriber The callback to remove from the event.
|
|
205
206
|
*/
|
|
206
|
-
public unsubscribe<T extends keyof MapViewEventsMap<K, V>>(
|
|
207
|
-
:
|
|
207
|
+
public unsubscribe<T extends keyof MapViewEventsMap<K, V>>(
|
|
208
|
+
event: T & string, subscriber: MapViewEventsMap<K, V>[T]
|
|
209
|
+
): void
|
|
208
210
|
{
|
|
209
211
|
this._publisher.unsubscribe(event, subscriber);
|
|
210
212
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Publisher from "../callbacks/publisher.js";
|
|
2
|
-
import type { Subscribable } from "../types.js";
|
|
2
|
+
import type { Callback, Subscribable } from "../types.js";
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
5
5
|
import type MapView from "./map-view.js";
|
|
@@ -167,8 +167,9 @@ export default class SetView<T> extends Set<T> implements Subscribable<SetViewEv
|
|
|
167
167
|
*
|
|
168
168
|
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
169
169
|
*/
|
|
170
|
-
public subscribe<K extends keyof SetViewEventsMap<T>>(
|
|
171
|
-
:
|
|
170
|
+
public subscribe<K extends keyof SetViewEventsMap<T>>(
|
|
171
|
+
event: K & string, subscriber: SetViewEventsMap<T>[K]
|
|
172
|
+
): Callback
|
|
172
173
|
{
|
|
173
174
|
return this._publisher.subscribe(event, subscriber);
|
|
174
175
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Callback } from "../types.js";
|
|
2
|
+
|
|
1
3
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
4
|
import type MapView from "./map-view.js";
|
|
3
5
|
|
|
@@ -66,7 +68,7 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
66
68
|
*
|
|
67
69
|
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
68
70
|
*/
|
|
69
|
-
subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]):
|
|
71
|
+
subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T & string, callback: MapViewEventsMap<K, V>[T]): Callback;
|
|
70
72
|
|
|
71
73
|
/**
|
|
72
74
|
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
@@ -92,7 +94,7 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
92
94
|
* @param event The name of the event to unsubscribe from.
|
|
93
95
|
* @param callback The callback to remove from the event.
|
|
94
96
|
*/
|
|
95
|
-
unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, callback: MapViewEventsMap<K, V>[T]): void;
|
|
97
|
+
unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T & string, callback: MapViewEventsMap<K, V>[T]): void;
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
/**
|
|
@@ -155,7 +157,7 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
155
157
|
*
|
|
156
158
|
* @returns A function that can be used to unsubscribe the callback from the event.
|
|
157
159
|
*/
|
|
158
|
-
subscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]):
|
|
160
|
+
subscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, callback: SetViewEventsMap<T>[K]): Callback;
|
|
159
161
|
|
|
160
162
|
/**
|
|
161
163
|
* Unsubscribes from an event and removes a callback from being executed when the event is published.
|
|
@@ -181,5 +183,5 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
181
183
|
* @param event The name of the event to unsubscribe from.
|
|
182
184
|
* @param callback The callback to remove from the event.
|
|
183
185
|
*/
|
|
184
|
-
unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K, callback: SetViewEventsMap<T>[K]): void;
|
|
186
|
+
unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, callback: SetViewEventsMap<T>[K]): void;
|
|
185
187
|
}
|
|
@@ -212,7 +212,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
212
212
|
|
|
213
213
|
next = [yield result.value];
|
|
214
214
|
}
|
|
215
|
-
|
|
216
215
|
})();
|
|
217
216
|
}
|
|
218
217
|
}
|
|
@@ -232,7 +231,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
232
231
|
|
|
233
232
|
yield result.value;
|
|
234
233
|
}
|
|
235
|
-
|
|
236
234
|
})();
|
|
237
235
|
}
|
|
238
236
|
else
|
|
@@ -248,7 +246,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
248
246
|
|
|
249
247
|
next = [yield result.value];
|
|
250
248
|
}
|
|
251
|
-
|
|
252
249
|
})();
|
|
253
250
|
}
|
|
254
251
|
}
|
|
@@ -178,8 +178,7 @@ export default class JSONStorage
|
|
|
178
178
|
* @returns The value with the specified key or the default value if the key doesn't exist.
|
|
179
179
|
*/
|
|
180
180
|
public get<T extends JSONValue>(key: string, defaultValue?: T, persistent?: boolean): T | undefined;
|
|
181
|
-
public get<T extends JSONValue>(key: string, defaultValue?: T, persistent = this._preferPersistence)
|
|
182
|
-
: T | undefined
|
|
181
|
+
public get<T extends JSONValue>(key: string, defaultValue?: T, persistent = this._preferPersistence): T | undefined
|
|
183
182
|
{
|
|
184
183
|
const storage = persistent ? this._persistent : this._volatile;
|
|
185
184
|
|
|
@@ -103,9 +103,9 @@ export default class PromiseQueue extends SmartPromise<void>
|
|
|
103
103
|
*
|
|
104
104
|
* @param promise A `DeferredPromise<void, T>` instance to enqueue.
|
|
105
105
|
*
|
|
106
|
-
* @returns A {@link
|
|
106
|
+
* @returns A {@link SmartPromise} that resolves to the value of the enqueued promise.
|
|
107
107
|
*/
|
|
108
|
-
public enqueue<T>(promise: DeferredPromise<void, T>):
|
|
108
|
+
public enqueue<T>(promise: DeferredPromise<void, T>): SmartPromise<T>;
|
|
109
109
|
|
|
110
110
|
/**
|
|
111
111
|
* Enqueues a {@link DeferredPromise} into the queue with an optional timeout.
|
|
@@ -156,9 +156,9 @@ export default class PromiseQueue extends SmartPromise<void>
|
|
|
156
156
|
*
|
|
157
157
|
* @param executor A callback that returns a `MaybePromise<T>` value to enqueue.
|
|
158
158
|
*
|
|
159
|
-
* @returns A {@link
|
|
159
|
+
* @returns A {@link SmartPromise} that resolves to the value of the enqueued executor.
|
|
160
160
|
*/
|
|
161
|
-
public enqueue<T>(executor: Callback<[], MaybePromise<T>>):
|
|
161
|
+
public enqueue<T>(executor: Callback<[], MaybePromise<T>>): SmartPromise<T>;
|
|
162
162
|
|
|
163
163
|
/**
|
|
164
164
|
* Enqueues a callback that returns a {@link MaybePromise}
|
|
@@ -188,8 +188,9 @@ export default class PromiseQueue extends SmartPromise<void>
|
|
|
188
188
|
* with a `TimeoutException` if the operation takes longer than the specified timeout.
|
|
189
189
|
*/
|
|
190
190
|
public enqueue<T>(executor: Callback<[], MaybePromise<T>>, timeout?: number): TimedPromise<T>;
|
|
191
|
-
public enqueue<T>(
|
|
192
|
-
:
|
|
191
|
+
public enqueue<T>(
|
|
192
|
+
executor: DeferredPromise<void, T> | Callback<[], MaybePromise<T>>, timeout?: number
|
|
193
|
+
): SmartPromise<T> | TimedPromise<T>
|
|
193
194
|
{
|
|
194
195
|
this._count += 1;
|
|
195
196
|
|
|
@@ -215,7 +216,7 @@ export default class PromiseQueue extends SmartPromise<void>
|
|
|
215
216
|
|
|
216
217
|
if (timeout) { return new TimedPromise<T>(_executor, timeout); }
|
|
217
218
|
|
|
218
|
-
return new
|
|
219
|
+
return new SmartPromise<T>(_executor);
|
|
219
220
|
}
|
|
220
221
|
|
|
221
222
|
public override readonly [Symbol.toStringTag]: string = "PromiseQueue";
|
|
@@ -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
|
}
|