refactor(menu): Made sure the menu can be used for more than group
This commit is contained in:
parent
a79898b2e9
commit
1d73ee8a78
16 changed files with 650 additions and 406 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import cron from "node-cron";
|
||||
import cron, {validate} from "node-cron";
|
||||
import {Class} from "../types/Class";
|
||||
import {randomUUID} from "node:crypto";
|
||||
|
||||
export type EventConfiguration = {
|
||||
name: string,
|
||||
|
|
@ -13,17 +14,32 @@ export interface TimedEvent {
|
|||
}
|
||||
|
||||
export class EventHandler {
|
||||
private eventHandlers: Map<string, CallableFunction[]> = new Map();
|
||||
private eventHandlers: Map<string, Map<string, CallableFunction>> = new Map();
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
public addHandler<T extends Class>(eventName: string, handler: (event: T) => void) {
|
||||
public addHandler<T extends Class>(eventName: string, handler: (event: T) => void): string {
|
||||
if (!this.eventHandlers.has(eventName)) {
|
||||
this.eventHandlers.set(eventName, []);
|
||||
this.eventHandlers.set(eventName, new Map());
|
||||
}
|
||||
|
||||
this.eventHandlers.get(eventName)?.push(handler);
|
||||
const id = randomUUID();
|
||||
this.eventHandlers.get(eventName)?.set(id, handler);
|
||||
return id;
|
||||
}
|
||||
|
||||
public removeHandler(eventName: string, id: string) {
|
||||
if (!this.eventHandlers.has(eventName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const localEventHandlers = this.eventHandlers.get(eventName);
|
||||
if (!localEventHandlers || !localEventHandlers.has(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
localEventHandlers.delete(id);
|
||||
}
|
||||
|
||||
public dispatch<T extends Class>(event: T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue