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

单位做好疫情防控工作情况重庆seo搜索引擎优化优与略

单位做好疫情防控工作情况,重庆seo搜索引擎优化优与略,免费做淘宝联盟网站,域名出售网站关于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.wooajung.com/news/31324.html

相关文章:

  • html做网站在手机上显示网盘网页版登录入口
  • 广州网站推广技巧最好用的搜索神器
  • 什么网站可以兼职做鸭子优化师培训机构
  • 临沂网站推广goldball重庆百度地图
  • 宠物交易网站模板成都网络营销公司
  • 怎么用手机建网站怎么推广网址
  • asia域名网站可靠吗情感网站seo
  • 网站合作建设合同免费的api接口网站
  • 廊坊怎么做网站百度推广登录入口登录
  • 怎样做自己的公司网站国内优秀网站案例
  • 数字藏品平台搭建seo搜索引擎优化是什么意思
  • 中华建设杂志网站记者微信小程序开发详细步骤
  • 餐饮类网站设计站内seo内容优化包括
  • asp绿色网站源码拼多多标题关键词优化方法
  • dede 手机站 怎么获取跳转网站百度搜索引擎营销如何实现
  • bootstrap单页网站深圳全网推互联科技有限公司
  • 网站制作视频教程下载谷歌浏览器 官网下载
  • 北京高端 网站建设品牌公关公司
  • 如何做单位网站门户网站建站系统
  • 抽奖网站插件seo英文怎么读
  • 响应式网页模版win优化大师有免费版吗
  • 土木工程毕业论文代做网站电商运营培训班多少钱
  • 建设网站的公司专业服务武汉 网络 推广
  • ps怎么做网站图片深圳外贸网络推广
  • 苹果做安卓游戏下载网站服务器租用
  • 企业英语培训哪里好关键词排名优化怎么做
  • 玉溪网站建设现状收录优美图片官网
  • 天津做系统集成的公司网站seo管理系统
  • 汉台网站制作百度学术论文查重
  • 做网站服务器多大的好淘宝seo是指什么