Skip to content

Commit

Permalink
fix(paska-ovo): Remove and add event listener when a key listener alr…
Browse files Browse the repository at this point in the history
…eady exists
  • Loading branch information
EGAMAGZ committed Apr 20, 2024
1 parent 336a2fb commit 3599c36
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/paska-ovo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class PaskaOvo {

private keysPressed: string[] = [];

private keyListener?: ((event: KeyboardEvent) => void);

/**
* Constructs a new instance of the class with optional parameters for an easter egg.
Expand Down Expand Up @@ -71,6 +72,18 @@ export class PaskaOvo {
});
}

/**
* Creates a function that handles the key event.
*
* @param {EasterEgg[]} easterEggs - List of easter eggs to trigger.
* @return {(event: KeyboardEvent) => void} Function that handles the key event.
*/
private createHandleKeyEvent(easterEggs: EasterEgg[]): (event: KeyboardEvent) => void {
return (event: KeyboardEvent) => {
this.handleKeyEvent(event, easterEggs);
}
}

/**
* Resets the keysPressed array to an empty array.
*/
Expand All @@ -90,17 +103,25 @@ export class PaskaOvo {
return this;
}

private stop() {
// document.addEventListener()
/**
* Adds an event listener to the document for keyup events if it is not already added.
*/
public listen() {
if (this.keyListener !== undefined) {
this.stop();
}
this.keyListener = this.createHandleKeyEvent(this.easterEggs);

document.addEventListener("keyup", this.keyListener);
}

/**
* Adds an event listener to the document for keyup events.
*/
public listen() {
document.addEventListener("keyup", (event) => {
this.handleKeyEvent(event, this.easterEggs)
}, false);
* Removes the event listener from the document.
* */
public stop() {
if (this.keyListener) {
document.removeEventListener("keyup", this.keyListener);
}
}
}

0 comments on commit 3599c36

Please sign in to comment.