tgui-core 5.5.0 → 5.5.2
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 +37 -27
- package/dist/common/eventbus.js +1 -1
- package/package.json +1 -1
|
@@ -3,56 +3,66 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Handles different event messages from byond and TGUI.
|
|
5
5
|
*
|
|
6
|
-
* Don't fret! This is a simple class under the hood. Using some typescript-fu,
|
|
7
|
-
* it's able to provide type safety for the event types and their payloads.
|
|
8
|
-
*
|
|
9
6
|
* The philosophy: This interacts directly with state managers in vanilla JS,
|
|
10
7
|
* offering a way to handle browser events. UIs then subscribe to these states
|
|
11
8
|
* and update accordingly.
|
|
12
9
|
*
|
|
13
10
|
* @usage
|
|
14
11
|
*
|
|
15
|
-
* First
|
|
12
|
+
* ### First step
|
|
13
|
+
*
|
|
14
|
+
* Create an EventBus with the listeners you want to handle.
|
|
15
|
+
*
|
|
16
16
|
* ```ts
|
|
17
17
|
* const bus = new EventBus(listeners);
|
|
18
18
|
*```
|
|
19
19
|
*
|
|
20
|
-
*
|
|
20
|
+
* ### Second step
|
|
21
|
+
*
|
|
22
|
+
* Next, define the listeners object. These are the event types and their
|
|
21
23
|
* corresponding callbacks.
|
|
24
|
+
*
|
|
22
25
|
* ```ts
|
|
23
26
|
* const listeners = {
|
|
24
|
-
* 'messageTypeA':
|
|
27
|
+
* 'messageTypeA': handlerA,
|
|
25
28
|
* } as const;
|
|
26
29
|
*```
|
|
27
30
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
31
|
+
* ### Third step
|
|
32
|
+
*
|
|
33
|
+
* Write a handler for the specific message type.
|
|
34
|
+
*
|
|
30
35
|
* ```ts
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* }
|
|
36
|
+
* type ExpectedPayloadA = {
|
|
37
|
+
* text: string;
|
|
38
|
+
* };
|
|
35
39
|
*
|
|
36
|
-
* function
|
|
37
|
-
*
|
|
38
|
-
* return { count: payload.count + 1, otherData: 'foo' };
|
|
40
|
+
* function handlerA(payload: ExpectedPayloadA) {
|
|
41
|
+
* logger.log(payload.text);
|
|
39
42
|
* }
|
|
43
|
+
*```
|
|
44
|
+
|
|
45
|
+
* You can now dispatch the messageTypeA event! If you want to shorten the
|
|
46
|
+
* syntax, you can name the handler function after the incoming event type.
|
|
40
47
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
48
|
+
* ````ts
|
|
49
|
+
* function messageTypeA(payload: ExpectedPayloadA) {
|
|
50
|
+
* logger.log(payload.text);
|
|
43
51
|
* }
|
|
44
|
-
|
|
52
|
+
*
|
|
53
|
+
* const listeners = {
|
|
54
|
+
* messageTypeA,
|
|
55
|
+
* } as const;
|
|
56
|
+
*
|
|
45
57
|
*/
|
|
46
|
-
export declare class EventBus<TListeners extends
|
|
58
|
+
export declare class EventBus<TListeners extends ListenerMap> {
|
|
47
59
|
private listeners;
|
|
48
60
|
constructor(initialListeners: TListeners);
|
|
49
61
|
/** Dispatch a message to the appropriate listener. */
|
|
50
|
-
dispatch
|
|
51
|
-
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];
|
|
57
|
-
}): void;
|
|
62
|
+
dispatch(message: Message): void;
|
|
58
63
|
}
|
|
64
|
+
export type ListenerMap = Record<string, (payload?: unknown) => void>;
|
|
65
|
+
export type Message = {
|
|
66
|
+
type: string;
|
|
67
|
+
payload?: unknown;
|
|
68
|
+
};
|
package/dist/common/eventbus.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
class
|
|
1
|
+
class s{listeners;constructor(s){this.listeners=s}dispatch(s){let t=this.listeners[s.type];"payload"in s?t(s.payload):t()}}export{s as EventBus};
|
package/package.json
CHANGED