Adds ICS
This commit is contained in:
parent
441715675c
commit
a79898b2e9
48 changed files with 2062 additions and 1503 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import cron from "node-cron";
|
||||
import {Class} from "../types/Class";
|
||||
import {Class} from "../types/Class";
|
||||
|
||||
export type EventConfiguration = {
|
||||
name: string,
|
||||
name: string,
|
||||
maxExecutions?: number,
|
||||
}
|
||||
|
||||
|
|
@ -14,34 +14,34 @@ export interface TimedEvent {
|
|||
|
||||
export class EventHandler {
|
||||
private eventHandlers: Map<string, CallableFunction[]> = new Map();
|
||||
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
|
||||
public addHandler<T extends Class>(eventName: string, handler: (event: T) => void) {
|
||||
if (!this.eventHandlers.has(eventName)) {
|
||||
this.eventHandlers.set(eventName, []);
|
||||
}
|
||||
|
||||
|
||||
this.eventHandlers.get(eventName)?.push(handler);
|
||||
}
|
||||
|
||||
|
||||
public dispatch<T extends Class>(event: T) {
|
||||
const eventName = event.constructor.name;
|
||||
if (!this.eventHandlers.has(eventName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
this.eventHandlers.get(eventName)?.forEach((handler) => {
|
||||
handler(event);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
public addTimed(event: TimedEvent) {
|
||||
if (!cron.validate(event.cronExpression)) {
|
||||
throw new Error(`Can't create event with name '${event.configuration.name}': Invalid cron expression.`)
|
||||
}
|
||||
|
||||
|
||||
cron.schedule(event.cronExpression, event.execute.bind(event), event.configuration);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue