@byloth/core 2.1.5 → 2.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.esm.js +26 -95
- package/dist/core.esm.js.map +1 -1
- package/dist/core.global.js +1 -1
- package/dist/core.global.js.map +1 -1
- package/dist/core.umd.cjs +1 -1
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +4 -2
- package/src/models/callbacks/publisher.ts +149 -24
- package/src/models/callbacks/switchable-callback.ts +6 -6
- package/src/models/callbacks/types.ts +52 -20
- package/src/models/collections/map-view.ts +14 -15
- package/src/models/collections/set-view.ts +13 -16
- package/src/models/collections/types.ts +6 -6
- package/src/models/iterators/smart-async-iterator.ts +1 -1
- package/src/models/iterators/smart-iterator.ts +1 -1
- package/src/models/timers/clock.ts +3 -3
- package/src/models/timers/countdown.ts +4 -4
- package/src/models/timers/game-loop.ts +4 -4
- package/src/models/types.ts +9 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@byloth/core",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
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",
|
|
@@ -52,13 +52,13 @@
|
|
|
52
52
|
"@byloth/eslint-config-typescript": "^3.1.0",
|
|
53
53
|
"@eslint/compat": "^1.3.0",
|
|
54
54
|
"@types/node": "^22.15.32",
|
|
55
|
-
"@vitest/coverage-v8": "^3.2.
|
|
55
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
56
56
|
"eslint": "^9.29.0",
|
|
57
57
|
"husky": "^9.1.7",
|
|
58
58
|
"jsdom": "^26.1.0",
|
|
59
59
|
"typescript": "^5.8.3",
|
|
60
60
|
"vite": "^6.3.5",
|
|
61
|
-
"vitest": "^3.2.
|
|
61
|
+
"vitest": "^3.2.4"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"dev": "vite",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const VERSION = "2.1.
|
|
1
|
+
export const VERSION = "2.1.6";
|
|
2
2
|
|
|
3
3
|
export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
|
|
4
4
|
|
|
@@ -53,6 +53,7 @@ export type {
|
|
|
53
53
|
CallbackMap,
|
|
54
54
|
FulfilledHandler,
|
|
55
55
|
GeneratorFunction,
|
|
56
|
+
InternalsEventsMap,
|
|
56
57
|
Iteratee,
|
|
57
58
|
IteratorLike,
|
|
58
59
|
JSONArray,
|
|
@@ -79,7 +80,8 @@ export type {
|
|
|
79
80
|
RejectedHandler,
|
|
80
81
|
SetViewEventsMap,
|
|
81
82
|
Subscribable,
|
|
82
|
-
TypeGuardPredicate
|
|
83
|
+
TypeGuardPredicate,
|
|
84
|
+
WildcardEventsMap
|
|
83
85
|
|
|
84
86
|
} from "./models/types.js";
|
|
85
87
|
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { ReferenceException } from "../exceptions/index.js";
|
|
2
2
|
|
|
3
|
-
import type { Callback, CallbackMap,
|
|
3
|
+
import type { Callback, CallbackMap, InternalsEventsMap, WildcardEventsMap } from "./types.js";
|
|
4
|
+
|
|
5
|
+
type I = InternalsEventsMap;
|
|
6
|
+
type W = WildcardEventsMap;
|
|
4
7
|
|
|
5
8
|
/**
|
|
6
9
|
* A class implementing the
|
|
@@ -39,10 +42,8 @@ import type { Callback, CallbackMap, WithWildcard } from "./types.js";
|
|
|
39
42
|
* A map containing the names of the emittable events and the
|
|
40
43
|
* related callback signatures that can be subscribed to them.
|
|
41
44
|
* Default is `Record<string, (...args: unknown[]) => unknown>`.
|
|
42
|
-
*
|
|
43
|
-
* @template E An utility type that extends the `T` map with a wildcard event.
|
|
44
45
|
*/
|
|
45
|
-
export default class Publisher<T extends CallbackMap<T> = CallbackMap
|
|
46
|
+
export default class Publisher<T extends CallbackMap<T> = CallbackMap>
|
|
46
47
|
{
|
|
47
48
|
/**
|
|
48
49
|
* A map containing all the subscribers for each event.
|
|
@@ -89,7 +90,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
89
90
|
*/
|
|
90
91
|
public clear(): void
|
|
91
92
|
{
|
|
92
|
-
// @ts-expect-error It's an internal event, not part of the public API.
|
|
93
93
|
this.publish("__internals__:clear");
|
|
94
94
|
|
|
95
95
|
this._subscribers.clear();
|
|
@@ -110,10 +110,10 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
110
110
|
* const publisher = new Publisher();
|
|
111
111
|
* const context = publisher.createScope();
|
|
112
112
|
*
|
|
113
|
-
* publisher.subscribe("player:death", () =>
|
|
114
|
-
* context.subscribe("player:spawn", () =>
|
|
113
|
+
* publisher.subscribe("player:death", () => console.log(`Player has died.`));
|
|
114
|
+
* context.subscribe("player:spawn", () => console.log(`Player has spawned.`));
|
|
115
115
|
*
|
|
116
|
-
* publisher.publish("player:spawn"); // Player has spawned.
|
|
116
|
+
* publisher.publish("player:spawn"); // "Player has spawned."
|
|
117
117
|
* context.publish("player:death"); // * no output *
|
|
118
118
|
* ```
|
|
119
119
|
*
|
|
@@ -124,24 +124,19 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
124
124
|
* related callback signatures that can be subscribed to them.
|
|
125
125
|
* Default is `T`.
|
|
126
126
|
*
|
|
127
|
-
* @template X An utility type that extends the `U` map with a wildcard event.
|
|
128
|
-
*
|
|
129
127
|
* @return
|
|
130
128
|
* A new instance of the {@link Publisher} class that can be
|
|
131
129
|
* used to publish and subscribe events within a specific context.
|
|
132
130
|
*/
|
|
133
|
-
public createScope<U extends T = T
|
|
131
|
+
public createScope<U extends T = T>(): Publisher<U>
|
|
134
132
|
{
|
|
135
|
-
const scope = new Publisher<U
|
|
136
|
-
|
|
137
|
-
const propagator = (event: (keyof T) & string, ...args: Parameters<T[keyof T]>): void =>
|
|
138
|
-
{
|
|
139
|
-
scope.publish(event, ...args);
|
|
140
|
-
};
|
|
133
|
+
const scope = new Publisher<U>();
|
|
141
134
|
|
|
142
|
-
// @ts-expect-error It's an internal event, not part of the public API.
|
|
143
135
|
this.subscribe("__internals__:clear", () => scope.clear());
|
|
144
|
-
this.subscribe("*",
|
|
136
|
+
this.subscribe("*", (event, ...args): void =>
|
|
137
|
+
{
|
|
138
|
+
scope.publish(event as keyof U & string, ...args as Parameters<U[keyof U]>);
|
|
139
|
+
});
|
|
145
140
|
|
|
146
141
|
return scope;
|
|
147
142
|
}
|
|
@@ -169,14 +164,40 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
169
164
|
*
|
|
170
165
|
* @returns An array containing the return values of all the subscribers.
|
|
171
166
|
*/
|
|
172
|
-
public publish<K extends keyof T>(event: K & string, ...args: Parameters<T[K]>): ReturnType<T[K]>[]
|
|
167
|
+
public publish<K extends keyof T>(event: K & string, ...args: Parameters<T[K]>): ReturnType<T[K]>[];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Publishes an internal event to all the subscribers.
|
|
171
|
+
*
|
|
172
|
+
* 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.
|
|
174
|
+
*
|
|
175
|
+
* ---
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* publisher.subscribe("__internals__:clear", () => console.log("Clearing..."));
|
|
180
|
+
* publisher.publish("__internals__:clear"); // "Clearing..."
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* ---
|
|
184
|
+
*
|
|
185
|
+
* @template K The key of the internal events map containing the callback signature to publish.
|
|
186
|
+
*
|
|
187
|
+
* @param event The name of the internal event to publish.
|
|
188
|
+
* @param args The arguments to pass to the subscribers.
|
|
189
|
+
*
|
|
190
|
+
* @returns An array containing the return values of all the subscribers.
|
|
191
|
+
*/
|
|
192
|
+
public publish<K extends keyof I>(event: K & string, ...args: Parameters<I[K]>): ReturnType<I[K]>[];
|
|
193
|
+
public publish(event: string, ...args: unknown[]): unknown[]
|
|
173
194
|
{
|
|
174
|
-
let results:
|
|
195
|
+
let results: unknown[];
|
|
175
196
|
let subscribers = this._subscribers.get(event);
|
|
176
197
|
if (subscribers)
|
|
177
198
|
{
|
|
178
199
|
results = subscribers.slice()
|
|
179
|
-
.map((subscriber) => subscriber(...args))
|
|
200
|
+
.map((subscriber) => subscriber(...args));
|
|
180
201
|
}
|
|
181
202
|
else { results = []; }
|
|
182
203
|
|
|
@@ -217,7 +238,61 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
217
238
|
*
|
|
218
239
|
* @returns A function that can be used to unsubscribe the subscriber from the event.
|
|
219
240
|
*/
|
|
220
|
-
public subscribe<K extends keyof
|
|
241
|
+
public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): () => void;
|
|
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;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Subscribes to the wildcard event to listen to all published events.
|
|
271
|
+
*
|
|
272
|
+
* The wildcard subscriber will be called for every event published, receiving
|
|
273
|
+
* the event type as the first parameter followed by all event arguments.
|
|
274
|
+
*
|
|
275
|
+
* ---
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* publisher.subscribe("*", (type, ...args) =>
|
|
280
|
+
* {
|
|
281
|
+
* console.log(`Event \`${type}\` was fired with args:`, args);
|
|
282
|
+
* });
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* ---
|
|
286
|
+
*
|
|
287
|
+
* @template K The key of the wildcard events map (always "*").
|
|
288
|
+
*
|
|
289
|
+
* @param event The wildcard event name ("*").
|
|
290
|
+
* @param subscriber The subscriber to execute for all published events.
|
|
291
|
+
*
|
|
292
|
+
* @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
|
|
293
|
+
*/
|
|
294
|
+
public subscribe<K extends keyof W>(event: K & string, subscriber: W[K]): () => void;
|
|
295
|
+
public subscribe(event: string, subscriber: Callback<unknown[], unknown>): () => void
|
|
221
296
|
{
|
|
222
297
|
const subscribers = this._subscribers.get(event) ?? [];
|
|
223
298
|
subscribers.push(subscriber);
|
|
@@ -257,7 +332,57 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
|
|
|
257
332
|
* @param event The name of the event to unsubscribe from.
|
|
258
333
|
* @param subscriber The subscriber to remove from the event.
|
|
259
334
|
*/
|
|
260
|
-
public unsubscribe<K extends keyof
|
|
335
|
+
public unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
|
|
336
|
+
|
|
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
|
+
/**
|
|
363
|
+
* Unsubscribes from the wildcard event and removes a subscriber from being executed for all events.
|
|
364
|
+
*
|
|
365
|
+
* This removes a previously registered wildcard listener that was capturing all published events.
|
|
366
|
+
*
|
|
367
|
+
* ---
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* const wildcardHandler = (type: string, ...args: unknown[]) => console.log(type, args);
|
|
372
|
+
*
|
|
373
|
+
* publisher.subscribe("*", wildcardHandler);
|
|
374
|
+
* publisher.unsubscribe("*", wildcardHandler);
|
|
375
|
+
* ```
|
|
376
|
+
*
|
|
377
|
+
* ---
|
|
378
|
+
*
|
|
379
|
+
* @template K The key of the wildcard events map (always "*").
|
|
380
|
+
*
|
|
381
|
+
* @param event The wildcard event name ("*").
|
|
382
|
+
* @param subscriber The wildcard subscriber to remove.
|
|
383
|
+
*/
|
|
384
|
+
public unsubscribe<K extends keyof W>(event: K & string, subscriber: W[K]): void;
|
|
385
|
+
public unsubscribe(event: string, subscriber: Callback<unknown[], unknown>): void
|
|
261
386
|
{
|
|
262
387
|
const subscribers = this._subscribers.get(event);
|
|
263
388
|
if (!(subscribers))
|
|
@@ -20,9 +20,9 @@ const Disabler = () => { /* ... */ };
|
|
|
20
20
|
* onPointerMove.register("released", () => { [...] });
|
|
21
21
|
* onPointerMove.register("pressed", () => { [...] });
|
|
22
22
|
*
|
|
23
|
-
* window.addEventListener("pointerdown", () =>
|
|
23
|
+
* window.addEventListener("pointerdown", () => onPointerMove.switch("pressed"));
|
|
24
24
|
* window.addEventListener("pointermove", onPointerMove);
|
|
25
|
-
* window.addEventListener("pointerup", () =>
|
|
25
|
+
* window.addEventListener("pointerup", () => onPointerMove.switch("released"));
|
|
26
26
|
* ```
|
|
27
27
|
*
|
|
28
28
|
* ---
|
|
@@ -153,7 +153,7 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
|
|
|
153
153
|
*
|
|
154
154
|
* @example
|
|
155
155
|
* ```ts
|
|
156
|
-
* window.addEventListener("pointerdown", () =>
|
|
156
|
+
* window.addEventListener("pointerdown", () => onPointerMove.enable());
|
|
157
157
|
* window.addEventListener("pointermove", onPointerMove);
|
|
158
158
|
* ```
|
|
159
159
|
*
|
|
@@ -204,7 +204,7 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
|
|
|
204
204
|
* @example
|
|
205
205
|
* ```ts
|
|
206
206
|
* window.addEventListener("pointermove", onPointerMove);
|
|
207
|
-
* window.addEventListener("pointerup", () =>
|
|
207
|
+
* window.addEventListener("pointerup", () => onPointerMove.disable());
|
|
208
208
|
* ```
|
|
209
209
|
*/
|
|
210
210
|
public disable(): void
|
|
@@ -294,9 +294,9 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
|
|
|
294
294
|
*
|
|
295
295
|
* @example
|
|
296
296
|
* ```ts
|
|
297
|
-
* window.addEventListener("pointerdown", () =>
|
|
297
|
+
* window.addEventListener("pointerdown", () => onPointerMove.switch("pressed"));
|
|
298
298
|
* window.addEventListener("pointermove", onPointerMove);
|
|
299
|
-
* window.addEventListener("pointerup", () =>
|
|
299
|
+
* window.addEventListener("pointerup", () => onPointerMove.switch("released"));
|
|
300
300
|
* ```
|
|
301
301
|
*
|
|
302
302
|
* ---
|
|
@@ -51,6 +51,51 @@ export type Callback<A extends unknown[] = [], R = void> = (...args: A) => R;
|
|
|
51
51
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
52
|
export type CallbackMap<T = Record<string, Callback<unknown[], unknown>>> = { [K in keyof T]: Callback<any[], any> };
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* An utility type that represents a map of internal events that may
|
|
56
|
+
* be used by the {@link Publisher} class or its child classes.
|
|
57
|
+
*
|
|
58
|
+
* Internal events follow the pattern `__${string}__:${string}` and
|
|
59
|
+
* are used for internal communication within the publisher system.
|
|
60
|
+
* These events are not part of the public API but can be subscribed to for advanced use cases.
|
|
61
|
+
*
|
|
62
|
+
* ---
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* const publisher = new Publisher<EventsMap>();
|
|
67
|
+
*
|
|
68
|
+
* publisher.subscribe("__internals__:clear", () => console.log("Publisher cleared"));
|
|
69
|
+
* publisher.clear(); // "Publisher cleared"
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export type InternalsEventsMap = Record<`__${string}__:${string}`, (...args: unknown[]) => unknown>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* An utility interface that defines a wildcard event for listening to all events.
|
|
76
|
+
*
|
|
77
|
+
* The wildcard event uses the `"*"` key and provides a callback that receives
|
|
78
|
+
* the event type as the first parameter, followed by all the event arguments.
|
|
79
|
+
*
|
|
80
|
+
* It's natively used by the {@link Publisher} class to allow subscribers to listen to all events.
|
|
81
|
+
*
|
|
82
|
+
* ---
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* const publisher = new Publisher<EventsMap>();
|
|
87
|
+
*
|
|
88
|
+
* publisher.subscribe("*", (type: string, ...args: unknown[]) =>
|
|
89
|
+
* {
|
|
90
|
+
* console.log(`Event \`${type}\` was fired with args:`, args));
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* publisher.publish("player:move", { x: 10, y: 20 }); // "Event `player:move` was fired with args: [{ x: 10, y: 20 }]"
|
|
94
|
+
* publisher.publish("player:death"); // "Event `player:death` was fired with args: []"
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export interface WildcardEventsMap { "*": (type: string, ...args: unknown[]) => void; }
|
|
98
|
+
|
|
54
99
|
/**
|
|
55
100
|
* An utility type that represents a {@link Publisher} object that can be published to.
|
|
56
101
|
* See also {@link Subscribable}.
|
|
@@ -74,11 +119,11 @@ export interface Publishable<T extends CallbackMap<T> = CallbackMap>
|
|
|
74
119
|
*
|
|
75
120
|
* @example
|
|
76
121
|
* ```ts
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
122
|
+
* publishable.subscribe("player:move", (coords) => { [...] });
|
|
123
|
+
* publishable.subscribe("player:move", ({ x, y }) => { [...] });
|
|
124
|
+
* publishable.subscribe("player:move", (evt) => { [...] });
|
|
80
125
|
*
|
|
81
|
-
*
|
|
126
|
+
* publishable.publish("player:move", { x: 10, y: 20 });
|
|
82
127
|
* ```
|
|
83
128
|
*
|
|
84
129
|
* ---
|
|
@@ -117,10 +162,10 @@ export interface Subscribable<T extends CallbackMap<T> = CallbackMap>
|
|
|
117
162
|
* @example
|
|
118
163
|
* ```ts
|
|
119
164
|
* let unsubscribe: () => void;
|
|
120
|
-
*
|
|
121
|
-
*
|
|
165
|
+
* subscribable.subscribe("player:death", unsubscribe);
|
|
166
|
+
* subscribable.subscribe("player:spawn", (evt) =>
|
|
122
167
|
* {
|
|
123
|
-
* unsubscribe =
|
|
168
|
+
* unsubscribe = subscribable.subscribe("player:move", ({ x, y }) => { [...] });
|
|
124
169
|
* });
|
|
125
170
|
* ```
|
|
126
171
|
*
|
|
@@ -157,16 +202,3 @@ export interface Subscribable<T extends CallbackMap<T> = CallbackMap>
|
|
|
157
202
|
*/
|
|
158
203
|
unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
|
|
159
204
|
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* An utility type that may be used to wrap a {@link CallbackMap} and extend it with a wildcard event.
|
|
163
|
-
* The resulting type will be the same as the original map, but with an additional `"*"` key.
|
|
164
|
-
*
|
|
165
|
-
* It's natively used by the {@link Publisher} class to allow subscribers to listen to all events.
|
|
166
|
-
*
|
|
167
|
-
* ---
|
|
168
|
-
*
|
|
169
|
-
* @template T A `CallbackMap` compatible interface that defines the map of callbacks.
|
|
170
|
-
*
|
|
171
|
-
*/
|
|
172
|
-
export type WithWildcard<T extends CallbackMap<T>> = T & { "*"?: (type: string, ...args: unknown[]) => void };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Publisher from "../callbacks/publisher.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { 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";
|
|
@@ -18,7 +18,7 @@ import type { MapViewEventsMap } from "./types.js";
|
|
|
18
18
|
* const map = new MapView<string, number>();
|
|
19
19
|
*
|
|
20
20
|
* map.subscribe("entry:add", (key: string, value: number) => console.log(`Added ${key}: ${value}`));
|
|
21
|
-
* map.set("answer", 42); // Added answer: 42
|
|
21
|
+
* map.set("answer", 42); // "Added answer: 42"
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
24
|
* ---
|
|
@@ -26,7 +26,7 @@ import type { MapViewEventsMap } from "./types.js";
|
|
|
26
26
|
* @template K The type of the keys in the map.
|
|
27
27
|
* @template V The type of the values in the map.
|
|
28
28
|
*/
|
|
29
|
-
export default class MapView<K, V> extends Map<K, V>
|
|
29
|
+
export default class MapView<K, V> extends Map<K, V> implements Subscribable<MapViewEventsMap<K, V>>
|
|
30
30
|
{
|
|
31
31
|
/**
|
|
32
32
|
* The internal {@link Publisher} instance used to publish events.
|
|
@@ -158,8 +158,8 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
158
158
|
* console.log(`Added ${key}: ${value}`);
|
|
159
159
|
* });
|
|
160
160
|
*
|
|
161
|
-
* map.set("key1", 2); // Added key1: 2
|
|
162
|
-
* map.set("answer", 42); // Added answer: 42
|
|
161
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
162
|
+
* map.set("answer", 42); // "Added answer: 42"
|
|
163
163
|
* map.set("key2", 4);
|
|
164
164
|
* map.set("key3", 8);
|
|
165
165
|
* ```
|
|
@@ -169,15 +169,14 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
169
169
|
* @template T The key of the map containing the callback signature to subscribe.
|
|
170
170
|
*
|
|
171
171
|
* @param event The name of the event to subscribe to.
|
|
172
|
-
* @param
|
|
172
|
+
* @param subscriber The callback to execute when the event is published.
|
|
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
|
|
177
|
-
|
|
178
|
-
): () => void
|
|
176
|
+
public subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T, subscriber: MapViewEventsMap<K, V>[T])
|
|
177
|
+
: () => void
|
|
179
178
|
{
|
|
180
|
-
return this._publisher.subscribe(event,
|
|
179
|
+
return this._publisher.subscribe(event, subscriber);
|
|
181
180
|
}
|
|
182
181
|
|
|
183
182
|
/**
|
|
@@ -191,7 +190,7 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
191
190
|
* const map = new MapView<string, number>();
|
|
192
191
|
*
|
|
193
192
|
* map.subscribe("entry:add", callback);
|
|
194
|
-
* map.set("key1", 2); // Added key1: 2
|
|
193
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
195
194
|
*
|
|
196
195
|
* map.unsubscribe("entry:add", callback);
|
|
197
196
|
* map.set("key2", 4);
|
|
@@ -202,12 +201,12 @@ export default class MapView<K, V> extends Map<K, V>
|
|
|
202
201
|
* @template T The key of the map containing the callback signature to unsubscribe.
|
|
203
202
|
*
|
|
204
203
|
* @param event The name of the event to unsubscribe from.
|
|
205
|
-
* @param
|
|
204
|
+
* @param subscriber The callback to remove from the event.
|
|
206
205
|
*/
|
|
207
|
-
public unsubscribe<T extends keyof
|
|
208
|
-
|
|
206
|
+
public unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T & string, subscriber: MapViewEventsMap<K, V>[T])
|
|
207
|
+
: void
|
|
209
208
|
{
|
|
210
|
-
this._publisher.unsubscribe(event,
|
|
209
|
+
this._publisher.unsubscribe(event, subscriber);
|
|
211
210
|
}
|
|
212
211
|
|
|
213
212
|
public override readonly [Symbol.toStringTag]: string = "MapView";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Publisher from "../callbacks/publisher.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { 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";
|
|
@@ -18,14 +18,14 @@ import type { SetViewEventsMap } from "./types.js";
|
|
|
18
18
|
* const set = new SetView<number>();
|
|
19
19
|
*
|
|
20
20
|
* set.subscribe("entry:add", (value: number) => console.log(`Added ${value}`));
|
|
21
|
-
* set.add(42); // Added 42
|
|
21
|
+
* set.add(42); // "Added 42"
|
|
22
22
|
* ```
|
|
23
23
|
*
|
|
24
24
|
* ---
|
|
25
25
|
*
|
|
26
26
|
* @template T The type of the values in the set.
|
|
27
27
|
*/
|
|
28
|
-
export default class SetView<T> extends Set<T>
|
|
28
|
+
export default class SetView<T> extends Set<T> implements Subscribable<SetViewEventsMap<T>>
|
|
29
29
|
{
|
|
30
30
|
/**
|
|
31
31
|
* The internal {@link Publisher} instance used to publish events.
|
|
@@ -152,8 +152,8 @@ export default class SetView<T> extends Set<T>
|
|
|
152
152
|
* console.log(`Added ${value}`);
|
|
153
153
|
* });
|
|
154
154
|
*
|
|
155
|
-
* set.add(2); // Added 2
|
|
156
|
-
* set.add(42); // Added 42
|
|
155
|
+
* set.add(2); // "Added 2"
|
|
156
|
+
* set.add(42); // "Added 42"
|
|
157
157
|
* set.add(4);
|
|
158
158
|
* set.add(8);
|
|
159
159
|
* ```
|
|
@@ -163,15 +163,14 @@ export default class SetView<T> extends Set<T>
|
|
|
163
163
|
* @template K The key of the map containing the callback signature to subscribe.
|
|
164
164
|
*
|
|
165
165
|
* @param event The name of the event to subscribe to.
|
|
166
|
-
* @param
|
|
166
|
+
* @param subscriber The callback to execute when the event is published.
|
|
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
|
|
171
|
-
|
|
172
|
-
): () => void
|
|
170
|
+
public subscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, subscriber: SetViewEventsMap<T>[K])
|
|
171
|
+
: () => void
|
|
173
172
|
{
|
|
174
|
-
return this._publisher.subscribe(event,
|
|
173
|
+
return this._publisher.subscribe(event, subscriber);
|
|
175
174
|
}
|
|
176
175
|
|
|
177
176
|
/**
|
|
@@ -185,7 +184,7 @@ export default class SetView<T> extends Set<T>
|
|
|
185
184
|
* const set = new SetView<number>();
|
|
186
185
|
*
|
|
187
186
|
* set.subscribe("entry:add", callback);
|
|
188
|
-
* set.add(2); // Added 2
|
|
187
|
+
* set.add(2); // "Added 2"
|
|
189
188
|
*
|
|
190
189
|
* set.unsubscribe("entry:add", callback);
|
|
191
190
|
* set.add(4);
|
|
@@ -196,13 +195,11 @@ export default class SetView<T> extends Set<T>
|
|
|
196
195
|
* @template K The key of the map containing the callback signature to unsubscribe.
|
|
197
196
|
*
|
|
198
197
|
* @param event The name of the event to unsubscribe from.
|
|
199
|
-
* @param
|
|
198
|
+
* @param subscriber The callback to remove from the event.
|
|
200
199
|
*/
|
|
201
|
-
public unsubscribe<K extends keyof
|
|
202
|
-
event: K & string, callback: WithWildcard<SetViewEventsMap<T>>[K]
|
|
203
|
-
): void
|
|
200
|
+
public unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, subscriber: SetViewEventsMap<T>[K]): void
|
|
204
201
|
{
|
|
205
|
-
this._publisher.unsubscribe(event,
|
|
202
|
+
this._publisher.unsubscribe(event, subscriber);
|
|
206
203
|
}
|
|
207
204
|
|
|
208
205
|
public override readonly [Symbol.toStringTag]: string = "SetView";
|
|
@@ -51,8 +51,8 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
51
51
|
* console.log(`Added ${key}: ${value}`);
|
|
52
52
|
* });
|
|
53
53
|
*
|
|
54
|
-
* map.set("key1", 2); // Added key1: 2
|
|
55
|
-
* map.set("answer", 42); // Added answer: 42
|
|
54
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
55
|
+
* map.set("answer", 42); // "Added answer: 42"
|
|
56
56
|
* map.set("key2", 4);
|
|
57
57
|
* map.set("key3", 8);
|
|
58
58
|
* ```
|
|
@@ -79,7 +79,7 @@ export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
|
|
|
79
79
|
* const map = new MapView<string, number>();
|
|
80
80
|
*
|
|
81
81
|
* map.subscribe("entry:add", callback);
|
|
82
|
-
* map.set("key1", 2); // Added key1: 2
|
|
82
|
+
* map.set("key1", 2); // "Added key1: 2"
|
|
83
83
|
*
|
|
84
84
|
* map.unsubscribe("entry:add", callback);
|
|
85
85
|
* map.set("key2", 4);
|
|
@@ -140,8 +140,8 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
140
140
|
* console.log(`Added ${value}`);
|
|
141
141
|
* });
|
|
142
142
|
*
|
|
143
|
-
* set.add(2); // Added 2
|
|
144
|
-
* set.add(42); // Added 42
|
|
143
|
+
* set.add(2); // "Added 2"
|
|
144
|
+
* set.add(42); // "Added 42"
|
|
145
145
|
* set.add(4);
|
|
146
146
|
* set.add(8);
|
|
147
147
|
* ```
|
|
@@ -168,7 +168,7 @@ export interface ReadonlySetView<T> extends ReadonlySet<T>
|
|
|
168
168
|
* const set = new SetView<number>();
|
|
169
169
|
*
|
|
170
170
|
* set.subscribe("entry:add", callback);
|
|
171
|
-
* set.add(2); // Added 2
|
|
171
|
+
* set.add(2); // "Added 2"
|
|
172
172
|
*
|
|
173
173
|
* set.unsubscribe("entry:add", callback);
|
|
174
174
|
* set.add(4);
|
|
@@ -1021,7 +1021,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
1021
1021
|
*
|
|
1022
1022
|
* for await (const value of iterator)
|
|
1023
1023
|
* {
|
|
1024
|
-
* if (value > 5) { break; } // Closing the iterator...
|
|
1024
|
+
* if (value > 5) { break; } // "Closing the iterator..."
|
|
1025
1025
|
*
|
|
1026
1026
|
* console.log(value); // 1, 2, 3, 4, 5
|
|
1027
1027
|
* }
|
|
@@ -893,7 +893,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
|
|
|
893
893
|
*
|
|
894
894
|
* for (const value of iterator)
|
|
895
895
|
* {
|
|
896
|
-
* if (value > 5) { break; } // Closing the iterator...
|
|
896
|
+
* if (value > 5) { break; } // "Closing the iterator..."
|
|
897
897
|
*
|
|
898
898
|
* console.log(value); // 1, 2, 3, 4, 5
|
|
899
899
|
* }
|
|
@@ -24,9 +24,9 @@ interface ClockEventsMap
|
|
|
24
24
|
* ```ts
|
|
25
25
|
* const clock = new Clock();
|
|
26
26
|
*
|
|
27
|
-
* clock.onStart(() =>
|
|
28
|
-
* clock.onTick((elapsedTime) =>
|
|
29
|
-
* clock.onStop(() =>
|
|
27
|
+
* clock.onStart(() => console.log("The clock has started."));
|
|
28
|
+
* clock.onTick((elapsedTime) => console.log(`The clock has ticked at ${elapsedTime}ms.`));
|
|
29
|
+
* clock.onStop(() => console.log("The clock has stopped."));
|
|
30
30
|
*
|
|
31
31
|
* clock.start();
|
|
32
32
|
* ```
|