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.
@@ -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, create an EventBus with the listeners you want to handle.
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
- * Next, define the listeners object. These are the event types and their
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': [handlerA, handlerB, handlerC],
27
+ * 'messageTypeA': handlerA,
25
28
  * } as const;
26
29
  *```
27
30
  *
28
- * Write a handler for the specific message type. They're handled serially.
29
- * Anything you return will be passed to the next listener as the payload.
31
+ * ### Third step
32
+ *
33
+ * Write a handler for the specific message type.
34
+ *
30
35
  * ```ts
31
- * function handlerA(payload: { text: string }) {
32
- * logger.log(payload.text);
33
- * // The payload is discarded!
34
- * }
36
+ * type ExpectedPayloadA = {
37
+ * text: string;
38
+ * };
35
39
  *
36
- * function handlerB(payload: { count: number }) {
37
- * // The next handler will receive this object as its payload.
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
- * function handlerC(payload: { count: number; otherData: string }) {
42
- * logger.log(`Count is ${payload.count} and otherData is ${payload.otherData}`);
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 Readonly<Record<string, Array<(payload: unknown) => void>>>> {
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<TType extends keyof TListeners>(message: {
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
+ };
@@ -1 +1 @@
1
- class e{listeners={};constructor(e){this.listeners=e}dispatch(e){let t=this.listeners[e.type]?.length||0;if(0===t)return void console.warn(`EventBus: No listeners for message type "${String(e.type)}"`);let s=e.payload;for(let r=0;r<t;r++)s=this.listeners[e.type]?.[r](s)}}export{e as EventBus};
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
@@ -67,5 +67,5 @@
67
67
  "test": "bun test"
68
68
  },
69
69
  "type": "module",
70
- "version": "5.5.0"
70
+ "version": "5.5.2"
71
71
  }