@byloth/core 2.2.0 → 2.2.2

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.2",
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",
@@ -50,15 +50,15 @@
50
50
  "types": "src/index.ts",
51
51
  "devDependencies": {
52
52
  "@byloth/eslint-config-typescript": "^3.2.2",
53
- "@eslint/compat": "^1.4.1",
54
- "@types/node": "^22.19.0",
55
- "@vitest/coverage-v8": "^3.2.4",
53
+ "@eslint/compat": "^2.0.0",
54
+ "@types/node": "^22.19.1",
55
+ "@vitest/coverage-v8": "^4.0.12",
56
56
  "eslint": "^9.39.1",
57
57
  "husky": "^9.1.7",
58
- "jsdom": "^27.1.0",
58
+ "jsdom": "^27.2.0",
59
59
  "typescript": "^5.9.3",
60
- "vite": "^7.2.1",
61
- "vitest": "^3.2.4"
60
+ "vite": "^7.2.4",
61
+ "vitest": "^4.0.12"
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.2";
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 };
@@ -16,8 +16,7 @@
16
16
  * // Uncaught Exception: The game saves may be corrupted. Try to restart the game.
17
17
  * // at /src/game/index.ts:37:15
18
18
  * // at /src/main.ts:23:17
19
- * //
20
- * // Caused by SyntaxError: Unexpected end of JSON input
19
+ * // Caused by: SyntaxError: Unexpected end of JSON input
21
20
  * // at /src/models/saves.ts:47:17
22
21
  * // at /src/game/index.ts:12:9
23
22
  * // at /src/main.ts:23:17
@@ -59,6 +58,7 @@ export default class Exception extends Error
59
58
  const exc = new Exception(error.message);
60
59
 
61
60
  exc.stack = error.stack;
61
+ exc.cause = error.cause;
62
62
  exc.name = error.name;
63
63
 
64
64
  return exc;
@@ -89,18 +89,6 @@ export default class Exception extends Error
89
89
 
90
90
  this.cause = cause;
91
91
  this.name = name;
92
-
93
- if (cause)
94
- {
95
- if (cause instanceof Error)
96
- {
97
- this.stack += `\n\nCaused by ${cause.stack}`;
98
- }
99
- else
100
- {
101
- this.stack += `\n\nCaused by ${cause}`;
102
- }
103
- }
104
92
  }
105
93
 
106
94
  public readonly [Symbol.toStringTag]: string = "Exception";
@@ -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,
@@ -1066,7 +1066,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
1066
1066
  * {
1067
1067
  * try
1068
1068
  * {
1069
- * if (value > 5) { throw new Error("The index is too high."); }
1069
+ * if (value > 5) { throw new Exception("The index is too high."); }
1070
1070
  *
1071
1071
  * console.log(value); // 1, 2, 3, 4, 5
1072
1072
  * }
@@ -939,7 +939,7 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
939
939
  * {
940
940
  * try
941
941
  * {
942
- * if (value > 5) { throw new Error("The index is too high."); }
942
+ * if (value > 5) { throw new Exception("The index is too high."); }
943
943
  *
944
944
  * console.log(value); // 1, 2, 3, 4, 5
945
945
  * }
@@ -261,10 +261,10 @@ export default class SmartPromise<T = void> implements Promise<T>
261
261
  * ```ts
262
262
  * const promise = new SmartPromise((resolve, reject) =>
263
263
  * {
264
- * setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
264
+ * setTimeout(() => reject(new Exception("An unknown error occurred.")), 1_000);
265
265
  * });
266
266
  *
267
- * promise.catch(); // Uncaught Error: An unknown error occurred.
267
+ * promise.catch(); // "Uncaught Exception: An unknown error occurred."
268
268
  * ```
269
269
  *
270
270
  * ---
@@ -288,10 +288,10 @@ export default class SmartPromise<T = void> implements Promise<T>
288
288
  * ```ts
289
289
  * const promise = new SmartPromise((resolve, reject) =>
290
290
  * {
291
- * setTimeout(() => reject(new Error("An unknown error occurred.")), 1_000);
291
+ * setTimeout(() => reject(new Exception("An unknown error occurred.")), 1_000);
292
292
  * });
293
293
  *
294
- * promise.catch((reason) => console.error(reason)); // "Error: An unknown error occurred."
294
+ * promise.catch((reason) => console.error(reason)); // "Uncaught Exception: An unknown error occurred."
295
295
  * ```
296
296
  *
297
297
  * ---
@@ -21,7 +21,7 @@ import type { MaybePromise, PromiseExecutor } from "./types.js";
21
21
  *
22
22
  * promise
23
23
  * .then((result) => console.log(result)) // "Hello, World!"
24
- * .catch((error) => console.error(error)); // TimeoutException: The operation has timed out.
24
+ * .catch((error) => console.error(error)); // "Uncaught TimeoutException: The operation has timed out."
25
25
  * ```
26
26
  *
27
27
  * ---