Skip to content

Commit ec64eed

Browse files
committed
add asterisk event
1 parent 0beaeaf commit ec64eed

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/connection.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export default class GameConnection<Ready extends boolean = false> {
6363
return this.#socket && this.#socket.readyState === this.#socket.OPEN;
6464
}
6565

66+
/**
67+
* Adds the listener function to the end of the listeners array for all
68+
* events. No checks are made to see if the listener has already been added.
69+
* Multiple calls passing the same combination of `eventName="*"` and listener
70+
* will result in the listener being added, and called, multiple times.
71+
*/
72+
public on<Event extends (typeof MessageTypes)[number] & keyof ReceiveEvents>(eventName: "*", cb: (eventName: Event, ...args: ReceiveEvents[Event]) => void): this;
73+
6674
/**
6775
* Adds the listener function to the end of the listeners array for the
6876
* event named `eventName`. No checks are made to see if the listener has
@@ -88,6 +96,12 @@ export default class GameConnection<Ready extends boolean = false> {
8896
return this;
8997
}
9098

99+
/**
100+
* Synchronously calls each of the listeners registered for all events (`eventName="*"`)
101+
* in the order they were registered, passing the supplied arguments to each.
102+
*/
103+
public emit<Index extends number, Event extends (typeof MessageTypes)[Index] & keyof ReceiveEvents>(listenerClass: "*", eventName: Event, ...args: ReceiveEvents[Event]): this;
104+
91105
/**
92106
* Synchronously calls each of the listeners registered for the event named `eventName`,
93107
* in the order they were registered, passing the supplied arguments to each.
@@ -152,6 +166,7 @@ export default class GameConnection<Ready extends boolean = false> {
152166
case GameConnection.HeaderBytes.Message:
153167
const messageId = buffer.read7BitInt();
154168
const args = buffer.deserialize();
169+
this.emit('*', GameConnection.MessageTypes[messageId] as any, ...args);
155170
this.emit(GameConnection.MessageTypes[messageId] as any, ...args);
156171
break;
157172
}
@@ -183,13 +198,13 @@ export default class GameConnection<Ready extends boolean = false> {
183198

184199
/**
185200
* @event PlayerInit
186-
*
201+
*
187202
* Upon receiving the `PlayerInit` event, the server requires
188203
* the client to send the `PlayerInit` event as well back to
189204
* the server.
190205
*/
191-
this.once('PlayerInit', () => {
192-
this.send('PlayerInit');
206+
this.once("PlayerInit", () => {
207+
this.send("PlayerInit");
193208
});
194209

195210
return this as GameConnection<true>;

0 commit comments

Comments
 (0)