@byloth/core 2.2.0 → 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.2.0",
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.2.0";
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,
@@ -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 };
@@ -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,