jan/web/app/_services/eventsService.ts
2023-10-20 11:29:10 +07:00

41 lines
885 B
TypeScript

export class EventEmitter {
private handlers: Map<string, Function[]>
constructor() {
this.handlers = new Map<string, Function[]>()
}
public on(eventName: string, handler: Function): void {
if (!this.handlers.has(eventName)) {
this.handlers.set(eventName, [])
}
this.handlers.get(eventName)?.push(handler)
}
public off(eventName: string, handler: Function): void {
if (!this.handlers.has(eventName)) {
return
}
const handlers = this.handlers.get(eventName)
const index = handlers?.indexOf(handler)
if (index !== undefined && index !== -1) {
handlers?.splice(index, 1)
}
}
public emit(eventName: string, args: any): void {
if (!this.handlers.has(eventName)) {
return
}
const handlers = this.handlers.get(eventName)
handlers?.forEach((handler) => {
handler(args)
})
}
}