当前位置: 首页 > news >正文

沈阳做网站的地方阿里企业邮箱登陆入口

沈阳做网站的地方,阿里企业邮箱登陆入口,做淘宝网店需要多少钱,世界工厂网登录关于EventBus事件总线 事件总线,实现 on, once, emit, off on, once 是注册函数,存储起来emit时找到对应的函数,执行off找到对应的函数,从对象中删除 注意 区分on和onceon绑定的事件可连续执行,除非offonce绑定的函数…

关于EventBus事件总线

  • 事件总线,实现 on, once, emit, off
    • on, once 是注册函数,存储起来
    • emit时找到对应的函数,执行
    • off找到对应的函数,从对象中删除
  • 注意
    • 区分on和once
    • on绑定的事件可连续执行,除非off
    • once绑定的函数 emit 一次即删除,也可未执行而被 off
    • 数据结构上标识出 on 和 once

实现方案1

代码实现:使用参数区分 on和once

class EventBus {/*{key1: [{fn: fn1, isOnce: false},{fn: fn2, isOnce: false},{fn: fn3, isOnce: true},],key2: []}*/private events: {[key: string]: Array<{fn: Function; isOnce: boolean}>}constructor() {this.events = {};}on(type: string, fn: Function, isOnce: boolean = false) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce});},// 这里是初步版本once_origin(type: string, fn: Function) {const events = this.events;if (events[type] == null) {events[type] = []; // 初始化 key 的 fn 数组}events[type].push({fn, isOnce: true});},once(type: string, fn: Function, isOnce: boolean = false) {this.on(type, fn, true);},off(type: string, fn?:Function) {if (!fn) {// 解绑所有 type 的函数this.events[type] = [];} else {// 解绑单个 fnconst fnList = this.events[type];if (fnList.length) {this.events[type] = fnList.filter(item.fn !== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];if (fnList == null) return;// 注意this.events[type] = fnList.filter(item => {const { fn, isOnce } = item;fn(...args);// 处理once, 它执行一次就要被过滤掉if (!isOnce) return true;return false;})}
}const e = new EventBus();
function fn1(a: any, b: any) {console.log('fn1', a, b)};
function fn2(a: any, b: any) {console.log('fn2', a, b)};
function fn3(a: any, b: any) {console.log('fn3', a, b)};e.on('key1', fn1);
e.on('key1', fn2);
e.once('key1', fn3);e.emit('key1', 10, 20); // 触发 fn1, fn2, fn3
e.off('key1', fn1);
e.emit('key1', 100, 200); // 触发 fn2

实现方案2

代码实现: 拆分保存 on和once

class EventBus {private events: { [key: string]: Array<Function>} // {key1: [fn1, fn2]}private onceEvents: {[key: string]: Array<Function>} // 结构同上constructor() {this.events = {}; // 存储 onthis.onceEvents = {}; // 存储 once}// on 触发on(type: string, fn: Function) {const events = this.events;if (events[type] === null) events[type] = [];events[type].push(fn);},once(type: string, fn: Function) {const onceEvents = this.onceEvents;if (onceEvents[type] === null) onceEvents[type] = [];onceEvents[type].push(fn);},// 解绑事件off(type: string, fn: Function) {if (!fn) {// 解绑所有事件this.events[type] = [];this.onceEvents[type] = [];} else {// 解绑单个const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {this.events[type] = fnList.filter(curFn => curFN!== fn);}if (onceFnList.length) {this.onceEvents[type] = onceFnList.filter(curFn => curFN!== fn);}}},emit(type: string, ...args: any[]) {const fnList = this.events[type];const onceFnList = this.onceEvents[type];if (fnList.length) {fnList.forEach(f => f(...args));}if (onceFnList.length) {onceFnList.forEach(f => f(...args));// once 执行一次就删除,这里更简单,代码更简洁this.onceEvents[type] = [];}}
}// 测试用例同上,此处省略

总结

  • 区分 on, once
  • 合理的数据结构,比算法优化更有效
http://www.yayakq.cn/news/700551/

相关文章:

  • 手机网站建设用乐云seojsp网站建设 书籍
  • 阿里云云主机做网站wordpress联系浮动
  • 地产网站建设公司wordpress架设进出销
  • 要加强分院网站建设遵义市住房城乡建设局网站
  • 网站源码建设模板市场营销网站建设
  • 微信网站开发与网站实质区别自动优化网站建设咨询
  • 网站建设和软件开发网站建设流程有几个阶段
  • 唐山高端品牌网站建设做citation的网站
  • 一个阿里云怎么做两个网站wordpress单击图片
  • 网站seo属于什么专业蓝气球卡地亚手表官方网站
  • 南京网站推广公司网站建设维护的知识
  • 惠州网站建设找哪个公司跨境电商哪个平台最好
  • 绵阳商城网站建设网站建设属于哪个专业
  • 数据开放网站建设在哪里做百度网站
  • 南通单位网站建设wordpress 调取分类
  • wordpress视频网站用什么播放器京东商城网站开发平台
  • 网校网站建设方案免费建网页
  • 免费网站100m新能源汽车价格表2021
  • 德国室内设计简述seo和sem的区别与联系
  • 网站 logfiles手机网站横竖屏
  • 做网站开发教程wordpress七牛图床插件
  • 网站地址申请中企动力成都分公司网站建设案例
  • 丰台体育馆网站建设做网站每年交服务费
  • 建立个人网站怎么赚钱网页设计与网站建设课设
  • 济南做网站建设公司网络平台推广宣传方案
  • 马蜂窝网站建设目的南山做网站方案
  • 企业网站模板哪个好做企业网站织梦和wordpress哪个好
  • 内蒙古有做购物网站的吗网站收缩目录模板
  • 网站整合discuz论坛dw网页制作模板下载
  • 公司怎么申请免费做网站查看邮箱注册的网站