gpt4 book ai didi

node.js - NodeJS EventEmitter 未触发

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:27 27 4
gpt4 key购买 nike

我正在开发一个 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();

最佳答案

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

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

As noted in the bug, anything with a domain property that is also an EventEmitter is going to have this conflict.

关于node.js - NodeJS EventEmitter 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37056190/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com