@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
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.
|
|
@@ -30,10 +30,10 @@ const SmartFunction = (Function as unknown) as new<A extends unknown[] = [], R =
|
|
|
30
30
|
*
|
|
31
31
|
* @template T
|
|
32
32
|
* The type signature of the callback function.
|
|
33
|
-
* It must be a function. Default is `(
|
|
33
|
+
* It must be a function. Default is `() => void`.
|
|
34
34
|
*/
|
|
35
35
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
export default abstract class CallableObject<T extends Callback<any[], any> =
|
|
36
|
+
export default abstract class CallableObject<T extends Callback<any[], any> = Callback>
|
|
37
37
|
extends SmartFunction<Parameters<T>, ReturnType<T>>
|
|
38
38
|
{
|
|
39
39
|
/**
|
|
@@ -41,7 +41,7 @@ export default abstract class CallableObject<T extends Callback<any[], any> = ()
|
|
|
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);
|
|
@@ -2,8 +2,8 @@ import { ReferenceException } from "../exceptions/index.js";
|
|
|
2
2
|
|
|
3
3
|
import type { Callback, CallbackMap, InternalsEventsMap, WildcardEventsMap } from "./types.js";
|
|
4
4
|
|
|
5
|
-
type
|
|
6
|
-
type
|
|
5
|
+
type P = InternalsEventsMap;
|
|
6
|
+
type S = WildcardEventsMap & InternalsEventsMap;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* A class implementing the
|
|
@@ -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 *
|
|
@@ -120,29 +93,29 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
120
93
|
* ---
|
|
121
94
|
*
|
|
122
95
|
* @template U
|
|
123
|
-
* A map containing the names of the emittable events and
|
|
124
|
-
* related callback signatures that can be subscribed to them.
|
|
125
|
-
* Default is `
|
|
96
|
+
* A map containing the additional names of the emittable events and
|
|
97
|
+
* the related callback signatures that can be subscribed to them.
|
|
98
|
+
* Default is `{ }`.
|
|
126
99
|
*
|
|
127
100
|
* @return
|
|
128
101
|
* A new instance of the {@link Publisher} class that can be
|
|
129
102
|
* used to publish and subscribe events within a specific context.
|
|
130
103
|
*/
|
|
131
|
-
|
|
104
|
+
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
106
|
+
public createScope<U extends CallbackMap<U> = { }>(): Publisher<U & T>
|
|
132
107
|
{
|
|
133
|
-
const scope = new Publisher
|
|
108
|
+
const scope = new Publisher();
|
|
134
109
|
|
|
135
110
|
this.subscribe("__internals__:clear", () => scope.clear());
|
|
136
|
-
this.subscribe("*", (event, ...args): void =>
|
|
137
|
-
{
|
|
138
|
-
scope.publish(event as keyof U & string, ...args as Parameters<U[keyof U]>);
|
|
139
|
-
});
|
|
111
|
+
this.subscribe("*", (event, ...args): void => { scope.publish(event, ...args); });
|
|
140
112
|
|
|
141
113
|
return scope;
|
|
142
114
|
}
|
|
143
115
|
|
|
144
116
|
/**
|
|
145
|
-
* Publishes an event to all the subscribers.
|
|
117
|
+
* Publishes an event to all the subscribers.
|
|
118
|
+
* This events will trigger the wildcard listeners as well, if any.
|
|
146
119
|
*
|
|
147
120
|
* ---
|
|
148
121
|
*
|
|
@@ -170,7 +143,8 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
170
143
|
* Publishes an internal event to all the subscribers.
|
|
171
144
|
*
|
|
172
145
|
* Internal events follow the pattern `__${string}__:${string}` and are used for internal
|
|
173
|
-
* communication within the publisher system. These events won't trigger wildcard listeners.
|
|
146
|
+
* communication within the publisher system. These events won't trigger wildcard listeners.
|
|
147
|
+
* Please note to use this method only if you know what you are doing.
|
|
174
148
|
*
|
|
175
149
|
* ---
|
|
176
150
|
*
|
|
@@ -189,7 +163,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
189
163
|
*
|
|
190
164
|
* @returns An array containing the return values of all the subscribers.
|
|
191
165
|
*/
|
|
192
|
-
public publish<K extends keyof
|
|
166
|
+
public publish<K extends keyof P>(event: K & string, ...args: Parameters<P[K]>): ReturnType<P[K]>[];
|
|
193
167
|
public publish(event: string, ...args: unknown[]): unknown[]
|
|
194
168
|
{
|
|
195
169
|
let results: unknown[];
|
|
@@ -238,33 +212,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
238
212
|
*
|
|
239
213
|
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
240
214
|
*/
|
|
241
|
-
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]):
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Subscribes to an internal event and adds a subscriber to be executed when the event is published.
|
|
245
|
-
*
|
|
246
|
-
* Internal events follow the pattern `__${string}__:${string}` and
|
|
247
|
-
* are used for internal communication within the publisher system.
|
|
248
|
-
* Please note to use this method only if you know what you are doing.
|
|
249
|
-
*
|
|
250
|
-
* ---
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```ts
|
|
254
|
-
* publisher.subscribe("__internals__:clear", () => console.log("All subscribers have been cleared."));
|
|
255
|
-
* publisher.clear(); // "All subscribers have been cleared."
|
|
256
|
-
* ```
|
|
257
|
-
*
|
|
258
|
-
* ---
|
|
259
|
-
*
|
|
260
|
-
* @template K The key of the internal events map containing the callback signature to subscribe.
|
|
261
|
-
*
|
|
262
|
-
* @param event The name of the internal event to subscribe to.
|
|
263
|
-
* @param subscriber The subscriber to execute when the internal event is published.
|
|
264
|
-
*
|
|
265
|
-
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
266
|
-
*/
|
|
267
|
-
public subscribe<K extends keyof I>(event: K & string, subscriber: I[K]): () => void;
|
|
215
|
+
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): Callback;
|
|
268
216
|
|
|
269
217
|
/**
|
|
270
218
|
* Subscribes to the wildcard event to listen to all published events.
|
|
@@ -278,21 +226,21 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
278
226
|
* ```ts
|
|
279
227
|
* publisher.subscribe("*", (type, ...args) =>
|
|
280
228
|
* {
|
|
281
|
-
* console.log(`Event
|
|
229
|
+
* console.log(`Event "${type}" was fired with args:`, args);
|
|
282
230
|
* });
|
|
283
231
|
* ```
|
|
284
232
|
*
|
|
285
233
|
* ---
|
|
286
234
|
*
|
|
287
|
-
* @template K The key of the wildcard events map (always
|
|
235
|
+
* @template K The key of the wildcard events map (always `*`).
|
|
288
236
|
*
|
|
289
|
-
* @param event The wildcard event name (
|
|
237
|
+
* @param event The wildcard event name (`*`).
|
|
290
238
|
* @param subscriber The subscriber to execute for all published events.
|
|
291
239
|
*
|
|
292
240
|
* @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
|
|
293
241
|
*/
|
|
294
|
-
public subscribe<K extends keyof
|
|
295
|
-
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
|
|
296
244
|
{
|
|
297
245
|
const subscribers = this._subscribers.get(event) ?? [];
|
|
298
246
|
subscribers.push(subscriber);
|
|
@@ -334,31 +282,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
334
282
|
*/
|
|
335
283
|
public unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
|
|
336
284
|
|
|
337
|
-
/**
|
|
338
|
-
* Unsubscribes from an internal event and removes a subscriber from being executed when the event is published.
|
|
339
|
-
*
|
|
340
|
-
* Internal events follow the pattern `__${string}__:${string}` and
|
|
341
|
-
* are used for internal communication within the publisher system.
|
|
342
|
-
*
|
|
343
|
-
* ---
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```ts
|
|
347
|
-
* const onClear = () => console.log("Publisher cleared");
|
|
348
|
-
*
|
|
349
|
-
* publisher.subscribe("__internals__:clear", onClear);
|
|
350
|
-
* publisher.unsubscribe("__internals__:clear", onClear);
|
|
351
|
-
* ```
|
|
352
|
-
*
|
|
353
|
-
* ---
|
|
354
|
-
*
|
|
355
|
-
* @template K The key of the internal events map containing the callback signature to unsubscribe.
|
|
356
|
-
*
|
|
357
|
-
* @param event The name of the internal event to unsubscribe from.
|
|
358
|
-
* @param subscriber The subscriber to remove from the internal event.
|
|
359
|
-
*/
|
|
360
|
-
public unsubscribe<K extends keyof I>(event: K & string, subscriber: I[K]): void;
|
|
361
|
-
|
|
362
285
|
/**
|
|
363
286
|
* Unsubscribes from the wildcard event and removes a subscriber from being executed for all events.
|
|
364
287
|
*
|
|
@@ -376,12 +299,12 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
376
299
|
*
|
|
377
300
|
* ---
|
|
378
301
|
*
|
|
379
|
-
* @template K The key of the wildcard events map (always
|
|
302
|
+
* @template K The key of the wildcard events map (always `*`).
|
|
380
303
|
*
|
|
381
|
-
* @param event The wildcard event name (
|
|
304
|
+
* @param event The wildcard event name (`*`).
|
|
382
305
|
* @param subscriber The wildcard subscriber to remove.
|
|
383
306
|
*/
|
|
384
|
-
public unsubscribe<K extends keyof
|
|
307
|
+
public unsubscribe<K extends keyof S>(event: K & string, subscriber: S[K]): void;
|
|
385
308
|
public unsubscribe(event: string, subscriber: Callback<unknown[], unknown>): void
|
|
386
309
|
{
|
|
387
310
|
const subscribers = this._subscribers.get(event);
|
|
@@ -402,5 +325,94 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
|
402
325
|
if (subscribers.length === 0) { this._subscribers.delete(event); }
|
|
403
326
|
}
|
|
404
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
|
+
|
|
405
417
|
public readonly [Symbol.toStringTag]: string = "Publisher";
|
|
406
418
|
}
|
|
@@ -27,7 +27,7 @@ const Disabler = () => { /* ... */ };
|
|
|
27
27
|
*
|
|
28
28
|
* ---
|
|
29
29
|
*
|
|
30
|
-
* @template T The type signature of the callback. Default is `(
|
|
30
|
+
* @template T The type signature of the callback. Default is `() => void`.
|
|
31
31
|
*/
|
|
32
32
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
33
|
export default class SwitchableCallback<T extends Callback<any[], any> = Callback> extends CallableObject<T>
|
|
@@ -130,7 +130,6 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
|
|
|
130
130
|
"The `SwitchableCallback` has no callback defined yet. " +
|
|
131
131
|
"Did you forget to call the `register` method?"
|
|
132
132
|
);
|
|
133
|
-
|
|
134
133
|
}) as unknown) as T;
|
|
135
134
|
}
|
|
136
135
|
|
|
@@ -69,7 +69,7 @@ export type CallbackMap<T = Record<string, Callback<unknown[], unknown>>> = { [K
|
|
|
69
69
|
* publisher.clear(); // "Publisher cleared"
|
|
70
70
|
* ```
|
|
71
71
|
*/
|
|
72
|
-
export type InternalsEventsMap = Record<`__${string}__:${string}`,
|
|
72
|
+
export type InternalsEventsMap = Record<`__${string}__:${string}`, Callback<unknown[], unknown>>;
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* An utility interface that defines a wildcard event for listening to all events.
|
|
@@ -87,14 +87,14 @@ export type InternalsEventsMap = Record<`__${string}__:${string}`, (...args: unk
|
|
|
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";
|