简体   繁体   English

使用关闭覆盖节点eventEmitter.on进行日志记录不起作用

[英]Overriding node eventEmitter.on with closure for logging not working

I'm trying to track down an issue in my SPA where an event is being triggered too many times. 我正试图在SPA中查找某个事件被触发太多次的问题。 With that in mind, I wanted to override node's eventEmitter.on function to add some logging in then call the original. 考虑到这一点,我想重写节点的eventEmitter.on函数以添加一些登录信息,然后调用原始信息。 This lead me to this answer on SO https://stackoverflow.com/a/10427757/261682 about overriding window.alert and I came up with the following code: 这使我在SO https://stackoverflow.com/a/10427757/261682上获得关于覆盖window.alert答案,然后我window.alert了以下代码:

// Ignore `state` here but it essentially means I can access `eventEmitter` all over my app.
var EventEmitter = ('events')   
state.eventEmitter = new EventEmitter()
state.eventEmitter.on = (function (original) {
    return function (name, callback) {
        console.log('On Called for %s', name)
        original(name, callback)
    }
})(state.eventEmitter.on)

This isn't working. 这不起作用。 The event isn't triggered at all and I don't see anything in the console. 该事件根本没有触发,控制台中也看不到任何东西。

What am I doing wrong? 我究竟做错了什么?

Note: There are other ways for me to track my original issue down but I'm interested as to why my code above isn't working. 注意:我还有其他方法可以追踪原始问题,但我对上述代码为何无法正常工作很感兴趣。

I tried your code and it mostly looks fine, though as I run it I get the following error: 我尝试了您的代码,尽管在运行时出现以下错误,但大多数情况下看起来都不错:

Cannot read property '_events' of undefined an internal error of EventEmitter Cannot read property '_events' of undefined的EventEmitter内部错误的Cannot read property '_events' of undefined

This is because the way you pass the original .on() function it will lose the context (this argument) when being called. 这是因为传递原始.on()函数的方式将在调用时丢失上下文(此参数)。

To avoid this bind the function to the event emitter by calling state.eventEmitter.on.bind(state.eventEmitter) . 为了避免这种情况,可以通过调用state.eventEmitter.on.bind(state.eventEmitter)将函数绑定到事件发射器。 I also noticed you forgot the return the result of the original function since on() is chainable. 我还注意到您忘记了返回原始函数的结果,因为on()是可链接的。

Furthermore if no one is listening to the events your overridden function won't be called as well. 此外,如果没有人在听事件,那么覆盖的函数也不会被调用。

Here's the code with the modifications mentioned above 这是经过上述修改的代码

var EventEmitter = require('events');
state.eventEmitter = new EventEmitter();
state.eventEmitter.on = (function (original) {
    return function (name, callback) {
        console.log('On Called for %s', name);
            return original(name, callback);
        }
})(state.eventEmitter.on.bind(state.eventEmitter));

state.eventEmitter.on('my_event', function () {
    console.log('my_event has been called');
});

state.eventEmitter.emit('my_event');

With this got the following output: 有了以下输出:

On Called for my_event
my_event has been called

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM