如果尚未添加侦听器并且侦听器处理程序是匿名函数,则在 NodeJS 中添加侦听器

gku*_*ian 5 event-handling listener addeventlistener node.js

有没有办法检查 Node.js 中的对象是否已存在侦听器?我想实现以下场景:

  • 获取db对象
  • 做一些操作
  • 如果尚未添加相同的侦听器,请添加侦听器,例如错误、结果、耗尽等[假设对于所有操作,侦听器操作都是相同的]

我想以这样的方式优化侦听器的添加:如果我们尝试添加现有侦听器,则不会添加新侦听器。Node 文档说“不会检查侦听器是否已添加。传递相同的 eventName 和侦听器组合的多个调用将导致侦听器被多次添加和调用。

有办法解决吗?
[编辑]-添加一些示例代码

 connpool.getConnection(function(err, connection) {

      var querystr = "Some valid SQL query";

      connection.execute(querystr, data, function(err, rows) {
        if (err) {
          console.error(err);
        }
        connection.on('error', function(err){onErr(err,connection);});
        do some stuff
        cleanup(connection);
    });
    })  

   var onErr = function(err, connection) {
      console.error({"Error message"});
      connection.release();
      cleanup(connection);
   };

   var cleanup = function(conn) {
    conn.removeListener('error',onErr);
   };
Run Code Online (Sandbox Code Playgroud)

Connection将包含一个数据库连接,它来自外部包。在语句connection.on('error', function(err){onErr(err,connection);}); 我正在使用匿名函数,因为我需要向清理方法传递一个额外的参数。在清理过程中,我没有获得该函数的处理程序,因为我使用的是匿名函数。

Coo*_*lue 2

只要您在挂钩时保留对侦听器的引用,就可以检查它是否在 . 返回的侦听器数组中emitter.listeners(eventName)

粗略的例子(我相信它会更有效)

/**
 * Created by cool.blue on 8/4/2016.
 * http://stackoverflow.com/q/38700859/2670182
 */
const EE = require('events');
const util = require('util');

var host = new EE();

// set up a emitter with n events
const n = 10;
const events = Array.apply(null, Array(n)).map((x, i) => 'event_' + i);

events.forEach(function(e){
    host.on(e, function g() {console.log(e)})
});

console.log(util.inspect(host));

// get a reference to one of the listener functions
const target = 'event_3';
var probe = host.listeners(target)[0];

// add a method to only add unique listeners
host.onUnique = function (type, listener){
    var slot = this.listeners(type).find(function(l) {
        return l === listener
    });

    if(slot)
        return this;

    console.log('adding');
    return this.on(type, listener)
};

// try to add the same listener again
var count0 = host.listenerCount(target);
var count1 = host.onUnique(target, probe).listenerCount(target);

console.log('added ' + (count1 - count0) + ' listeners');   // added 0 listeners
console.log(util.inspect(host));

// try to add a new listener
count0 = host.listenerCount(target);
count1 = host.onUnique(target, function h(){ console.log('different cb')}).listenerCount(target);

console.log('added ' + (count1 - count0) + ' listeners');   // added 1 listeners
console.log(util.inspect(host));
Run Code Online (Sandbox Code Playgroud)

回答更新的问题...

你可以做这样的事情......

长话短说

基本思想是为侦听器使用非匿名函数,并传递对其的引用以及与外部作用域中的实用程序函数的连接。

const EE = require('events');
const util = require('util');

(function manage(host){

    host.name = 'host';
    host.release = function(){
        console.log('released!')
    };

    function l(err) {
        onErr(err, host, l)
    }
    l.e = 'error';

    host.on('error', l);

    if(Math.random() > 0.5)
        host.emit('error', new Error('oops!'));

    if(l.e)
        cleanUp(host, l, 'manage');

})(new EE());

function onErr(e, h, l) {
    console.error(`\n${h.name}: ${e.message}`);
    h.release();
    cleanUp(h, l, 'onError')
}

function cleanUp(h, l, context){
    console.log('\n\x1b[33m' + context + '\n'
        + 'before:\t' + h._eventsCount + '\x1b[0m\n' + util.inspect(h));
    h.removeListener(l.e, l);
    console.log('\n\x1b[33mafter:\t' + h._eventsCount + '\x1b[0m\n' + util.inspect(h));
    delete l.e
}
Run Code Online (Sandbox Code Playgroud)

IIFE 只是为了模拟外部范围中没有引用host( ) 的情况。connection