猿问

node.js eventemitter.on 不手动remove 会被回收吗

node.js eventemitter.on 不手动remove 会被回收吗


MM们
浏览 570回答 2
2回答

UYOU

在nodejs中有一个EventEmitter类,目的是实现各种事件的event处理。当需要指定绑定事件时,可以使用EventEmitter类的on方法或addListener方法.这两个方法实现原理相同,只是名称不一样.这两个方法都有两个参数,第一个参数为指定事件名 第二个参数为该事件的事件处理函数:var http = require('http');var server = http.createServer();server.addListener('request', function(req, res) { console.log('addListener');res.end();})server.on('request', function(req, res) { console.log('on');res.end();})setMaxListeners()在默认情况下,同一个指定的事件,最多可以绑定10个事件处理函数。也可以通过下面的方法修改:var http = require('http');var server = http.createServer();server.setMaxListeners(5);server.addListener('request', function(req, res) { console.log('mark');res.end();})也可以指定最多绑定5个,当绑定数超出时,启动nodejs时,会有相应的错误提示。(node) warning: possible EventEmitter memory leak detected. 6 request listeners added. Use emitter.setMaxListeners() to increase limit.Traceat Server.addListener (events.js:179:15)at Object.<anonymous> (/Users/demacia/nodejs-mark/event_emitter.js:41:8)at Module._compile (module.js:460:26)at Object.Module._extensions..js (module.js:478:10)at Module.load (module.js:355:32)at Function.Module._load (module.js:310:12)at Function.Module.runMain (module.js:501:10)at startup (node.js:129:16)at node.js:814:3emitter.once()和emitter.on()emitter.once()与emitter.on()方法类似区别在于,emitter.once()当事件处理函数,执行一次后会立即解除,该事件处理函数只会执行一次。removeListener()可以解除某个事件处理函数:var http = require('http');var server = http.createServer();var mark = function(req, res) { console.log('mark');res.end();}server.addListener('request', mark);server.removeListener('request', mark);removeAllListeners()可以取消所有事件处理函数:var http = require('http');var server = http.createServer();server.addListener('request', function(req, res) { console.log(1);res.end();})server.addListener('request', function(req, res) { console.log(2);res.end();})server.removeAllListeners('request');listenerCount()获取事件处理函数的数量:var http = require('http');var events = require('events');var server = http.createServer();server.addListener('request', function(req, res) { console.log(1);res.end();})server.addListener('request', function(req, res) { console.log(2);res.end();})console.log(events.EventEmitter.listenerCount(server, 'request'));newListener()任何时候当继承EventEmitter类的子类实例化对象绑定事件处理函数时,都将触发EventEmitter类的newListener事件var http = require('http');var server = http.createServer();var mark = function(req, res) { console.log('mark');res.end();}server.on('newListener', function(e, f) { console.log('newListener');});server.on('removeListener', function(e, f) { console.log('removeListener');});server.addListener('request', mark);以上代码执行结果为:输出两次newListener因为removeListener方法也是属于新绑定的事件处理函数removeListener()任何时候当继承removeListener类的子类实例对象取消处理函数时,都将触发EventEmitter类的removeListener事件var http = require('http');var server = http.createServer();var mark = function(req, res) { console.log('mark');res.end();}server.on('newListener', function(e, f) { console.log('newListener');});server.on('removeListener', function(e, f) { console.log('removeListener');});server.addListener('request', mark);server.removeListener('request', mark);执行结果为:输出两次newListener和一次removeListener
随时随地看视频慕课网APP

相关分类

Node.js
我要回答