@byloth/core 2.1.8 → 2.2.1

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.
@@ -6,25 +6,6 @@ import type MapView from "./map-view.js";
6
6
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
7
7
  import type SetView from "./set-view.js";
8
8
 
9
- /**
10
- * A type that represents the map of events published by a {@link MapView} object.
11
- * See also {@link SetViewEventsMap}.
12
- *
13
- * The keys of the map are the event names while the values are the callback function signatures.
14
- *
15
- * ---
16
- *
17
- * @template K The type of the keys in the map.
18
- * @template V The type of the values in the map.
19
- */
20
- export interface MapViewEventsMap<K, V>
21
- {
22
- "entry:add": (key: K, value: V) => void;
23
- "entry:remove": (key: K, value: V) => void;
24
-
25
- "collection:clear": () => void;
26
- }
27
-
28
9
  /**
29
10
  * An utility type that represents a read-only {@link MapView} object.
30
11
  * See also {@link ReadonlySetView}.
@@ -40,79 +21,65 @@ export interface MapViewEventsMap<K, V>
40
21
  export interface ReadonlyMapView<K, V> extends ReadonlyMap<K, V>
41
22
  {
42
23
  /**
43
- * Subscribes to an event and adds a callback to be executed when the event is published.
24
+ * Subscribes to the `add` event of the map with a callback that will be executed when an entry is added.
44
25
  *
45
26
  * ---
46
27
  *
47
28
  * @example
48
29
  * ```ts
49
- * const map = new MapView<string, number>();
50
- * const unsubscribe = map.subscribe("entry:add", (key: string, value: number) =>
51
- * {
52
- * if (key === "answer") { unsubscribe(); }
53
- * console.log(`Added ${key}: ${value}`);
54
- * });
30
+ * map.onAdd((key, value) => console.log(`Added ${key}: ${value}`));
55
31
  *
56
32
  * map.set("key1", 2); // "Added key1: 2"
57
33
  * map.set("answer", 42); // "Added answer: 42"
58
- * map.set("key2", 4);
59
- * map.set("key3", 8);
60
34
  * ```
61
35
  *
62
36
  * ---
63
37
  *
64
- * @template T The key of the map containing the callback signature to subscribe.
65
- *
66
- * @param event The name of the event to subscribe to.
67
- * @param callback The callback to execute when the event is published.
38
+ * @param callback The callback that will be executed when an entry is added to the map.
68
39
  *
69
- * @returns A function that can be used to unsubscribe the callback from the event.
40
+ * @returns A function that can be used to unsubscribe from the event.
70
41
  */
71
- subscribe<T extends keyof MapViewEventsMap<K, V>>(event: T & string, callback: MapViewEventsMap<K, V>[T]): Callback;
42
+ onAdd(callback: (key: K, value: V) => void): Callback;
72
43
 
73
44
  /**
74
- * Unsubscribes from an event and removes a callback from being executed when the event is published.
45
+ * Subscribes to the `remove` event of the map with a callback that will be executed when an entry is removed.
75
46
  *
76
47
  * ---
77
48
  *
78
49
  * @example
79
50
  * ```ts
80
- * const callback = (key: string, value: number) => console.log(`Added ${key}: ${value}`);
81
- * const map = new MapView<string, number>();
82
- *
83
- * map.subscribe("entry:add", callback);
84
- * map.set("key1", 2); // "Added key1: 2"
51
+ * map.onRemove((key, value) => console.log(`Removed ${key}: ${value}`));
85
52
  *
86
- * map.unsubscribe("entry:add", callback);
87
- * map.set("key2", 4);
53
+ * map.delete("key1"); // "Removed key1: 2"
54
+ * map.delete("answer"); // "Removed answer: 42"
88
55
  * ```
89
56
  *
90
57
  * ---
91
58
  *
92
- * @template T The key of the map containing the callback signature to unsubscribe.
59
+ * @param callback The callback that will be executed when an entry is removed from the map.
93
60
  *
94
- * @param event The name of the event to unsubscribe from.
95
- * @param callback The callback to remove from the event.
61
+ * @returns A function that can be used to unsubscribe from the event.
96
62
  */
97
- unsubscribe<T extends keyof MapViewEventsMap<K, V>>(event: T & string, callback: MapViewEventsMap<K, V>[T]): void;
98
- }
99
-
100
- /**
101
- * A type that represents the map of events published by a {@link SetView} object.
102
- * See also {@link MapViewEventsMap}.
103
- *
104
- * The keys of the map are the event names while the values are the callback function signatures.
105
- *
106
- * ---
107
- *
108
- * @template T The type of the values in the set.
109
- */
110
- export interface SetViewEventsMap<T>
111
- {
112
- "entry:add": (value: T) => void;
113
- "entry:remove": (value: T) => void;
63
+ onRemove(callback: (key: K, value: V) => void): Callback;
114
64
 
115
- "collection:clear": () => void;
65
+ /**
66
+ * Subscribes to the `clear` event of the map with a callback that will be executed when the map is cleared.
67
+ *
68
+ * ---
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * map.onClear(() => console.log("The map has all been cleared."));
73
+ * map.clear();
74
+ * ```
75
+ *
76
+ * ---
77
+ *
78
+ * @param callback The callback that will be executed when the map is cleared.
79
+ *
80
+ * @returns A function that can be used to unsubscribe from the event.
81
+ */
82
+ onClear(callback: () => void): Callback;
116
83
  }
117
84
 
118
85
  /**
@@ -129,59 +96,63 @@ export interface SetViewEventsMap<T>
129
96
  export interface ReadonlySetView<T> extends ReadonlySet<T>
130
97
  {
131
98
  /**
132
- * Subscribes to an event and adds a callback to be executed when the event is published.
99
+ * Subscribes to the `add` event of the set with a callback that will be executed when a value is added.
133
100
  *
134
101
  * ---
135
102
  *
136
103
  * @example
137
104
  * ```ts
138
- * const set = new SetView<number>();
139
- * const unsubscribe = set.subscribe("entry:add", (value: number) =>
140
- * {
141
- * if (value === 42) { unsubscribe(); }
142
- * console.log(`Added ${value}`);
143
- * });
105
+ * set.onAdd((value) => console.log(`Added ${value}`));
144
106
  *
145
107
  * set.add(2); // "Added 2"
146
108
  * set.add(42); // "Added 42"
147
- * set.add(4);
148
- * set.add(8);
149
109
  * ```
150
110
  *
151
111
  * ---
152
112
  *
153
- * @template K The key of the map containing the callback signature to subscribe.
154
- *
155
- * @param event The name of the event to subscribe to.
156
- * @param callback The callback to execute when the event is published.
113
+ * @param callback The callback that will be executed when a value is added to the set.
157
114
  *
158
- * @returns A function that can be used to unsubscribe the callback from the event.
115
+ * @returns A function that can be used to unsubscribe from the event.
159
116
  */
160
- subscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, callback: SetViewEventsMap<T>[K]): Callback;
117
+ onAdd(callback: (value: T) => void): Callback;
161
118
 
162
119
  /**
163
- * Unsubscribes from an event and removes a callback from being executed when the event is published.
120
+ * Subscribes to the `remove` event of the set with a callback that will be executed when a value is removed.
164
121
  *
165
122
  * ---
166
123
  *
167
124
  * @example
168
125
  * ```ts
169
- * const callback = (value: number) => console.log(`Added ${value}`);
170
- * const set = new SetView<number>();
126
+ * set.onRemove((value) => console.log(`Removed ${value}`));
171
127
  *
172
- * set.subscribe("entry:add", callback);
173
- * set.add(2); // "Added 2"
128
+ * set.delete(2); // "Removed 2"
129
+ * set.delete(42); // "Removed 42"
130
+ * ```
131
+ *
132
+ * ---
133
+ *
134
+ * @param callback The callback that will be executed when a value is removed from the set.
135
+ *
136
+ * @returns A function that can be used to unsubscribe from the event.
137
+ */
138
+ onRemove(callback: (value: T) => void): Callback;
139
+
140
+ /**
141
+ * Subscribes to the `clear` event of the set with a callback that will be executed when the set is cleared.
142
+ *
143
+ * ---
174
144
  *
175
- * set.unsubscribe("entry:add", callback);
176
- * set.add(4);
145
+ * @example
146
+ * ```ts
147
+ * set.onClear(() => console.log("The set has all been cleared."));
148
+ * set.clear(); // "The set has all been cleared."
177
149
  * ```
178
150
  *
179
151
  * ---
180
152
  *
181
- * @template K The key of the map containing the callback signature to unsubscribe.
153
+ * @param callback The callback that will be executed when the set is cleared.
182
154
  *
183
- * @param event The name of the event to unsubscribe from.
184
- * @param callback The callback to remove from the event.
155
+ * @returns A function that can be used to unsubscribe from the event.
185
156
  */
186
- unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, callback: SetViewEventsMap<T>[K]): void;
157
+ onClear(callback: () => void): Callback;
187
158
  }
@@ -5,7 +5,7 @@ export {
5
5
 
6
6
  } from "./aggregators/index.js";
7
7
 
8
- export { CallableObject, Publisher, SwitchableCallback } from "./callbacks/index.js";
8
+ export { CallableObject, CallbackChain, Publisher, SwitchableCallback } from "./callbacks/index.js";
9
9
  export { MapView, SetView } from "./collections/index.js";
10
10
  export {
11
11
  Exception,
@@ -8,9 +8,9 @@ import GameLoop from "./game-loop.js";
8
8
 
9
9
  interface ClockEventsMap
10
10
  {
11
- start: () => void;
12
- stop: () => void;
13
- tick: (elapsedTime: number) => void;
11
+ "start": () => void;
12
+ "stop": () => void;
13
+ "tick": (elapsedTime: number) => void;
14
14
  }
15
15
 
16
16
  /**
@@ -14,9 +14,6 @@ interface CountdownEventsMap
14
14
  stop: (reason: unknown) => void;
15
15
  tick: (remainingTime: number) => void;
16
16
  expire: () => void;
17
-
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
- [key: string]: Callback<any[], any>;
20
17
  }
21
18
 
22
19
  /**
@@ -211,28 +208,6 @@ export default class Countdown extends GameLoop
211
208
  this._publisher.publish("stop", reason);
212
209
  }
213
210
 
214
- /**
215
- * Subscribes to the `expire` event of the countdown.
216
- *
217
- * ---
218
- *
219
- * @example
220
- * ```ts
221
- * countdown.onExpire(() => { [...] }); // This callback will be executed once the countdown has expired.
222
- * countdown.start();
223
- * ```
224
- *
225
- * ---
226
- *
227
- * @param callback The callback that will be executed when the countdown expires.
228
- *
229
- * @returns A function that can be used to unsubscribe from the event.
230
- */
231
- public onExpire(callback: Callback): Callback
232
- {
233
- return this._publisher.subscribe("expire", callback);
234
- }
235
-
236
211
  /**
237
212
  * Subscribes to the `tick` event of the countdown.
238
213
  *
@@ -273,5 +248,27 @@ export default class Countdown extends GameLoop
273
248
  });
274
249
  }
275
250
 
251
+ /**
252
+ * Subscribes to the `expire` event of the countdown.
253
+ *
254
+ * ---
255
+ *
256
+ * @example
257
+ * ```ts
258
+ * countdown.onExpire(() => { [...] }); // This callback will be executed once the countdown has expired.
259
+ * countdown.start();
260
+ * ```
261
+ *
262
+ * ---
263
+ *
264
+ * @param callback The callback that will be executed when the countdown expires.
265
+ *
266
+ * @returns A function that can be used to unsubscribe from the event.
267
+ */
268
+ public onExpire(callback: Callback): Callback
269
+ {
270
+ return this._publisher.subscribe("expire", callback);
271
+ }
272
+
276
273
  public override readonly [Symbol.toStringTag]: string = "Countdown";
277
274
  }
@@ -7,8 +7,8 @@ import type { Callback } from "../types.js";
7
7
 
8
8
  interface GameLoopEventsMap
9
9
  {
10
- start: () => void;
11
- stop: () => void;
10
+ "start": () => void;
11
+ "stop": () => void;
12
12
  }
13
13
 
14
14
  /**
@@ -9,7 +9,7 @@ export type {
9
9
 
10
10
  } from "./aggregators/types.js";
11
11
 
12
- export type { MapViewEventsMap, ReadonlyMapView, SetViewEventsMap, ReadonlySetView } from "./collections/types.js";
12
+ export type { ReadonlyMapView, ReadonlySetView } from "./collections/types.js";
13
13
  export type {
14
14
  GeneratorFunction,
15
15
  AsyncGeneratorFunction,