ultra-minimal pubsub module written in TypeScript
interface Callback {
  (...data: any): void
}

const subscribers = new Map<string, Set<Callback>>()

export const subscribe = (event: string, callback: Callback): void => {
  if (subscribers.has(event)) {
    subscribers.get(event)?.add(callback)
  } else {
    subscribers.set(event, new Set<Callback>().add(callback))
  }
}

export const publish = (event: string, ...data: any): void => {
  if (subscribers.has(event)) {
    Promise.all([...subscribers.get(event)!].map((cb: Callback) => cb(...data)))
  }
}

export const unsubscribe = (event: string, callback?: Callback): void => {
  if (callback) {
    subscribers.get(event)?.delete(callback)
  } else if (event === "*") {
    subscribers.clear()
  } else {
    subscribers.delete(event)
  }
}