@byloth/core 2.2.6 → 2.2.8

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.6",
3
+ "version": "2.2.8",
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",
@@ -49,16 +49,16 @@
49
49
  },
50
50
  "types": "src/index.ts",
51
51
  "devDependencies": {
52
- "@byloth/eslint-config-typescript": "^4.0.0",
53
- "@eslint/compat": "^2.0.3",
54
- "@types/node": "^22.19.15",
55
- "@vitest/coverage-v8": "^4.1.2",
56
- "eslint": "^10.1.0",
52
+ "@byloth/eslint-config-typescript": "^4.0.1",
53
+ "@eslint/compat": "^2.1.0",
54
+ "@types/node": "^24.12.3",
55
+ "@vitest/coverage-v8": "^4.1.5",
56
+ "eslint": "^10.3.0",
57
57
  "husky": "^9.1.7",
58
- "jsdom": "^29.0.1",
59
- "typescript": "^6.0.2",
60
- "vite": "^8.0.3",
61
- "vitest": "^4.1.2"
58
+ "jsdom": "^29.1.1",
59
+ "typescript": "^6.0.3",
60
+ "vite": "^8.0.11",
61
+ "vitest": "^4.1.5"
62
62
  },
63
63
  "scripts": {
64
64
  "dev": "vite",
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = "2.2.6";
1
+ export const VERSION = "2.2.8";
2
2
 
3
3
  export type { Constructor, Interval, Timeout, ValueOf } from "./core/types.js";
4
4
 
@@ -4,6 +4,12 @@ import CallableObject from "./callable-object.js";
4
4
  import type { Callback } from "./types.js";
5
5
 
6
6
  const Disabler = () => { /* ... */ };
7
+ const NotImplemented = () =>
8
+ {
9
+ throw new NotImplementedException(
10
+ "The `SwitchableCallback` has no callback defined yet. Did you forget to call the `register` method?"
11
+ );
12
+ };
7
13
 
8
14
  /**
9
15
  * A class representing a callback that can be switched between multiple implementations.
@@ -123,14 +129,7 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
123
129
  else
124
130
  {
125
131
  key = "";
126
-
127
- callback = ((() =>
128
- {
129
- throw new NotImplementedException(
130
- "The `SwitchableCallback` has no callback defined yet. " +
131
- "Did you forget to call the `register` method?"
132
- );
133
- }) as unknown) as T;
132
+ callback = (NotImplemented as unknown) as T;
134
133
  }
135
134
 
136
135
  this._key = key;
@@ -318,5 +317,63 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
318
317
  }
319
318
  }
320
319
 
320
+ /**
321
+ * Resets the callback to its initial state, unregistering all the implementations at once.
322
+ *
323
+ * After calling this method, the callback will behave as if it had just been constructed:
324
+ * a {@link NotImplementedException} will be thrown when invoked before registering a new implementation.
325
+ *
326
+ * This will also re-enable the object, restoring the {@link SwitchableCallback.isEnabled} state to `true`.
327
+ *
328
+ * ---
329
+ *
330
+ * @example
331
+ * ```ts
332
+ * onPointerMove.reset();
333
+ * ```
334
+ */
335
+ public reset(): void;
336
+
337
+ /**
338
+ * Resets the callback to its initial state, unregistering all the previous
339
+ * implementations at once and setting the given one as the new default.
340
+ *
341
+ * After calling this method, the callback will behave as if it had just
342
+ * been constructed with the specified implementation enabled by default.
343
+ *
344
+ * This will also re-enable the object, restoring the {@link SwitchableCallback.isEnabled} state to `true`.
345
+ *
346
+ * ---
347
+ *
348
+ * @example
349
+ * ```ts
350
+ * onPointerMove.reset((evt) => { [...] });
351
+ * ```
352
+ *
353
+ * ---
354
+ *
355
+ * @param callback The callback that will be executed when the object is invoked as a function by default.
356
+ * @param key The key that is associated by default to the given callback. Default is `default`.
357
+ */
358
+ public reset(callback: T, key?: string): void;
359
+ public reset(callback?: T, key = "default"): void
360
+ {
361
+ this._callbacks.clear();
362
+ this._isEnabled = true;
363
+
364
+ if (callback)
365
+ {
366
+ this._callbacks.set(key, callback);
367
+ }
368
+ else
369
+ {
370
+ key = "";
371
+ callback = (NotImplemented as unknown) as T;
372
+ }
373
+
374
+ this._key = key;
375
+ this._callback = callback;
376
+ }
377
+
321
378
  public override readonly [Symbol.toStringTag]: string = "SwitchableCallback";
322
379
  }