//node中的事件(evets)模块的用法和原理实现
//1、基本用法on、emit let EventEmitter=require("./events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter)//相当于Office.prototype.proto= EventEmitter.prototype; let office = new Office(); office.on("paper",user1); function user1(){ console.log("user1订阅了报纸") } office.on("paper",user2); function user2(){ console.log("user2订阅了报纸") } office.emit("paper"); //user1订阅了报纸 //user2订阅了报纸
//1实现的原理.on和emit的实现原理和发布订阅一样,用一个空的对象的key存储用户订阅的内容,value存储订阅此内容的用户{"paper":[user1,user2]}
function EventEmitter(){ this._events=Object.create(null);//{}中有原型链,上面创建的是没有原型链的对象 } EventEmitter.prototype.on=function(type,callback){//对象存储订阅的内容 if(!this._events){ this._events=Object.create(null);//原型指向某函数时,让这个函数中也有这个对象 } if(this._events[type]){ this._events[type].push(callback);//有paper说明之前有用户,有用户说明有数组,直接push }else{ this._events[type]=[callback];//第一个用户用数组存起来 } } EventEmitter.prototype.emit=function(type){//遍历对象中存储的方法 if(this._events[type]){//只有订阅这个事件才执行 this._events[type].forEach(listener =>{ listener.call(this); }) } } module.exports=EventEmitter;
//2、带参数的on、emit
let EventEmitter=require("events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息");
//user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息
//2.实现的原理
function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;
//3、removeListener删除
let EventEmitter=require("./events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); office.removeListener("paper",user1); office.emit("paper","这里是报社发出的消息2"); //user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息2
//3.removeListeren实现的原理:找到相应的数组项删除
function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){//有此事件才进入 this._events[type]=this._events[type].filter(function(listener){ return listener != callback;//只有不和需要移除的函数相等才返回 }) } } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;
//4、once只订阅一次
let EventEmitter=require("./events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.once("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); office.emit("paper","这里是报社发出的消息2");
//user1报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息 //user2报纸:这里是报社发出的消息2
//4.once实现的原理:实现一次后清空
function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){ if(!this._events){ this._events=Object.create(null); } if(this._events[type]){ this._events[type].push(callback); }else{ this._events[type]=[callback]; } } EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener.l !==callback;//删除时把有l的也删除 }) } } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){//确保这个在on之后执行 callback(...arguments)//argument是回调函数的参数 this.removeListener(type,wrap);//数组中移除掉 } wrap.l=callback//避免removeListener删除不了 this.on(type,wrap,flag);//先执行一次 } EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag);//on中的函数上添加参数 }) } } module.exports=EventEmitter;
//5.addListener用法同on
//5的原理
EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener
//6.newListener监听之后的事件
let EventEmitter=require("./events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("newListener",function(data){ console.log(data) }) office.on("paper1",user2); function user2(data){ console.log("user2报纸:"+data) } office.emit("paper","这里是报社发出的消息"); //paper1 //user1报纸:这里是报社发出的消息
//6.newListener实现的原理:排除在newlistener之前和自己本身的事件,将事件作为参数传给回调函数
function EventEmitter(){ this._events=Object.create(null); } EventEmitter.prototype.on=function(type,callback){
if(!this._events){ this._events=Object.create(null);}if(type !=="newListener"){//排除type为newListener的情况 this._events["newListener"] //排除type在newListener之前的情况 && this._events["newListener"].forEach(listener =>{ listener(type)//将事件作为参数传出去 })}if(this._events[type]){ this._events[type].push(callback);}else{ this._events[type]=[callback];}复制代码
} EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener !==callback; }) } } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){ callback(...arguments) this.removeListener(type,wrap); } wrap.l=callback; this.on(type,wrap,flag); } EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag); }) } } module.exports=EventEmitter;
//7.defaultMaxListeners默认事件的最大值
let EventEmitter=require("./events"); console.log(EventEmitter.defaultMaxListeners)
//10
//8.listener监听事件
let EventEmitter=require("./events"); function Office(){
}; let util=require("util"); util.inherits(Office,EventEmitter) let office = new Office(); office.on("paper",user1); function user1(data){ console.log("user1报纸:"+data) } office.on("paper",user2); function user2(data){ console.log("user2报纸:"+data) } console.log(office.listeners('paper')) //[ [Function: user1], [Function: user2] ]
//8.listener实现的原理里:对象中找到指定的事件,将事件下的数组返回
function EventEmitter(){ this._events=Object.create(null); } EventEmitter.defaultMaxListeners="10" EventEmitter.prototype.on=function(type,callback){
if(!this._events){ this._events=Object.create(null);}if(type !=="newListener"){ this._events["newListener"] && this._events["newListener"].forEach(listener =>{ listener(type) })}if(this._events[type]){ this._events[type].push(callback);}else{ this._events[type]=[callback];}复制代码
} EventEmitter.prototype.removeListener=function(type,callback){ if(this._events[type]){ this._events[type]=this._events[type].filter(function(listener){ return listener != callback && listener !==callback; }) } } EventEmitter.prototype.listeners=function(type){ return this._events[type];//返回这个事件下的数组 } EventEmitter.prototype.once=function(type,callback,flag){ function wrap(){ callback(...arguments) this.removeListener(type,wrap); } wrap.l=callback; this.on(type,wrap,flag); } EventEmitter.prototype.addListener=EventEmitter.prototype.on;//将on的原型指向addlistener EventEmitter.prototype.emit=function(type,flag){ if(this._events[type]){ this._events[type].forEach(listener =>{ listener.call(this,flag); }) } } module.exports=EventEmitter;