@byloth/core 2.1.5 → 2.1.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byloth/core",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
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.3",
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.3"
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.5";
1
+ export const VERSION = "2.1.7";
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,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
- => (...args: A) => R;
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 `(...args: any[]) => any`.
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> = () => void>
36
+ export default abstract class CallableObject<T extends Callback<any[], any> = Callback>
37
37
  extends SmartFunction<Parameters<T>, ReturnType<T>>
38
38
  {
39
39
  /**
@@ -1,6 +1,9 @@
1
1
  import { ReferenceException } from "../exceptions/index.js";
2
2
 
3
- import type { Callback, CallbackMap, WithWildcard } from "./types.js";
3
+ import type { Callback, CallbackMap, InternalsEventsMap, WildcardEventsMap } from "./types.js";
4
+
5
+ type P = InternalsEventsMap;
6
+ type S = WildcardEventsMap & InternalsEventsMap;
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, W extends WithWildcard<T> = WithWildcard<T>>
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,44 +110,39 @@ 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", () => { console.log(`Player has died.`); });
114
- * context.subscribe("player:spawn", () => { console.log(`Player has spawned.`); });
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
  *
120
120
  * ---
121
121
  *
122
122
  * @template U
123
- * A map containing the names of the emittable events and the
124
- * related callback signatures that can be subscribed to them.
125
- * Default is `T`.
126
- *
127
- * @template X An utility type that extends the `U` map with a wildcard event.
123
+ * A map containing the additional names of the emittable events and
124
+ * the related callback signatures that can be subscribed to them.
125
+ * Default is `{ }`.
128
126
  *
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, X extends WithWildcard<U> = WithWildcard<U>>(): Publisher<U, X>
134
- {
135
- const scope = new Publisher<U, X>();
136
131
 
137
- const propagator = (event: (keyof T) & string, ...args: Parameters<T[keyof T]>): void =>
138
- {
139
- scope.publish(event, ...args);
140
- };
132
+ // eslint-disable-next-line @typescript-eslint/no-empty-object-type
133
+ public createScope<U extends CallbackMap<U> = { }>(): Publisher<U & T>
134
+ {
135
+ const scope = new Publisher();
141
136
 
142
- // @ts-expect-error It's an internal event, not part of the public API.
143
137
  this.subscribe("__internals__:clear", () => scope.clear());
144
- this.subscribe("*", propagator as W["*"]);
138
+ this.subscribe("*", (event, ...args): void => { scope.publish(event, ...args); });
145
139
 
146
140
  return scope;
147
141
  }
148
142
 
149
143
  /**
150
- * Publishes an event to all the subscribers.
144
+ * Publishes an event to all the subscribers.
145
+ * This events will trigger the wildcard listeners as well, if any.
151
146
  *
152
147
  * ---
153
148
  *
@@ -169,14 +164,41 @@ 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
+ * Please note to use this method only if you know what you are doing.
175
+ *
176
+ * ---
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * publisher.subscribe("__internals__:clear", () => console.log("Clearing..."));
181
+ * publisher.publish("__internals__:clear"); // "Clearing..."
182
+ * ```
183
+ *
184
+ * ---
185
+ *
186
+ * @template K The key of the internal events map containing the callback signature to publish.
187
+ *
188
+ * @param event The name of the internal event to publish.
189
+ * @param args The arguments to pass to the subscribers.
190
+ *
191
+ * @returns An array containing the return values of all the subscribers.
192
+ */
193
+ public publish<K extends keyof P>(event: K & string, ...args: Parameters<P[K]>): ReturnType<P[K]>[];
194
+ public publish(event: string, ...args: unknown[]): unknown[]
173
195
  {
174
- let results: ReturnType<T[K]>[];
196
+ let results: unknown[];
175
197
  let subscribers = this._subscribers.get(event);
176
198
  if (subscribers)
177
199
  {
178
200
  results = subscribers.slice()
179
- .map((subscriber) => subscriber(...args)) as ReturnType<T[K]>[];
201
+ .map((subscriber) => subscriber(...args));
180
202
  }
181
203
  else { results = []; }
182
204
 
@@ -217,7 +239,35 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
217
239
  *
218
240
  * @returns A function that can be used to unsubscribe the subscriber from the event.
219
241
  */
220
- public subscribe<K extends keyof W>(event: K & string, subscriber: W[K]): () => void
242
+ public subscribe<K extends keyof T>(event: K & string, subscriber: T[K]): () => void;
243
+
244
+ /**
245
+ * Subscribes to the wildcard event to listen to all published events.
246
+ *
247
+ * The wildcard subscriber will be called for every event published, receiving
248
+ * the event type as the first parameter followed by all event arguments.
249
+ *
250
+ * ---
251
+ *
252
+ * @example
253
+ * ```ts
254
+ * publisher.subscribe("*", (type, ...args) =>
255
+ * {
256
+ * console.log(`Event \`${type}\` was fired with args:`, args);
257
+ * });
258
+ * ```
259
+ *
260
+ * ---
261
+ *
262
+ * @template K The key of the wildcard events map (always "*").
263
+ *
264
+ * @param event The wildcard event name ("*").
265
+ * @param subscriber The subscriber to execute for all published events.
266
+ *
267
+ * @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
268
+ */
269
+ public subscribe<K extends keyof S>(event: K & string, subscriber: S[K]): () => void;
270
+ public subscribe(event: string, subscriber: Callback<unknown[], unknown>): () => void
221
271
  {
222
272
  const subscribers = this._subscribers.get(event) ?? [];
223
273
  subscribers.push(subscriber);
@@ -257,7 +307,32 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap, W extends
257
307
  * @param event The name of the event to unsubscribe from.
258
308
  * @param subscriber The subscriber to remove from the event.
259
309
  */
260
- public unsubscribe<K extends keyof W>(event: K & string, subscriber: W[K]): void
310
+ public unsubscribe<K extends keyof T>(event: K & string, subscriber: T[K]): void;
311
+
312
+ /**
313
+ * Unsubscribes from the wildcard event and removes a subscriber from being executed for all events.
314
+ *
315
+ * This removes a previously registered wildcard listener that was capturing all published events.
316
+ *
317
+ * ---
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * const wildcardHandler = (type: string, ...args: unknown[]) => console.log(type, args);
322
+ *
323
+ * publisher.subscribe("*", wildcardHandler);
324
+ * publisher.unsubscribe("*", wildcardHandler);
325
+ * ```
326
+ *
327
+ * ---
328
+ *
329
+ * @template K The key of the wildcard events map (always "*").
330
+ *
331
+ * @param event The wildcard event name ("*").
332
+ * @param subscriber The wildcard subscriber to remove.
333
+ */
334
+ public unsubscribe<K extends keyof S>(event: K & string, subscriber: S[K]): void;
335
+ public unsubscribe(event: string, subscriber: Callback<unknown[], unknown>): void
261
336
  {
262
337
  const subscribers = this._subscribers.get(event);
263
338
  if (!(subscribers))
@@ -20,14 +20,14 @@ const Disabler = () => { /* ... */ };
20
20
  * onPointerMove.register("released", () => { [...] });
21
21
  * onPointerMove.register("pressed", () => { [...] });
22
22
  *
23
- * window.addEventListener("pointerdown", () => { onPointerMove.switch("pressed"); });
23
+ * window.addEventListener("pointerdown", () => onPointerMove.switch("pressed"));
24
24
  * window.addEventListener("pointermove", onPointerMove);
25
- * window.addEventListener("pointerup", () => { onPointerMove.switch("released"); });
25
+ * window.addEventListener("pointerup", () => onPointerMove.switch("released"));
26
26
  * ```
27
27
  *
28
28
  * ---
29
29
  *
30
- * @template T The type signature of the callback. Default is `(...args: any[]) => any`.
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>
@@ -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", () => { onPointerMove.enable(); });
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", () => { onPointerMove.disable(); });
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", () => { onPointerMove.switch("pressed"); });
297
+ * window.addEventListener("pointerdown", () => onPointerMove.switch("pressed"));
298
298
  * window.addEventListener("pointermove", onPointerMove);
299
- * window.addEventListener("pointerup", () => { onPointerMove.switch("released"); });
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}`, Callback<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
- * publisher.subscribe("player:move", (coords) => { [...] });
78
- * publisher.subscribe("player:move", ({ x, y }) => { [...] });
79
- * publisher.subscribe("player:move", (evt) => { [...] });
122
+ * publishable.subscribe("player:move", (coords) => { [...] });
123
+ * publishable.subscribe("player:move", ({ x, y }) => { [...] });
124
+ * publishable.subscribe("player:move", (evt) => { [...] });
80
125
  *
81
- * publisher.publish("player:move", { x: 10, y: 20 });
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
- * publisher.subscribe("player:death", unsubscribe);
121
- * publisher.subscribe("player:spawn", (evt) =>
165
+ * subscribable.subscribe("player:death", unsubscribe);
166
+ * subscribable.subscribe("player:spawn", (evt) =>
122
167
  * {
123
- * unsubscribe = publisher.subscribe("player:move", ({ x, y }) => { [...] });
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 { WithWildcard } from "../callbacks/types.js";
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 callback The callback to execute when the event is published.
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 WithWildcard<MapViewEventsMap<K, V>>>(
177
- event: T & string, callback: WithWildcard<MapViewEventsMap<K, V>>[T]
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, callback);
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 callback The callback to remove from the event.
204
+ * @param subscriber The callback to remove from the event.
206
205
  */
207
- public unsubscribe<T extends keyof WithWildcard<MapViewEventsMap<K, V>>>(
208
- event: T & string, callback: WithWildcard<MapViewEventsMap<K, V>>[T]): void
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, callback);
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 { WithWildcard } from "../callbacks/types.js";
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 callback The callback to execute when the event is published.
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 WithWildcard<SetViewEventsMap<T>>>(
171
- event: K & string, callback: WithWildcard<SetViewEventsMap<T>>[K]
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, callback);
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 callback The callback to remove from the event.
198
+ * @param subscriber The callback to remove from the event.
200
199
  */
201
- public unsubscribe<K extends keyof WithWildcard<SetViewEventsMap<T>>>(
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, callback);
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(() => { 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."); });
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
  * ```