Improve consistency of interface

This commit is contained in:
Jonathan Staab
2023-03-27 15:05:34 -05:00
parent 853b42c1c9
commit 9b6a779397
9 changed files with 223 additions and 165 deletions
+7 -15
View File
@@ -1,32 +1,24 @@
export type EventBusHandler = (...args: any[]) => void
export type EventBusListener = {
id: string
handler: EventBusHandler
}
export class EventBus {
static ANY = Math.random().toString().slice(2)
listeners: Record<string, Array<EventBusListener>> = {}
listeners: Record<string, Array<EventBusHandler>> = {}
on(name: string, handler: EventBusHandler) {
const id = Math.random().toString().slice(2)
this.listeners[name] = this.listeners[name] || ([] as Array<EventBusListener>)
this.listeners[name].push({id, handler})
return id
this.listeners[name] = this.listeners[name] || ([] as Array<EventBusHandler>)
this.listeners[name].push(handler)
}
off(name: string, id: string) {
this.listeners[name] = this.listeners[name].filter(l => l.id !== id)
off(name: string, handler: EventBusHandler) {
this.listeners[name] = this.listeners[name].filter(h => h !== handler)
}
clear() {
this.listeners = {}
}
handle(k: string, ...payload: any) {
for (const {handler} of this.listeners[k] || []) {
for (const handler of this.listeners[k] || []) {
handler(...payload)
}
for (const {handler} of this.listeners[EventBus.ANY] || []) {
for (const handler of this.listeners[EventBus.ANY] || []) {
handler(k, ...payload)
}
}