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.
@@ -12,47 +12,58 @@
12
12
  *
13
13
  * @usage
14
14
  *
15
- * First, create an EventBus with the listeners you want to handle.
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
- * Next, define the listeners object. These are the event types and their
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': [handlerA, handlerB, handlerC],
30
+ * 'messageTypeA': handlerA,
25
31
  * } as const;
26
32
  *```
27
33
  *
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.
34
+ * ### Third step
35
+ *
36
+ * Write a handler for the specific message type.
37
+ *
30
38
  * ```ts
31
- * function handlerA(payload: { text: string }) {
32
- * logger.log(payload.text);
33
- * // The payload is discarded!
34
- * }
39
+ * type ExpectedPayloadA = {
40
+ * text: string;
41
+ * };
35
42
  *
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' };
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
- * function handlerC(payload: { count: number; otherData: string }) {
42
- * logger.log(`Count is ${payload.count} and otherData is ${payload.otherData}`);
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, Array<(payload: unknown) => void>>>> {
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
  }
@@ -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){this.listeners[s.type]?.(s.payload)}}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.1"
71
71
  }