v0.8.2
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(enabled?
, filter?
, logger?
): 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 | Description |
---|---|---|---|
enabled | boolean | true | Whether to enable debug monitoring |
filter? | (event : string ) => boolean | undefined | Optional filter function to only log certain events |
logger | (event : string , âŚargs : any []) => void | console.log | Custom logger function (defaults to console.log) |
Returns
void
emit
⸠emit(event
, ...args
): boolean
Emit an event
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to emit |
...args | any [] | Arguments to pass to event listeners |
Returns
boolean
Whether the event had listeners
listenerCount
⸠listenerCount(event
): number
Get the number of listeners for an event
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to check |
Returns
number
The number of listeners for the event
off
⸠off(event
, listener
): void
Remove an event listener
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to stop listening for |
listener | (âŚargs : any []) => void | The callback function to remove |
Returns
void
on
⸠on(event
, listener
): void
Register an event listener
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to listen for |
listener | (âŚargs : any []) => void | The callback function to execute when the event occurs |
Returns
void
once
⸠once(event
, listener
): void
Register a one-time event listener
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to listen for |
listener | (âŚargs : any []) => void | The callback function to execute when the event occurs |
Returns
void