当前位置: 首页 > 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/646510/

相关文章:

  • 网站被降权表现在线教育平台搭建
  • seo资料站wordpress悬浮小工具的插件
  • 广东网站制作公司酒店手机网站模板
  • 楚雄企业网站建设公司直播营销
  • 网页网站公司如何做备份2018年网站开发技术
  • 加盟做网站公司名称起名大全
  • 西宁网站建设有限公司山东网站建设设计
  • 杭州集团网站建设方案百度关键词搜索次数
  • 网站分析与优化的文章餐饮网站建设
  • 涡阳网站建设哪家好查询工具类网站制作
  • 织梦网站怎么做301跳转广州网站建设网页设计
  • 南充市住房和城乡建设厅官方网站网站建设 百度云盘
  • 商城网站的psd模板免费下载好网站制作
  • 大型营销型网站制作网站服务器租用多少钱
  • app哪个网站开发好好用的wordpress模板
  • 哈尔滨网站制作工程用什么工具建设网站
  • 余杭区网站建设微网站开发系统
  • 网站关键词排名优化工具四川新站优化
  • ps网站设计概述炉石卡牌制作网页
  • 深圳网站建设定制平台wordpress portfolio
  • 网站制作ppt模板wordpress 表格 链接地址
  • 更合网站开发门户网站建设如何入账
  • 电商网站开发教材营销型网站名词解释
  • 编写网站方案设计书表格什么建设网站
  • 中企动力近期做的网站广告公司广告设计
  • 阿里云空间部署网站吗创建网站平台要多少钱
  • 雷州网站建设公司株洲头条新闻
  • 青岛seo整站优化公司企业网站建设服务好
  • 我国档案网站建设研究论文wordpress博客 文章格式
  • 做网站网站湖南企业seo优化报价