@byloth/core 2.0.0 → 2.0.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.
@@ -3,6 +3,8 @@ import { KeyException, NotImplementedException, RuntimeException } from "../exce
3
3
  import CallableObject from "./callable-object.js";
4
4
  import type { Callback } from "./types.js";
5
5
 
6
+ const Disabler = () => { /* ... */ };
7
+
6
8
  /**
7
9
  * A class representing a callback that can be switched between multiple implementations.
8
10
  *
@@ -76,28 +78,60 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
76
78
  /**
77
79
  * Initializes a new instance of the {@link SwitchableCallback} class.
78
80
  *
81
+ * ---
82
+ *
83
+ * @example
79
84
  * ```ts
80
85
  * const onPointerMove = new SwitchableCallback<(evt: PointerEvent) => void>();
81
86
  * ```
82
87
  */
83
- public constructor()
84
- {
85
- const _default = () =>
86
- {
87
- throw new NotImplementedException(
88
- "The `SwitchableCallback` has no callback defined yet. " +
89
- "Did you forget to call the `register` method?"
90
- );
91
- };
88
+ public constructor();
92
89
 
90
+ /**
91
+ * Initializes a new instance of the {@link SwitchableCallback}
92
+ * class with the specified callback enabled by default.
93
+ *
94
+ * ---
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * const onPointerMove = new SwitchableCallback<(evt: PointerEvent) => void>((evt) => { [...] });
99
+ * ```
100
+ *
101
+ * ---
102
+ *
103
+ * @param callback The callback that will be executed when the object is invoked as a function by default.
104
+ * @param key The key that is associated by default to the given callback. Default is `default`.
105
+ */
106
+ public constructor(callback: T, key?: string);
107
+ public constructor(callback?: T, key = "default")
108
+ {
93
109
  super();
94
110
 
95
- this._callback = ((_default) as unknown) as T;
96
111
  this._callbacks = new Map<string, T>();
97
-
98
112
  this._isEnabled = true;
99
- this._key = "";
100
113
 
114
+ if (callback)
115
+ {
116
+ this._callbacks.set(key, callback);
117
+ }
118
+ else
119
+ {
120
+ key = "";
121
+
122
+ callback = ((() =>
123
+ {
124
+ throw new NotImplementedException(
125
+ "The `SwitchableCallback` has no callback defined yet. " +
126
+ "Did you forget to call the `register` method?"
127
+ );
128
+
129
+ }) as unknown) as T;
130
+ }
131
+
132
+ this._key = key;
133
+
134
+ this._callback = callback;
101
135
  this._invoke = (...args: Parameters<T>): ReturnType<T> => this._callback(...args);
102
136
  }
103
137
 
@@ -106,28 +140,50 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
106
140
  *
107
141
  * Also note that:
108
142
  * - If any implementation has been registered yet, a {@link KeyException} will be thrown.
143
+ * - If any key is given and it doesn't have any associated
144
+ * implementation yet, a {@link KeyException} will be thrown.
109
145
  * - If the callback is already enabled, a {@link RuntimeException} will be thrown.
110
146
  *
147
+ * ---
148
+ *
149
+ * @example
111
150
  * ```ts
112
151
  * window.addEventListener("pointerdown", () => { onPointerMove.enable(); });
113
152
  * window.addEventListener("pointermove", onPointerMove);
114
153
  * ```
154
+ *
155
+ * @param key
156
+ * The key that is associated with the implementation to enable. Default is the currently selected implementation.
115
157
  */
116
- public enable(): void
158
+ public enable(key?: string): void
117
159
  {
118
- if (!(this._key))
160
+ if (key === undefined)
119
161
  {
120
- throw new KeyException(
121
- "The `SwitchableCallback` has no callback defined yet. " +
122
- "Did you forget to call the `register` method?"
123
- );
162
+ if (!(this._key))
163
+ {
164
+ throw new KeyException(
165
+ "The `SwitchableCallback` has no callback defined yet. " +
166
+ "Did you forget to call the `register` method?"
167
+ );
168
+ }
169
+
170
+ key = this._key;
171
+ }
172
+ else if (!(key))
173
+ {
174
+ throw new KeyException("The key must be a non-empty string.");
124
175
  }
176
+ else if (!(this._callbacks.has(key)))
177
+ {
178
+ throw new KeyException(`The key '${key}' doesn't yet have any associated callback.`);
179
+ }
180
+
125
181
  if (this._isEnabled)
126
182
  {
127
183
  throw new RuntimeException("The `SwitchableCallback` is already enabled.");
128
184
  }
129
185
 
130
- this._callback = this._callbacks.get(this._key)!;
186
+ this._callback = this._callbacks.get(key)!;
131
187
  this._isEnabled = true;
132
188
  }
133
189
 
@@ -136,6 +192,9 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
136
192
  *
137
193
  * If the callback is already disabled, a {@link RuntimeException} will be thrown.
138
194
  *
195
+ * ---
196
+ *
197
+ * @example
139
198
  * ```ts
140
199
  * window.addEventListener("pointermove", onPointerMove);
141
200
  * window.addEventListener("pointerup", () => { onPointerMove.disable(); });
@@ -148,8 +207,7 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
148
207
  throw new RuntimeException("The `SwitchableCallback` is already disabled.");
149
208
  }
150
209
 
151
- // eslint-disable-next-line @typescript-eslint/no-empty-function
152
- this._callback = (() => { }) as T;
210
+ this._callback = Disabler as T;
153
211
  this._isEnabled = false;
154
212
  }
155
213
 
@@ -160,11 +218,16 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
160
218
  * - If the callback has no other implementation registered yet, this one will be selected as default.
161
219
  * - If the key has already been used for another implementation, a {@link KeyException} will be thrown.
162
220
  *
221
+ * ---
222
+ *
223
+ * @example
163
224
  * ```ts
164
225
  * onPointerMove.register("pressed", () => { [...] });
165
226
  * onPointerMove.register("released", () => { [...] });
166
227
  * ```
167
228
  *
229
+ * ---
230
+ *
168
231
  * @param key The key that will be associated with the implementation.
169
232
  * @param callback The implementation to register.
170
233
  */
@@ -190,10 +253,15 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
190
253
  * - If the key is the currently selected implementation, a {@link KeyException} will be thrown.
191
254
  * - If the key has no associated implementation yet, a {@link KeyException} will be thrown.
192
255
  *
256
+ * ---
257
+ *
258
+ * @example
193
259
  * ```ts
194
260
  * onPointerMove.unregister("released");
195
261
  * ```
196
262
  *
263
+ * ---
264
+ *
197
265
  * @param key The key that is associated with the implementation to unregister.
198
266
  */
199
267
  public unregister(key: string): void
@@ -215,12 +283,17 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
215
283
  *
216
284
  * If the key has no associated implementation yet, a {@link KeyException} will be thrown.
217
285
  *
286
+ * ---
287
+ *
288
+ * @example
218
289
  * ```ts
219
290
  * window.addEventListener("pointerdown", () => { onPointerMove.switch("pressed"); });
220
291
  * window.addEventListener("pointermove", onPointerMove);
221
292
  * window.addEventListener("pointerup", () => { onPointerMove.switch("released"); });
222
293
  * ```
223
294
  *
295
+ * ---
296
+ *
224
297
  * @param key The key that is associated with the implementation to switch to.
225
298
  */
226
299
  public switch(key: string): void
@@ -26,6 +26,9 @@ export default class Exception extends Error
26
26
  /**
27
27
  * A static method to convert a generic caught error, ensuring it's an instance of the {@link Exception} class.
28
28
  *
29
+ * ---
30
+ *
31
+ * @example
29
32
  * ```ts
30
33
  * try { [...] }
31
34
  * catch (error)
@@ -36,6 +39,8 @@ export default class Exception extends Error
36
39
  * }
37
40
  * ```
38
41
  *
42
+ * ---
43
+ *
39
44
  * @param error The caught error to convert.
40
45
  *
41
46
  * @returns An instance of the {@link Exception} class.
@@ -62,10 +67,15 @@ export default class Exception extends Error
62
67
  /**
63
68
  * Initializes a new instance of the {@link Exception} class.
64
69
  *
70
+ * ---
71
+ *
72
+ * @example
65
73
  * ```ts
66
74
  * throw new Exception("An error occurred while processing the request.");
67
75
  * ```
68
76
  *
77
+ * ---
78
+ *
69
79
  * @param message The message that describes the error.
70
80
  * @param cause The previous caught error that caused this one, if any.
71
81
  * @param name The name of the exception. Default is `"Exception"`.
@@ -117,10 +127,15 @@ export class FatalErrorException extends Exception
117
127
  /**
118
128
  * Initializes a new instance of the {@link FatalErrorException} class.
119
129
  *
130
+ * ---
131
+ *
132
+ * @example
120
133
  * ```ts
121
134
  * throw new FatalErrorException("This error should never happen. Please, contact the support team.");
122
135
  * ```
123
136
  *
137
+ * ---
138
+ *
124
139
  * @param message The message that describes the error.
125
140
  * @param cause The previous caught error that caused this one, if any.
126
141
  * @param name The name of the exception. Default is `"FatalErrorException"`.
@@ -160,10 +175,15 @@ export class NotImplementedException extends FatalErrorException
160
175
  /**
161
176
  * Initializes a new instance of the {@link NotImplementedException} class.
162
177
  *
178
+ * ---
179
+ *
180
+ * @example
163
181
  * ```ts
164
182
  * throw new NotImplementedException("This method hasn't been implemented yet. Check back later.");
165
183
  * ```
166
184
  *
185
+ * ---
186
+ *
167
187
  * @param message The message that describes the error.
168
188
  * @param cause The previous caught error that caused this one, if any.
169
189
  * @param name The name of the exception. Default is `"NotImplementedException"`.
@@ -22,10 +22,15 @@ export class FileException extends Exception
22
22
  /**
23
23
  * Initializes a new instance of the {@link FileException} class.
24
24
  *
25
+ * ---
26
+ *
27
+ * @example
25
28
  * ```ts
26
29
  * throw new FileException("An error occurred while trying to read the file.");
27
30
  * ```
28
31
  *
32
+ * ---
33
+ *
29
34
  * @param message The message that describes the error.
30
35
  * @param cause The previous caught error that caused this one, if any.
31
36
  * @param name The name of the exception. Default is `"FileException"`.
@@ -55,10 +60,15 @@ export class FileExistsException extends FileException
55
60
  /**
56
61
  * Initializes a new instance of the {@link FileExistsException} class.
57
62
  *
63
+ * ---
64
+ *
65
+ * @example
58
66
  * ```ts
59
67
  * throw new FileExistsException("The file named 'data.json' already exists on the server.");
60
68
  * ```
61
69
  *
70
+ * ---
71
+ *
62
72
  * @param message The message that describes the error.
63
73
  * @param cause The previous caught error that caused this one, if any.
64
74
  * @param name The name of the exception. Default is `"FileExistsException"`.
@@ -88,10 +98,15 @@ export class FileNotFoundException extends FileException
88
98
  /**
89
99
  * Initializes a new instance of the {@link FileNotFoundException} class.
90
100
  *
101
+ * ---
102
+ *
103
+ * @example
91
104
  * ```ts
92
105
  * throw new FileNotFoundException("The file named 'data.json' wasn't found on the server.");
93
106
  * ```
94
107
  *
108
+ * ---
109
+ *
95
110
  * @param message The message that describes the error.
96
111
  * @param cause The previous caught error that caused this one, if any.
97
112
  * @param name The name of the exception. Default is `"FileNotFoundException"`.
@@ -121,10 +136,15 @@ export class KeyException extends Exception
121
136
  /**
122
137
  * Initializes a new instance of the {@link KeyException} class.
123
138
  *
139
+ * ---
140
+ *
141
+ * @example
124
142
  * ```ts
125
143
  * throw new KeyException("The 'id' key wasn't found in the dictionary.");
126
144
  * ```
127
145
  *
146
+ * ---
147
+ *
128
148
  * @param message The message that describes the error.
129
149
  * @param cause The previous caught error that caused this one, if any.
130
150
  * @param name The name of the exception. Default is `"KeyException"`.
@@ -162,10 +182,15 @@ export class NetworkException extends Exception
162
182
  /**
163
183
  * Initializes a new instance of the {@link NetworkException} class.
164
184
  *
185
+ * ---
186
+ *
187
+ * @example
165
188
  * ```ts
166
189
  * throw new NetworkException("Couldn't connect to the server. Please, try again later.");
167
190
  * ```
168
191
  *
192
+ * ---
193
+ *
169
194
  * @param message The message that describes the error.
170
195
  * @param cause The previous caught error that caused this one, if any.
171
196
  * @param name The name of the exception. Default is `"NetworkException"`.
@@ -195,10 +220,15 @@ export class PermissionException extends Exception
195
220
  /**
196
221
  * Initializes a new instance of the {@link PermissionException} class.
197
222
  *
223
+ * ---
224
+ *
225
+ * @example
198
226
  * ```ts
199
227
  * throw new PermissionException("You don't have permission to access this resource.");
200
228
  * ```
201
229
  *
230
+ * ---
231
+ *
202
232
  * @param message The message that describes the error.
203
233
  * @param cause The previous caught error that caused this one, if any.
204
234
  * @param name The name of the exception. Default is `"PermissionException"`.
@@ -228,10 +258,15 @@ export class ReferenceException extends Exception
228
258
  /**
229
259
  * Initializes a new instance of the {@link ReferenceException} class.
230
260
  *
261
+ * ---
262
+ *
263
+ * @example
231
264
  * ```ts
232
265
  * throw new ReferenceException("The 'canvas' element wasn't found in the document.");
233
266
  * ```
234
267
  *
268
+ * ---
269
+ *
235
270
  * @param message The message that describes the error.
236
271
  * @param cause The previous caught error that caused this one, if any.
237
272
  * @param name The name of the exception. Default is `"ReferenceException"`.
@@ -263,10 +298,15 @@ export class RuntimeException extends Exception
263
298
  /**
264
299
  * Initializes a new instance of the {@link RuntimeException} class.
265
300
  *
301
+ * ---
302
+ *
303
+ * @example
266
304
  * ```ts
267
305
  * throw new RuntimeException("The received input seems to be malformed or corrupted.");
268
306
  * ```
269
307
  *
308
+ * ---
309
+ *
270
310
  * @param message The message that describes the error.
271
311
  * @param cause The previous caught error that caused this one, if any.
272
312
  * @param name The name of the exception. Default is `"RuntimeException"`.
@@ -296,10 +336,15 @@ export class EnvironmentException extends RuntimeException
296
336
  /**
297
337
  * Initializes a new instance of the {@link EnvironmentException} class.
298
338
  *
339
+ * ---
340
+ *
341
+ * @example
299
342
  * ```ts
300
343
  * throw new EnvironmentException("The required environment variable 'API_KEY' isn't set.");
301
344
  * ```
302
345
  *
346
+ * ---
347
+ *
303
348
  * @param message The message that describes the error.
304
349
  * @param cause The previous caught error that caused this one, if any.
305
350
  * @param name The name of the exception. Default is `"EnvironmentException"`.
@@ -328,10 +373,15 @@ export class TimeoutException extends Exception
328
373
  /**
329
374
  * Initializes a new instance of the {@link TimeoutException} class.
330
375
  *
376
+ * ---
377
+ *
378
+ * @example
331
379
  * ```ts
332
380
  * throw new TimeoutException("The task took too long to complete.");
333
381
  * ```
334
382
  *
383
+ * ---
384
+ *
335
385
  * @param message The message that describes the error.
336
386
  * @param cause The previous caught error that caused this one, if any.
337
387
  * @param name The name of the exception. Default is `"TimeoutException"`.
@@ -363,10 +413,15 @@ export class TypeException extends Exception
363
413
  /**
364
414
  * Initializes a new instance of the {@link TypeException} class.
365
415
  *
416
+ * ---
417
+ *
418
+ * @example
366
419
  * ```ts
367
420
  * throw new TypeException("The 'username' argument must be a valid string.");
368
421
  * ```
369
422
  *
423
+ * ---
424
+ *
370
425
  * @param message The message that describes the error.
371
426
  * @param cause The previous caught error that caused this one, if any.
372
427
  * @param name The name of the exception. Default is `"TypeException"`.
@@ -398,10 +453,15 @@ export class ValueException extends Exception
398
453
  /**
399
454
  * Initializes a new instance of the {@link ValueException} class.
400
455
  *
456
+ * ---
457
+ *
458
+ * @example
401
459
  * ```ts
402
460
  * throw new ValueException("The 'grade' argument cannot be negative.");
403
461
  * ```
404
462
  *
463
+ * ---
464
+ *
405
465
  * @param message The message that describes the error.
406
466
  * @param cause The previous caught error that caused this one, if any.
407
467
  * @param name The name of the exception. Default is `"ValueException"`.
@@ -433,10 +493,15 @@ export class RangeException extends ValueException
433
493
  /**
434
494
  * Initializes a new instance of the {@link RangeException} class.
435
495
  *
496
+ * ---
497
+ *
498
+ * @example
436
499
  * ```ts
437
500
  * throw new RangeException("The 'percentage' argument must be between 0 and 100.");
438
501
  * ```
439
502
  *
503
+ * ---
504
+ *
440
505
  * @param message The message that describes the error.
441
506
  * @param cause The previous caught error that caused this one, if any.
442
507
  * @param name The name of the exception. Default is `"RangeException"`.