mirror of
https://github.com/koel/koel
synced 2024-12-20 09:33:23 +00:00
34 lines
802 B
TypeScript
34 lines
802 B
TypeScript
import { EventName } from '@/config'
|
|
|
|
export const eventBus = {
|
|
all: new Map(),
|
|
|
|
on (name: EventName | EventName[] | Partial<{ [K in EventName]: TAnyFunction }>, callback?: TAnyFunction) {
|
|
if (Array.isArray(name)) {
|
|
name.forEach(k => this.on(k, callback))
|
|
return
|
|
}
|
|
|
|
if (typeof name === 'object') {
|
|
for (const k in name) {
|
|
this.on(k as EventName, name[k as EventName])
|
|
}
|
|
return
|
|
}
|
|
|
|
if (this.all.has(name)) {
|
|
this.all.get(name).push(callback)
|
|
return
|
|
}
|
|
|
|
this.all.set(name, [callback])
|
|
},
|
|
|
|
emit (name: EventName, ...args: any) {
|
|
if (this.all.has(name)) {
|
|
this.all.get(name).forEach((cb: TAnyFunction) => cb(...args))
|
|
} else {
|
|
console.warn(`Event ${name} is not listened by any component`)
|
|
}
|
|
}
|
|
}
|