NodeJS EventEmitter没有触发

时间:2016-05-05 17:00:31

标签: node.js eventemitter

我正在上一个班级WorkflowParent,这个班级都是EventEmitter,并且具有EventEmitter类型的子属性。

我希望该类拦截其子属性中的某些事件,并使用不同的事件名称重新发出它们。

看起来我做得对,但它不起作用。这是我的代码:

var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var awsswf = require('aws-swf');
var swf = awsswf.createClient();
var util = require('util');

function WorkflowParent(){
    EventEmitter.call(this);
    var me = this;

    //Create decider object
    me.decider = new awsswf.Decider({}); // <---- THIS IS AN EventEmitter

    //Emit events from `decider` and `actor` objects
    ['poll'].forEach(function(eventName){
        me.decider.on(eventName, function(event){
            console.log('decider event "'+eventName+'"', event); // <---- THIS IS EXECUTED
            var attached = me.emit(eventName+'.decider', event); // emit "poll.decider" event
            console.log('attached to poll.decider event?', attached); // <---- THIS IS `false`
        });
    });

}

util.inherits(WorkflowParent, EventEmitter);

WorkflowParent.prototype.start = function(){
    this.on('poll.decider', function(event){
        console.log('poll.decider event triggered!'); // <---- THIS NEVER RUNS
    });
    this.decider.start();
};


module.exports = new WorkflowParent();

1 个答案:

答案 0 :(得分:1)

事实证明,您不能在继承自domain的类中使用名为EventEmitter的属性。我将me.domain重命名为me._domain并且有效。

在此处找到此错误的详细信息:https://github.com/nodejs/node-v0.x-archive/issues/3922

  

如错误中所述,具有域属性的任何内容也是一个   EventEmitter会发生这种冲突。