import mitt from '@/utils/mitt'
import { onBeforeUnmount } from 'vue'

declare interface Fn<T = any> {
  (...arg: T[]): T
}
interface Option {
  name: string // 事件名称
  callback: Fn // 回调
}

const emitter = mitt()

export const useEmitt = (option?: Option) => {
  if (option) {
    emitter.on(option.name, option.callback)

    onBeforeUnmount(() => {
      emitter.off(option.name, option.callback)
    })
  }

  return {
    emitter
  }
}
