Skip to content

Commit

Permalink
Refactor: Add JSDoc comments to EventEmitter class and Event type
Browse files Browse the repository at this point in the history
  • Loading branch information
EvandroLG committed Dec 26, 2024
1 parent ae433d2 commit bdf7a4f
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
/**
* Represents an event with associated data.
*/
export type Event = {
/**
* The data associated with the event. The type of data is unknown.
*/
data: unknown;
};

/**
* Event emitter class that allows registering event listeners and emitting events.
*/
export class EventEmitter {
/**
* A map of event keys to their respective callback functions.
* @private
*/
private events: { [key: string]: (param: Event) => void };

/**
* Initializes a new instance of the EventEmitter class.
*/
constructor() {
this.events = {};
}

public listener(keyEvent: string, callback: (param: Event) => void) {
/**
* Registers a listener for a specific event key.
*
* @param {string} keyEvent - The key of the event to listen for.
* @param {(param: Event) => void} callback - The callback function to be invoked when the event is emitted.
*/
public listener(keyEvent: string, callback: (param: Event) => void): void {
this.events[keyEvent] = callback;
}

public emit(keyEvent: string, param: Event) {
/**
* Emits an event, invoking the corresponding listener with the provided parameter.
*
* @param {string} keyEvent - The key of the event to emit.
* @param {Event} param - The parameter to pass to the event's callback function.
*/
public emit(keyEvent: string, param: Event): void {
if (this.events[keyEvent]) {
this.events[keyEvent](param);
}
Expand Down

0 comments on commit bdf7a4f

Please sign in to comment.