Skip to content

Commit

Permalink
docs: add tsdoc to EventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
AtilioA committed Oct 27, 2023
1 parent 1238681 commit 70b8cbf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/models/EventBus.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -15,18 +27,31 @@ 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] = [];
}
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 = {};
}
Expand Down
21 changes: 21 additions & 0 deletions src/models/interfaces/IEventBus.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Represents various events that can occur during a game.
*/
export type eventType =
| "gameStarted"
| "drawInitialCards"
Expand Down Expand Up @@ -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;
}

0 comments on commit 70b8cbf

Please sign in to comment.