@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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byloth/core",
3
- "version": "2.1.8",
3
+ "version": "2.2.1",
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.2.2",
53
53
  "@eslint/compat": "^1.4.1",
54
54
  "@types/node": "^22.19.0",
55
- "@vitest/coverage-v8": "^3.2.4",
55
+ "@vitest/coverage-v8": "^4.0.8",
56
56
  "eslint": "^9.39.1",
57
57
  "husky": "^9.1.7",
58
58
  "jsdom": "^27.1.0",
59
59
  "typescript": "^5.9.3",
60
- "vite": "^7.2.1",
61
- "vitest": "^3.2.4"
60
+ "vite": "^7.2.2",
61
+ "vitest": "^4.0.8"
62
62
  },
63
63
  "scripts": {
64
64
  "dev": "vite",
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = "2.1.8";
1
+ export const VERSION = "2.2.1";
2
2
 
3
3
  export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
4
4
 
@@ -7,6 +7,7 @@ export {
7
7
  AggregatedIterator,
8
8
  AggregatedAsyncIterator,
9
9
  CallableObject,
10
+ CallbackChain,
10
11
  Clock,
11
12
  Countdown,
12
13
  DeferredPromise,
@@ -61,7 +62,6 @@ export type {
61
62
  KeyedIteratee,
62
63
  KeyedReducer,
63
64
  KeyedTypeGuardPredicate,
64
- MapViewEventsMap,
65
65
  MaybeAsyncKeyedIteratee,
66
66
  MaybeAsyncKeyedReducer,
67
67
  MaybeAsyncGeneratorFunction,
@@ -77,7 +77,6 @@ export type {
77
77
  ReadonlySetView,
78
78
  Reducer,
79
79
  RejectedHandler,
80
- SetViewEventsMap,
81
80
  Subscribable,
82
81
  TypeGuardPredicate,
83
82
  WildcardEventsMap
@@ -0,0 +1,163 @@
1
+ import CallableObject from "./callable-object.js";
2
+ import type { Callback } from "./types.js";
3
+
4
+ /**
5
+ * A class that collects multiple functions or callbacks and executes them sequentially when invoked.
6
+ *
7
+ * This is particularly useful for managing multiple cleanup operations, such as
8
+ * collecting unsubscribe callbacks from event subscriptions and calling them all at once.
9
+ *
10
+ * ---
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const unsubscribeAll = new CallbackChain<() => void>()
15
+ * .add(() => console.log("Doing something..."))
16
+ * .add(() => console.log("Doing something else..."))
17
+ * .add(() => console.log("Doing yet another thing..."));
18
+ *
19
+ * unsubscribeAll(); // Doing something...
20
+ * // Doing something else...
21
+ * // Doing yet another thing...
22
+ * ```
23
+ *
24
+ * ---
25
+ *
26
+ * @template T
27
+ * The type signature of the functions in the chain.
28
+ * All functions must share the same signature. Default is `() => void`.
29
+ */
30
+ export default class CallbackChain<
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ T extends Callback<any[], any> = Callback,
33
+ P extends Parameters<T> = Parameters<T>,
34
+ R extends ReturnType<T> = ReturnType<T>
35
+ > extends CallableObject<Callback<P, R[]>>
36
+ {
37
+ /**
38
+ * The array containing all the functions in the chain.
39
+ */
40
+ protected readonly _callbacks: T[];
41
+
42
+ /**
43
+ * Gets the number of functions currently in the chain.
44
+ */
45
+ public get size(): number
46
+ {
47
+ return this._callbacks.length;
48
+ }
49
+
50
+ /**
51
+ * Initializes a new instance of the {@link CallbackChain} class.
52
+ *
53
+ * ---
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const chain = new CallbackChain();
58
+ * ```
59
+ *
60
+ * ---
61
+ *
62
+ * @param callback Optional initial functions to add to the chain.
63
+ */
64
+ public constructor(...callback: T[])
65
+ {
66
+ super();
67
+
68
+ this._callbacks = callback;
69
+ }
70
+
71
+ /**
72
+ * Executes all functions in the chain sequentially with the provided arguments.
73
+ *
74
+ * ---
75
+ *
76
+ * @param args The arguments to pass to each function in the chain.
77
+ *
78
+ * @returns An array containing the return values of all functions.
79
+ */
80
+ protected override _invoke(...args: Parameters<T>): ReturnType<T>[]
81
+ {
82
+ return this._callbacks.map((callback) => callback(...args)) as ReturnType<T>[];
83
+ }
84
+
85
+ /**
86
+ * Adds a function to the chain.
87
+ *
88
+ * ---
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * const chain = new CallbackChain();
93
+ * const cleanup = () => console.log("Cleaning up..."));
94
+ *
95
+ * chain.add(cleanup);
96
+ * ```
97
+ *
98
+ * ---
99
+ *
100
+ * @param callback The function to add to the chain.
101
+ *
102
+ * @returns The current instance for method chaining.
103
+ */
104
+ public add(callback: T): this
105
+ {
106
+ this._callbacks.push(callback);
107
+
108
+ return this;
109
+ }
110
+
111
+ /**
112
+ * Removes a specific function from the chain.
113
+ *
114
+ * ---
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * const chain = new CallbackChain();
119
+ * const cleanup = () => console.log("Cleaning up..."));
120
+ *
121
+ * chain.add(cleanup);
122
+ * chain.remove(cleanup);
123
+ * ```
124
+ *
125
+ * ---
126
+ *
127
+ * @param callback The function to remove from the chain.
128
+ *
129
+ * @returns `true` if the function was found and removed, `false` otherwise.
130
+ */
131
+ public remove(callback: T): boolean
132
+ {
133
+ const index = this._callbacks.indexOf(callback);
134
+ if (index < 0) { return false; }
135
+
136
+ this._callbacks.splice(index, 1);
137
+
138
+ return true;
139
+ }
140
+
141
+ /**
142
+ * Removes all functions from the chain.
143
+ *
144
+ * ---
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * const chain = new CallbackChain();
149
+ *
150
+ * chain.add(() => console.log("Doing something..."));
151
+ * chain.add(() => console.log("Doing something else..."));
152
+ * chain.add(() => console.log("Doing yet another thing..."));
153
+ *
154
+ * chain.clear();
155
+ * ```
156
+ */
157
+ public clear(): void
158
+ {
159
+ this._callbacks.length = 0;
160
+ }
161
+
162
+ public override readonly [Symbol.toStringTag]: string = "CallbackChain";
163
+ }
@@ -1,5 +1,6 @@
1
1
  import CallableObject from "./callable-object.js";
2
+ import CallbackChain from "./callback-chain.js";
2
3
  import Publisher from "./publisher.js";
3
4
  import SwitchableCallback from "./switchable-callback.js";
4
5
 
5
- export { CallableObject, Publisher, SwitchableCallback };
6
+ export { CallableObject, CallbackChain, Publisher, SwitchableCallback };
@@ -1,9 +1,16 @@
1
1
  import Publisher from "../callbacks/publisher.js";
2
- import type { Callback, Subscribable } from "../types.js";
2
+ import type { Callback } 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";
6
- import type { MapViewEventsMap } from "./types.js";
6
+
7
+ interface MapViewEventsMap<K, V>
8
+ {
9
+ "add": (key: K, value: V) => void;
10
+ "remove": (key: K, value: V) => void;
11
+
12
+ "clear": () => void;
13
+ }
7
14
 
8
15
  /**
9
16
  * A wrapper class around the native {@link Map} class that provides additional functionality
@@ -17,7 +24,7 @@ import type { MapViewEventsMap } from "./types.js";
17
24
  * ```ts
18
25
  * const map = new MapView<string, number>();
19
26
  *
20
- * map.subscribe("entry:add", (key: string, value: number) => console.log(`Added ${key}: ${value}`));
27
+ * map.onAdd((key: string, value: number) => console.log(`Added ${key}: ${value}`));
21
28
  * map.set("answer", 42); // "Added answer: 42"
22
29
  * ```
23
30
  *
@@ -26,7 +33,7 @@ import type { MapViewEventsMap } from "./types.js";
26
33
  * @template K The type of the keys in the map.
27
34
  * @template V The type of the values in the map.
28
35
  */
29
- export default class MapView<K, V> extends Map<K, V> implements Subscribable<MapViewEventsMap<K, V>>
36
+ export default class MapView<K, V> extends Map<K, V>
30
37
  {
31
38
  /**
32
39
  * The internal {@link Publisher} instance used to publish events.
@@ -86,7 +93,7 @@ export default class MapView<K, V> extends Map<K, V> implements Subscribable<Map
86
93
  {
87
94
  super.set(key, value);
88
95
 
89
- this._publisher.publish("entry:add", key, value);
96
+ this._publisher.publish("add", key, value);
90
97
 
91
98
  return this;
92
99
  }
@@ -118,7 +125,7 @@ export default class MapView<K, V> extends Map<K, V> implements Subscribable<Map
118
125
 
119
126
  super.delete(key);
120
127
 
121
- this._publisher.publish("entry:remove", key, value);
128
+ this._publisher.publish("remove", key, value);
122
129
 
123
130
  return true;
124
131
  }
@@ -141,74 +148,77 @@ export default class MapView<K, V> extends Map<K, V> implements Subscribable<Map
141
148
  const size = this.size;
142
149
 
143
150
  super.clear();
144
- if (size > 0) { this._publisher.publish("collection:clear"); }
151
+ if (size > 0) { this._publisher.publish("clear"); }
145
152
  }
146
153
 
147
154
  /**
148
- * Subscribes to an event and adds a callback to be executed when the event is published.
155
+ * Subscribes to the `add` event of the map with a callback that will be executed when an entry is added.
149
156
  *
150
157
  * ---
151
158
  *
152
159
  * @example
153
160
  * ```ts
154
- * const map = new MapView<string, number>();
155
- * const unsubscribe = map.subscribe("entry:add", (key: string, value: number) =>
156
- * {
157
- * if (key === "answer") { unsubscribe(); }
158
- * console.log(`Added ${key}: ${value}`);
159
- * });
161
+ * map.onAdd((key, value) => console.log(`Added ${key}: ${value}`));
160
162
  *
161
163
  * map.set("key1", 2); // "Added key1: 2"
162
164
  * map.set("answer", 42); // "Added answer: 42"
163
- * map.set("key2", 4);
164
- * map.set("key3", 8);
165
165
  * ```
166
166
  *
167
167
  * ---
168
168
  *
169
- * @template T The key of the map containing the callback signature to subscribe.
170
- *
171
- * @param event The name of the event to subscribe to.
172
- * @param subscriber The callback to execute when the event is published.
169
+ * @param callback The callback that will be executed when an entry is added to the map.
173
170
  *
174
- * @returns A function that can be used to unsubscribe the callback from the event.
171
+ * @returns A function that can be used to unsubscribe from the event.
175
172
  */
176
- public subscribe<T extends keyof MapViewEventsMap<K, V>>(
177
- event: T & string, subscriber: MapViewEventsMap<K, V>[T]
178
- ): Callback
173
+ public onAdd(callback: (key: K, value: V) => void): Callback
179
174
  {
180
- return this._publisher.subscribe(event, subscriber);
175
+ return this._publisher.subscribe("add", callback);
181
176
  }
182
177
 
183
178
  /**
184
- * Unsubscribes from an event and removes a callback from being executed when the event is published.
179
+ * Subscribes to the `remove` event of the map with a callback that will be executed when an entry is removed.
185
180
  *
186
181
  * ---
187
182
  *
188
183
  * @example
189
184
  * ```ts
190
- * const callback = (key: string, value: number) => console.log(`Added ${key}: ${value}`);
191
- * const map = new MapView<string, number>();
185
+ * map.onRemove((key, value) => console.log(`Removed ${key}: ${value}`));
192
186
  *
193
- * map.subscribe("entry:add", callback);
194
- * map.set("key1", 2); // "Added key1: 2"
187
+ * map.delete("key1"); // "Removed key1: 2"
188
+ * map.delete("answer"); // "Removed answer: 42"
189
+ * ```
190
+ *
191
+ * ---
195
192
  *
196
- * map.unsubscribe("entry:add", callback);
197
- * map.set("key2", 4);
193
+ * @param callback The callback that will be executed when an entry is removed from the map.
194
+ *
195
+ * @returns A function that can be used to unsubscribe from the event.
196
+ */
197
+ public onRemove(callback: (key: K, value: V) => void): Callback
198
+ {
199
+ return this._publisher.subscribe("remove", callback);
200
+ }
201
+
202
+ /**
203
+ * Subscribes to the `clear` event of the map with a callback that will be executed when the map is cleared.
204
+ *
205
+ * ---
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * map.onClear(() => console.log("The map has all been cleared."));
210
+ * map.clear(); // "The map has all been cleared."
198
211
  * ```
199
212
  *
200
213
  * ---
201
214
  *
202
- * @template T The key of the map containing the callback signature to unsubscribe.
215
+ * @param callback The callback that will be executed when the map is cleared.
203
216
  *
204
- * @param event The name of the event to unsubscribe from.
205
- * @param subscriber The callback to remove from the event.
217
+ * @returns A function that can be used to unsubscribe from the event.
206
218
  */
207
- public unsubscribe<T extends keyof MapViewEventsMap<K, V>>(
208
- event: T & string, subscriber: MapViewEventsMap<K, V>[T]
209
- ): void
219
+ public onClear(callback: () => void): Callback
210
220
  {
211
- this._publisher.unsubscribe(event, subscriber);
221
+ return this._publisher.subscribe("clear", callback);
212
222
  }
213
223
 
214
224
  public override readonly [Symbol.toStringTag]: string = "MapView";
@@ -1,9 +1,16 @@
1
1
  import Publisher from "../callbacks/publisher.js";
2
- import type { Callback, Subscribable } from "../types.js";
2
+ import type { Callback } 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";
6
- import type { SetViewEventsMap } from "./types.js";
6
+
7
+ interface SetViewEventsMap<T>
8
+ {
9
+ "add": (value: T) => void;
10
+ "remove": (value: T) => void;
11
+
12
+ "clear": () => void;
13
+ }
7
14
 
8
15
  /**
9
16
  * A wrapper class around the native {@link Set} class that provides additional functionality
@@ -17,7 +24,7 @@ import type { SetViewEventsMap } from "./types.js";
17
24
  * ```ts
18
25
  * const set = new SetView<number>();
19
26
  *
20
- * set.subscribe("entry:add", (value: number) => console.log(`Added ${value}`));
27
+ * set.onAdd((value: number) => console.log(`Added ${value}`));
21
28
  * set.add(42); // "Added 42"
22
29
  * ```
23
30
  *
@@ -25,7 +32,7 @@ import type { SetViewEventsMap } from "./types.js";
25
32
  *
26
33
  * @template T The type of the values in the set.
27
34
  */
28
- export default class SetView<T> extends Set<T> implements Subscribable<SetViewEventsMap<T>>
35
+ export default class SetView<T> extends Set<T>
29
36
  {
30
37
  /**
31
38
  * The internal {@link Publisher} instance used to publish events.
@@ -84,7 +91,7 @@ export default class SetView<T> extends Set<T> implements Subscribable<SetViewEv
84
91
  {
85
92
  super.add(value);
86
93
 
87
- this._publisher.publish("entry:add", value);
94
+ this._publisher.publish("add", value);
88
95
 
89
96
  return this;
90
97
  }
@@ -112,7 +119,7 @@ export default class SetView<T> extends Set<T> implements Subscribable<SetViewEv
112
119
  public override delete(value: T): boolean
113
120
  {
114
121
  const result = super.delete(value);
115
- if (result) { this._publisher.publish("entry:remove", value); }
122
+ if (result) { this._publisher.publish("remove", value); }
116
123
 
117
124
  return result;
118
125
  }
@@ -135,72 +142,77 @@ export default class SetView<T> extends Set<T> implements Subscribable<SetViewEv
135
142
  const size = this.size;
136
143
 
137
144
  super.clear();
138
- if (size > 0) { this._publisher.publish("collection:clear"); }
145
+ if (size > 0) { this._publisher.publish("clear"); }
139
146
  }
140
147
 
141
148
  /**
142
- * Subscribes to an event and adds a callback to be executed when the event is published.
149
+ * Subscribes to the `add` event of the set with a callback that will be executed when a value is added.
143
150
  *
144
151
  * ---
145
152
  *
146
153
  * @example
147
154
  * ```ts
148
- * const set = new SetView<number>();
149
- * const unsubscribe = set.subscribe("entry:add", (value: number) =>
150
- * {
151
- * if (value === 42) { unsubscribe(); }
152
- * console.log(`Added ${value}`);
153
- * });
155
+ * set.onAdd((value) => console.log(`Added ${value}`));
154
156
  *
155
157
  * set.add(2); // "Added 2"
156
158
  * set.add(42); // "Added 42"
157
- * set.add(4);
158
- * set.add(8);
159
159
  * ```
160
160
  *
161
161
  * ---
162
162
  *
163
- * @template K The key of the map containing the callback signature to subscribe.
164
- *
165
- * @param event The name of the event to subscribe to.
166
- * @param subscriber The callback to execute when the event is published.
163
+ * @param callback The callback that will be executed when a value is added to the set.
167
164
  *
168
- * @returns A function that can be used to unsubscribe the callback from the event.
165
+ * @returns A function that can be used to unsubscribe from the event.
169
166
  */
170
- public subscribe<K extends keyof SetViewEventsMap<T>>(
171
- event: K & string, subscriber: SetViewEventsMap<T>[K]
172
- ): Callback
167
+ public onAdd(callback: (value: T) => void): Callback
173
168
  {
174
- return this._publisher.subscribe(event, subscriber);
169
+ return this._publisher.subscribe("add", callback);
175
170
  }
176
171
 
177
172
  /**
178
- * Unsubscribes from an event and removes a callback from being executed when the event is published.
173
+ * Subscribes to the `remove` event of the set with a callback that will be executed when a value is removed.
179
174
  *
180
175
  * ---
181
176
  *
182
177
  * @example
183
178
  * ```ts
184
- * const callback = (value: number) => console.log(`Added ${value}`);
185
- * const set = new SetView<number>();
179
+ * set.onRemove((value) => console.log(`Removed ${value}`));
186
180
  *
187
- * set.subscribe("entry:add", callback);
188
- * set.add(2); // "Added 2"
181
+ * set.delete(2); // "Removed 2"
182
+ * set.delete(42); // "Removed 42"
183
+ * ```
184
+ *
185
+ * ---
189
186
  *
190
- * set.unsubscribe("entry:add", callback);
191
- * set.add(4);
187
+ * @param callback The callback that will be executed when a value is removed from the set.
188
+ *
189
+ * @returns A function that can be used to unsubscribe from the event.
190
+ */
191
+ public onRemove(callback: (value: T) => void): Callback
192
+ {
193
+ return this._publisher.subscribe("remove", callback);
194
+ }
195
+
196
+ /**
197
+ * Subscribes to the `clear` event of the set with a callback that will be executed when the set is cleared.
198
+ *
199
+ * ---
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * set.onClear(() => console.log("The set has all been cleared."));
204
+ * set.clear(); // "The set has all been cleared."
192
205
  * ```
193
206
  *
194
207
  * ---
195
208
  *
196
- * @template K The key of the map containing the callback signature to unsubscribe.
209
+ * @param callback The callback that will be executed when the set is cleared.
197
210
  *
198
- * @param event The name of the event to unsubscribe from.
199
- * @param subscriber The callback to remove from the event.
211
+ * @returns A function that can be used to unsubscribe from the event.
200
212
  */
201
- public unsubscribe<K extends keyof SetViewEventsMap<T>>(event: K & string, subscriber: SetViewEventsMap<T>[K]): void
213
+ public onClear(callback: () => void): Callback
202
214
  {
203
- this._publisher.unsubscribe(event, subscriber);
215
+ return this._publisher.subscribe("clear", callback);
204
216
  }
205
217
 
206
218
  public override readonly [Symbol.toStringTag]: string = "SetView";