tgui-core 5.4.2 → 5.5.0

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.
@@ -11,39 +11,48 @@
11
11
  * and update accordingly.
12
12
  *
13
13
  * @usage
14
+ *
15
+ * First, create an EventBus with the listeners you want to handle.
14
16
  * ```ts
15
17
  * const bus = new EventBus(listeners);
18
+ *```
16
19
  *
17
- * // These are the event types and their corresponding callbacks.
20
+ * Next, define the listeners object. These are the event types and their
21
+ * corresponding callbacks.
22
+ * ```ts
18
23
  * const listeners = {
19
- * 'messageTypeA': (message) => { logger.log(message.payload); },
24
+ * 'messageTypeA': [handlerA, handlerB, handlerC],
20
25
  * } as const;
26
+ *```
21
27
  *
22
- * // You can further shorten this by linking a function to the event type:
23
- * function messageTypeA(payload: { text: string }) {
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.
30
+ * ```ts
31
+ * function handlerA(payload: { text: string }) {
24
32
  * logger.log(payload.text);
33
+ * // The payload is discarded!
25
34
  * }
26
35
  *
27
- * const listeners = {
28
- * messageTypeA,
29
- * } as const;
30
- *
31
- *
32
- * // Later, dispatch a message:
33
- * const message: ByondMessage = {
34
- * type: 'messageTypeA',
35
- * payload: { text: 'Hello, world!' },
36
- * };
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' };
39
+ * }
37
40
  *
38
- * bus.dispatch(message);
39
- * ```
41
+ * function handlerC(payload: { count: number; otherData: string }) {
42
+ * logger.log(`Count is ${payload.count} and otherData is ${payload.otherData}`);
43
+ * }
44
+ *```
40
45
  */
41
- export declare class EventBus<TListeners extends Readonly<Record<string, (payload: unknown) => void>>> {
46
+ export declare class EventBus<TListeners extends Readonly<Record<string, Array<(payload: unknown) => void>>>> {
42
47
  private listeners;
43
48
  constructor(initialListeners: TListeners);
44
49
  /** Dispatch a message to the appropriate listener. */
45
50
  dispatch<TType extends keyof TListeners>(message: {
46
51
  type: TType;
47
- payload?: Parameters<TListeners[TType]>[0];
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];
48
57
  }): void;
49
58
  }
@@ -1 +1 @@
1
- class s{listeners={};constructor(s){this.listeners=s}dispatch(s){this.listeners[s.type]?.(s.payload)}}export{s as EventBus};
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};
package/package.json CHANGED
@@ -67,5 +67,5 @@
67
67
  "test": "bun test"
68
68
  },
69
69
  "type": "module",
70
- "version": "5.4.2"
70
+ "version": "5.5.0"
71
71
  }