interface PubSubType {
events: { [key: string]: { name: string, once: boolean, cb: Function }[] }
on(name: string, cb: Function): void
once(name: string, cb: Function): void
emit(name: string, ...arg: Array<any>): void
off(name: string, cb: Function): void
}
class PubSub implements PubSubType {
events: { [key: string]: { name: string, once: boolean, cb: Function }[] }
constructor() {
this.events = {}
}
private eventsOnApi(this: PubSubType, name: string, cb: Function, once: boolean = false) {
const list = this.events[name] || [];
list.push({ name, cb, once })
this.events[name] = list
}
on(name: string, cb: Function): void {
this.eventsOnApi(name, cb)
}
once(name: string, cb: Function): void {
this.eventsOnApi(name, cb, true)
}
emit(name: string, ...arg: Array<any>): void {
const list = this.events[name];
list && list.forEach(item => {
if (item.once) {
item.cb.apply(this, arg)
this.off(name, item.cb)
} else {
item.cb.apply(this, arg)
}
})
}
off(name: string, cb: Function): void {
const list = this.events[name];
if (!list) return
const fileList = list.filter(item => item.cb !== cb)
this.events[name] = fileList
}
}

最新文章

  1. node crypto md5加密,并解决中文不相同的问题
  2. 在python中处理XML
  3. “SSLError: The read operation timed out” when using pip
  4. FileInputStream类
  5. mysql更新一个表里的字段等于另一个表某字段的值
  6. NOIP总结
  7. 【React Native 实战】旋转图片验证码
  8. About the Storage allocation
  9. AnonymousType匿名类型和对象之间的转换
  10. .Net 多线程开发优化实践
  11. Zabbix实战-简易教程系列
  12. wamp apache无法启动的解决方法
  13. View事件分发
  14. Codeforces.1045A.Last chance(最大流ISAP 线段树优化建图)
  15. 查看占用IO的进程
  16. how-to-view-source-of-chrome-extension
  17. Azure Storage架构介绍
  18. webapi Route 特性
  19. 清理tomcat日志大的文件
  20. kong 插件开发分析

热门文章

  1. Elasticsearch:top_hits aggregation
  2. Node Exporter监控指标
  3. SQLyog企业版
  4. 《HelloGitHub》第 78 期
  5. mac通过docker一键部署MySQL8
  6. 2021东华杯misc project
  7. 消除两个inline-block元素之间的间隔
  8. golang中的init初始化函数
  9. Java多线程-ThreadPool线程池-2(四)
  10. Codeforces Round #809 (Div. 2)C.Qpwoeirut And The City