v0.10.0
Class: EventBus
EventBus
A wrapper around Node.js EventEmitter that provides a central event bus for the entire application. This class is responsible for dispatching events and registering event handlers.
Event naming convention: Events follow a namespaced pattern with colon separators: “domain:action” For example: “player:connected”, “table:player:joined”, etc.
Use the EventTypes constants to ensure consistent event naming across the application.
External Usage: Developers can extend the EventBus with their own custom events:
// Define your custom events
const MY_EVENTS = {
CUSTOM_ACTION: "myGame:customAction"
} as const;
// Use the predefined event constants in your code
eventBus.on(MY_EVENTS.CUSTOM_ACTION, (data) => {
console.log(`Custom action received: ${data}`);
});
// You can also use string literals, but you lose type safety
eventBus.on("myGame:anotherAction", (data) => {
console.log(`Another action received: ${data}`);
});
Table of contents
Constructors
Methods
Constructors
constructor
• new EventBus(): EventBus
Returns
Methods
debugMonitor
▸ debugMonitor(«destructured»?): void
Debug monitor for all events Logs all events and their payloads to the console Useful during development or debugging
Parameters
| Name | Type | Default value |
|---|---|---|
«destructured» | Object | {} |
› enabled? | boolean | true |
› filter? | (event: string) => boolean | undefined |
› logger? | (event: string, …args: unknown[]) => void | console.log |
Returns
void
emit
▸ emit(event, payload?): boolean
Emit an event
Parameters
| Name | Type | Description |
|---|---|---|
event | string | The event to emit |
payload | unknown | - |
Returns
boolean
Whether the event had listeners
listenerCount
▸ listenerCount(event): number
Get the number of listeners for an event
Parameters
| Name | Type | Description |
|---|---|---|
event | Object | The event to check |
event.event | string | - |
Returns
number
The number of listeners for the event
off
▸ off<T>(«destructured»): void
Remove an event listener
Type parameters
| Name | Type |
|---|---|
T | unknown |
Parameters
| Name | Type |
|---|---|
«destructured» | Object |
› event | string |
› listener | (payload: T) => void |
Returns
void
on
▸ on<T>(«destructured»): void
Register an event listener
Type parameters
| Name | Type |
|---|---|
T | unknown |
Parameters
| Name | Type |
|---|---|
«destructured» | Object |
› event | string |
› listener | (payload: T) => void |
Returns
void
once
▸ once<T>(«destructured»): void
Register a one-time event listener
Type parameters
| Name | Type |
|---|---|
T | unknown |
Parameters
| Name | Type |
|---|---|
«destructured» | Object |
› event | string |
› listener | (payload: T) => void |
Returns
void