@byloth/core 2.0.0-rc.10 → 2.0.0-rc.11

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.0.0-rc.10",
3
+ "version": "2.0.0-rc.11",
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",
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = "2.0.0-rc.10";
1
+ export const VERSION = "2.0.0-rc.11";
2
2
 
3
3
  export type { Constructor, Interval, Timeout } from "./core/types.js";
4
4
 
@@ -20,4 +20,6 @@ export default abstract class CallableObject<T extends Callback<any[], any> = ()
20
20
  }
21
21
 
22
22
  public abstract invoke(...args: Parameters<T>): ReturnType<T>;
23
+
24
+ public readonly [Symbol.toStringTag]: string = "CallableObject";
23
25
  }
@@ -12,6 +12,20 @@ export default class Publisher<T extends { [K in keyof T]: Callback<any[], any>
12
12
  this._subscribers = new Map();
13
13
  }
14
14
 
15
+ public clear(): void
16
+ {
17
+ this._subscribers.clear();
18
+ }
19
+
20
+ public publish<K extends keyof T>(event: K, ...args: Parameters<T[K]>): ReturnType<T[K]>[]
21
+ {
22
+ const subscribers = this._subscribers.get(event);
23
+ if (!(subscribers)) { return []; }
24
+
25
+ return subscribers.slice()
26
+ .map((subscriber) => subscriber(...args)) as ReturnType<T[K]>[];
27
+ }
28
+
15
29
  public subscribe<K extends keyof T>(event: K, subscriber: T[K]): () => void
16
30
  {
17
31
  if (!(this._subscribers.has(event))) { this._subscribers.set(event, []); }
@@ -31,14 +45,19 @@ export default class Publisher<T extends { [K in keyof T]: Callback<any[], any>
31
45
  subscribers.splice(index, 1);
32
46
  };
33
47
  }
34
-
35
- public publish<K extends keyof T>(event: K, ...args: Parameters<T[K]>): ReturnType<T[K]>[]
48
+ public unsubscribe<K extends keyof T>(event: K, subscriber: T[K]): void
36
49
  {
37
50
  const subscribers = this._subscribers.get(event);
38
- if (!(subscribers)) { return []; }
51
+ if (!(subscribers)) { return; }
39
52
 
40
- return subscribers.slice()
41
- .map((subscriber) => subscriber(...args)) as ReturnType<T[K]>[];
53
+ const index = subscribers.indexOf(subscriber);
54
+ if (index < 0)
55
+ {
56
+ throw new ReferenceException("Unable to unsubscribe the required subscriber. " +
57
+ "The subscription was already unsubscribed or was never subscribed.");
58
+ }
59
+
60
+ subscribers.splice(index, 1);
42
61
  }
43
62
 
44
63
  public readonly [Symbol.toStringTag]: string = "Publisher";
@@ -105,4 +105,6 @@ export default class SwitchableCallback<T extends Callback<any[], any> = Callbac
105
105
  this._callback = this._callbacks.get(key)!;
106
106
  }
107
107
  }
108
+
109
+ public readonly [Symbol.toStringTag]: string = "SwitchableCallback";
108
110
  }