interface Callback {
(...data: any): void
}
const subscribers = new Map()
export const subscribe = (event: string, callback: Callback): void => {
if (subscribers.has(event)) {
subscribers.get(event).set(callback, callback)
} else {
subscribers.set(event, new Map().set(callback, callback))
}
}
export const publish = (event: string, ...data: any): void => {
subscribers.get(event)?.forEach((cb: Callback) => cb(...data))
}
export const unsubscribe = (event: string, callback: Callback): void => {
subscribers.get(event)?.delete(callback)
}
export const unsubscribeEvent = (event: string): void => {
subscribers.delete(event)
}
export const unsubscribeAll = (): void => {
subscribers.clear()
}