diff --git a/src/models/EventBus.ts b/src/models/EventBus.ts index c45b5cc..9e105ee 100644 --- a/src/models/EventBus.ts +++ b/src/models/EventBus.ts @@ -1,12 +1,24 @@ /* eslint-disable @typescript-eslint/ban-types */ + import { IEventBus, eventType } from "./interfaces/IEventBus"; +/** + * Implementation of the IEventBus interface. + * Singleton class to manage and handle event subscriptions and publications. + */ export class EventBus implements IEventBus { private static instance: EventBus; private listeners: { [event: string]: Function[] } = {}; - private constructor() { } // private constructor to prevent direct construction calls with the 'new' operator. + /** + * Private constructor to prevent direct construction calls with the 'new' operator. + */ + private constructor() { } + /** + * Get the instance of EventBus. + * @returns {EventBus} The singleton instance of EventBus. + */ public static getInstance(): EventBus { if (!EventBus.instance) { EventBus.instance = new EventBus(); @@ -15,6 +27,11 @@ export class EventBus implements IEventBus { return EventBus.instance; } + /** + * Subscribe to an event. + * @param event - Type of event to listen for. + * @param listener - Callback function to be executed when the event is fired. + */ subscribe(event: eventType, listener: Function): void { if (!this.listeners[event]) { this.listeners[event] = []; @@ -22,11 +39,19 @@ export class EventBus implements IEventBus { this.listeners[event].push(listener); } + /** + * Publish (trigger) an event. + * @param event - Type of event to be triggered. + * @param args - Arguments to be passed to the subscribed listeners. + */ // eslint-disable-next-line @typescript-eslint/no-explicit-any publish(event: eventType, ...args: any[]): void { (this.listeners[event] || []).forEach(listener => listener(...args)); } + /** + * Clear all event listeners. + */ clear(): void { this.listeners = {}; } diff --git a/src/models/interfaces/IEventBus.ts b/src/models/interfaces/IEventBus.ts index e96d914..821bb39 100644 --- a/src/models/interfaces/IEventBus.ts +++ b/src/models/interfaces/IEventBus.ts @@ -1,3 +1,6 @@ +/** + * Represents various events that can occur during a game. + */ export type eventType = | "gameStarted" | "drawInitialCards" @@ -25,10 +28,28 @@ export type eventType = | "invalidPlay" // Details about the invalid play ; +/** + * Interface for the event bus system. + */ export interface IEventBus { + /** + * Subscribe to an event. + * @param event - Type of event to listen for. + * @param listener - Callback function to be executed when the event is fired. + */ // eslint-disable-next-line @typescript-eslint/ban-types subscribe(event: eventType, listener: Function): void; + + /** + * Publish (trigger) an event. + * @param event - Type of event to be triggered. + * @param args - Arguments to be passed to the subscribed listeners. + */ // eslint-disable-next-line @typescript-eslint/no-explicit-any publish(event: eventType, ...args: any[]): void; + + /** + * Clear all event listeners. + */ clear(): void; }