Added Jest and tests for EventBus

This commit is contained in:
Simon Cambier
2022-04-23 17:03:13 +02:00
parent 9bd0b76473
commit 3bcaca708d
9 changed files with 3722 additions and 7 deletions

View File

@@ -6,13 +6,22 @@ export class EventBus {
public on(ctx: string, event: string, callback: EventBusCallback): void {
if (ctx.includes('@') || event.includes('@')) {
throw new Error('Invalid ctx/event name - Cannot contain @')
throw new Error('Invalid context/event name - Cannot contain @')
}
this.handlers.set(`${ctx}@${event}`, callback)
}
public off(ctx: string, event: string): void {
this.handlers.delete(`${ctx}@${event}`)
public off(ctx: string, event?: string): void {
if (event) {
this.handlers.delete(`${ctx}@${event}`)
}
else {
for (const [key] of this.handlers.entries()) {
if (key.startsWith(`${ctx}@`)) {
this.handlers.delete(key)
}
}
}
}
public disable(ctx: string): void {