@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/dist/core.js +204 -188
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/models/callbacks/callable-object.ts +2 -0
- package/src/models/callbacks/publisher.ts +24 -5
- package/src/models/callbacks/switchable-callback.ts +2 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -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
|
-
|
|
41
|
-
|
|
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";
|