03.node.js事件类EventEmitter

阅读更多
  1. EventEmitter类
    EventEmitter的核心就是事件触发和事件监听功能的封装.
    支持事件响应的核心模块都是EventEmitter的子类,一般使用子类就可以了.
    EventEmitter的简单例子EventEmitterDemo.js如
    var events = require('events');//引入模块
    //events模块只提供了一个对象events.EventEmitter
    var eventEmitter = new events.EventEmitter();//创建eventEmitter对象
    
    eventEmitter.on('event_name',function(arg1,arg2){//回调参数接收事件参数
        console.log('event_name 1 with two arguments',arg1,arg2);
    });
    
    //为相同事件注册第二个事件监听器
    eventEmitter.on('event_name',function(arg1,arg2,arg3){
        console.log('event_name 2 with three arguments',arg1,arg2,arg3);
    });
    
    //为相同事件注册第三个事件监听器
    eventEmitter.on('event_name',function(){
        console.log('event_name 3 without  argument');
    });
    
    eventEmitter.emit('event_name');//触发事件
    console.log('----------');
    
    setTimeout(function(){
    	eventEmitter.emit('event_name','参数1','参数2');//触发事件,包含事件参数
    
    },1000);
    
    
    
      运行结果
    >node EventEmitterDemo.js
    event_name 1 with two arguments undefined undefined
    event_name 2 with three arguments undefined undefined undefined
    event_name 3 without  argument
    ----------
    event_name 1 with two arguments 参数1 参数2
    event_name 2 with three arguments 参数1 参数2 undefined
    event_name 3 without  argument
     

  2. EventEmitter类方法,EventEmitter实例方法与事件
    EventEmitter.js内容如
    var events = require('events');
    
    var eventEmitter = new events.EventEmitter();
    /*--------------事件---------------*/
    //EventEmitter内置事件,程序异常触发error事件,没有监听器Node.js输出错误并退出
    //建议为会触发error事件的对象添加监听器,避免整个程序奔溃
    eventEmitter.on('error',function(){
    	console.log('error catched');
    });
    eventEmitter.emit('error');
    //EventEmitter内置事件,eventEmitter实例移除监听器时removeListener事件会被触发
    eventEmitter.on('removeListener',function(event,fn){
    	console.log('remove a Listener',event,fn);
    });
    //EventEmitter内置事件,eventEmitter实例添加新的监听器时newListener事件会被触发
    eventEmitter.on('newListener',function(event,fn){
    	console.log('add new Listener',event,fn);
    });
    
    
    /*--------------类方法----------------*/
    //返回eventEmitter实例指定事件的监听器个数
    var event_name_count = events.EventEmitter.listenerCount(eventEmitter,'event_name');
    var remove_listener_count = events.EventEmitter.listenerCount(eventEmitter,'removeListener');
    console.log('event_name_count:' + event_name_count, 'remove_listener_count:' + remove_listener_count);
    
    /*--------------方法----------------*/
    //单个事件的监听器数量默认为10,超出会有警告信息,可以提高监听器默认限制的数量
    eventEmitter.setMaxListeners(3);
    
    //注册一个监听器
    eventEmitter.on('event_name',function(arg1){
    	console.log('this is a listener added by on with argument',arg1);
    });
    //注册监听器到监听器数组的尾部
    eventEmitter.addListener('event_name',function(arg1,arg2){
    	console.log('this is a listener added by addListener with arguments',arg1,arg2);
    });
    //注册一个单次监听器,被触发后先触发removeListener事件后再执行本监听器,最后移除本监听器
    eventEmitter.once('event_name',function(){
        console.log('this is a listener added by once');
    });
    
    
    var eventHandler = function(){
    	console.log('this is a listener with a name eventHandler');
    };
    eventEmitter.on('event_name',eventHandler);
    
    console.log('------------------event_name事件的监听器数组-------------');
    //返回指定事件的监听器数组
    var eventListenersArray = eventEmitter.listeners('event_name');
    console.log(eventListenersArray);
    
    console.log('-------------------触发event_name事件---------------------');
    //触发监听事件,有监听器返回true,否则返回false
    var hasEventHandler = eventEmitter.emit('event_name','参数1','参数2');
    console.log('hasEventHandler:' + hasEventHandler);
    
    setTimeout(function(){
    	eventEmitter.emit('event_name');
    },1000);
    
    console.log('---------------------------------------------');
    var event_name_count = events.EventEmitter.listenerCount(eventEmitter,'event_name');
    var remove_listener_count = events.EventEmitter.listenerCount(eventEmitter,'removeListener');
    console.log('event_name_count:' + event_name_count, 'remove_listener_count:' + remove_listener_count);
    
    console.log('-----------------移除eventHandler监听器----------------------');
    //移除指定监听器
    eventEmitter.removeListener('event_name',eventHandler);
    
    
    var event_name_count = events.EventEmitter.listenerCount(eventEmitter,'event_name');
    var remove_listener_count = events.EventEmitter.listenerCount(eventEmitter,'removeListener');
    console.log('event_name_count:' + event_name_count, 'remove_listener_count:' + remove_listener_count);
    
    console.log('----------------移除event_name事件所有监听器-------------------');
    //移除指定事件的所有监听器
    eventEmitter.removeAllListeners('event_name');
    var event_name_count = events.EventEmitter.listenerCount(eventEmitter,'event_name');
    var remove_listener_count = events.EventEmitter.listenerCount(eventEmitter,'removeListener');
    console.log('event_name_count:' + event_name_count, 'remove_listener_count:' + remove_listener_count);
    
    console.log('---------------移除eventEmitter实例所有监听器-----------------------');
    //移除所有的监听器
    eventEmitter.removeAllListeners();
    var event_name_count = events.EventEmitter.listenerCount(eventEmitter,'event_name');
    var remove_listener_count = events.EventEmitter.listenerCount(eventEmitter,'removeListener');
    console.log('event_name_count:' + event_name_count, 'remove_listener_count:' + remove_listener_count);
    
    
    
    
     运行结果如
    >node EventEmitter.js
    error catched
    event_name_count:0 remove_listener_count:1
    add new Listener event_name function (arg1){
            console.log('this is a listener added by on with argument',arg1);
    }
    add new Listener event_name function (arg1,arg2){
            console.log('this is a listener added by addListener with arguments',ar
    1,arg2);
    }
    add new Listener event_name function (){
        console.log('this is a listener added by once');
    }
    add new Listener event_name function (){
            console.log('this is a listener with a name eventHandler');
    }
    ------------------event_name事件的监听器数组-------------
    [ [Function],
      [Function],
      { [Function: g] listener: [Function] },
      [Function: eventHandler] ]
    -------------------触发event_name事件---------------------
    this is a listener added by on with argument 参数1
    this is a listener added by addListener with arguments 参数1 参数2
    remove a Listener event_name function (){
        console.log('this is a listener added by once');
    }
    this is a listener added by once
    this is a listener with a name eventHandler
    hasEventHandler:true
    ---------------------------------------------
    event_name_count:3 remove_listener_count:1
    -----------------移除eventHandler监听器----------------------
    remove a Listener event_name function (){
            console.log('this is a listener with a name eventHandler');
    }
    event_name_count:2 remove_listener_count:1
    ----------------移除event_name事件所有监听器-------------------
    remove a Listener event_name function (arg1,arg2){
            console.log('this is a listener added by addListener with arguments',ar
    1,arg2);
    }
    remove a Listener event_name function (arg1){
            console.log('this is a listener added by on with argument',arg1);
    }
    event_name_count:0 remove_listener_count:1
    ---------------移除eventEmitter实例所有监听器-----------------------
    remove a Listener error function (){
            console.log('error catched');
    }
    remove a Listener newListener function (event,fn){
            console.log('add new Listener',event,fn);
    }
    event_name_count:0 remove_listener_count:0
    (node:26660) Warning: Possible EventEmitter memory leak detected. 4 event_name
    isteners added. Use emitter.setMaxListeners() to increase limit
    
    
     

你可能感兴趣的:(node)