首页 > 其他分享 >bus.ts

bus.ts

时间:2022-08-30 23:46:53浏览次数:57  
标签:string bus list ts Array type fn name

type BusClass = {
  emit: (name: string) => void
  on: (name: string, callback: Function) => void
}
type PramsKey = string | number | symbol
// 调度中心
type List = {
  [key: PramsKey]: Array<Function>
}
class Bus implements BusClass {
  list: List
  constructor() {
    this.list = {}
  }
  emit(name: string, ...args: Array<any>) {
    let eventName: Array<Function> = this.list[name]
    eventName.forEach(fn => {
      fn.apply(this, args)
    })

  }
  on(name: string, callback: Function) {
    let fn: Array<Function> = this.list[name] || []
    fn.push(callback)
    this.list[name] = fn
  }
}

export default new Bus()

标签:string,bus,list,ts,Array,type,fn,name
From: https://www.cnblogs.com/jaycethanks/p/16641374.html

相关文章