tgui-core 5.5.0 → 5.5.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.
- package/dist/common/eventbus.d.ts +32 -21
- package/dist/common/eventbus.js +1 -1
- package/package.json +1 -1
|
@@ -12,47 +12,58 @@
|
|
|
12
12
|
*
|
|
13
13
|
* @usage
|
|
14
14
|
*
|
|
15
|
-
* First
|
|
15
|
+
* ### First step
|
|
16
|
+
*
|
|
17
|
+
* Create an EventBus with the listeners you want to handle.
|
|
18
|
+
*
|
|
16
19
|
* ```ts
|
|
17
20
|
* const bus = new EventBus(listeners);
|
|
18
21
|
*```
|
|
19
22
|
*
|
|
20
|
-
*
|
|
23
|
+
* ### Second step
|
|
24
|
+
*
|
|
25
|
+
* Next, define the listeners object. These are the event types and their
|
|
21
26
|
* corresponding callbacks.
|
|
27
|
+
*
|
|
22
28
|
* ```ts
|
|
23
29
|
* const listeners = {
|
|
24
|
-
* 'messageTypeA':
|
|
30
|
+
* 'messageTypeA': handlerA,
|
|
25
31
|
* } as const;
|
|
26
32
|
*```
|
|
27
33
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
34
|
+
* ### Third step
|
|
35
|
+
*
|
|
36
|
+
* Write a handler for the specific message type.
|
|
37
|
+
*
|
|
30
38
|
* ```ts
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* }
|
|
39
|
+
* type ExpectedPayloadA = {
|
|
40
|
+
* text: string;
|
|
41
|
+
* };
|
|
35
42
|
*
|
|
36
|
-
* function
|
|
37
|
-
*
|
|
38
|
-
* return { count: payload.count + 1, otherData: 'foo' };
|
|
43
|
+
* function handlerA(payload: ExpectedPayloadA) {
|
|
44
|
+
* logger.log(payload.text);
|
|
39
45
|
* }
|
|
46
|
+
*```
|
|
47
|
+
|
|
48
|
+
* You can now dispatch the messageTypeA event! If you want to shorten the
|
|
49
|
+
* syntax, you can name the handler function after the incoming event type.
|
|
40
50
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
51
|
+
* ````ts
|
|
52
|
+
* function messageTypeA(payload: ExpectedPayloadA) {
|
|
53
|
+
* logger.log(payload.text);
|
|
43
54
|
* }
|
|
44
|
-
|
|
55
|
+
*
|
|
56
|
+
* const listeners = {
|
|
57
|
+
* messageTypeA,
|
|
58
|
+
* } as const;
|
|
59
|
+
*
|
|
45
60
|
*/
|
|
46
|
-
export declare class EventBus<TListeners extends Readonly<Record<string,
|
|
61
|
+
export declare class EventBus<TListeners extends Readonly<Record<string, (payload: unknown) => void>>> {
|
|
47
62
|
private listeners;
|
|
48
63
|
constructor(initialListeners: TListeners);
|
|
49
64
|
/** Dispatch a message to the appropriate listener. */
|
|
50
65
|
dispatch<TType extends keyof TListeners>(message: {
|
|
51
66
|
type: TType;
|
|
52
|
-
|
|
53
|
-
* This should match the first listener's payload type. It can be passed
|
|
54
|
-
* along and mutated by each listener in the chain by returning a new value.
|
|
55
|
-
*/
|
|
56
|
-
payload?: Parameters<TListeners[TType][0]>[0];
|
|
67
|
+
payload?: Parameters<TListeners[TType]>[0];
|
|
57
68
|
}): void;
|
|
58
69
|
}
|
package/dist/common/eventbus.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class s{listeners={};constructor(s){this.listeners=s}dispatch(s){this.listeners[s.type]?.(s.payload)}}export{s as EventBus};
|
package/package.json
CHANGED