First commit

This commit is contained in:
Jonathan Staab
2023-03-25 10:08:17 -05:00
commit 39d001280c
12 changed files with 4962 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
export type EventBusListener = {
id: string
handler: (...args: any[]) => void
}
export class EventBus {
listeners: Record<string, Array<EventBusListener>> = {}
on(name, handler) {
const id = Math.random().toString().slice(2)
this.listeners[name] = this.listeners[name] || ([] as Array<EventBusListener>)
this.listeners[name].push({id, handler})
return id
}
off(name, id) {
this.listeners[name] = this.listeners[name].filter(l => l.id !== id)
}
handle(k, ...payload) {
for (const {handler} of this.listeners[k] || []) {
handler(...payload)
}
}
}
+1
View File
@@ -0,0 +1 @@
export * from './EventBus'