UNPKG

shoehive

Version:

WebSocket-based multiplayer game framework for real-time, event-driven gameplay

84 lines (83 loc) 2.96 kB
import { EventType } from "./EventTypes"; /** * 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: * * ```typescript * // 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}`); * }); * ``` */ export declare class EventBus { private emitter; private debugEnabled; private debugFilter?; private debugLogger; private originalEmit; constructor(); /** * Register an event listener * @param event The event to listen for * @param listener The callback function to execute when the event occurs */ on(event: EventType | string, listener: (...args: any[]) => void): void; /** * Register a one-time event listener * @param event The event to listen for * @param listener The callback function to execute when the event occurs */ once(event: EventType | string, listener: (...args: any[]) => void): void; /** * Remove an event listener * @param event The event to stop listening for * @param listener The callback function to remove */ off(event: EventType | string, listener: (...args: any[]) => void): void; /** * Emit an event * @param event The event to emit * @param args Arguments to pass to event listeners * @returns Whether the event had listeners */ emit(event: EventType | string, ...args: any[]): boolean; /** * Get the number of listeners for an event * @param event The event to check * @returns The number of listeners for the event */ listenerCount(event: EventType | string): number; /** * Debug monitor for all events * Logs all events and their payloads to the console * Useful during development or debugging * * @param enabled Whether to enable debug monitoring * @param filter Optional filter function to only log certain events * @param logger Custom logger function (defaults to console.log) */ debugMonitor(enabled?: boolean, filter?: (event: string) => boolean, logger?: (event: string, ...args: any[]) => void): void; }