简体   繁体   English

NodeJS中基于对象的EventListener / EventEmitter?

[英]Object Based EventListener/EventEmitter in NodeJS?

I suddenly realized that event emitter in NodeJS is usually like a static method in Java.. Example: 我突然意识到,NodeJS中的事件发射器通常就像Java中的静态方法。

// This illustrated that event listener is universal
function A(a){
  var that = this;
  this.a = a;
  this.cnt = 0;
  this.done = function(){
    this.emit("done");
  };
  this.say = function(){
    console.log(a + " = " + that.cnt);
  };
  this.inc = function(){
    that.cnt++;
  };
}
A.prototype = new events.EventEmitter;


var a = new A("a"),
    b = new A("b"),
    c = new A("c");

a.on("done",function(){a.inc()});
b.on("done",function(){b.inc()});
c.on("done",function(){c.inc()});


c.done();
c.done();
a.say();
b.say();

This code would give output: 此代码将给出输出:

a = 2
b = 2

While I'm actually expecting: 虽然我实际上期望:

a = 0
b = 0

I believe this is because of the line: 我相信这是因为:

A.prototype = new events.EventEmitter;

and I think the "prototype" kind of definition would be used like "static" in Java. 而且我认为Java中会像“静态”那样使用“原型”类型的定义。

In order to have per-object based event listener, I changed the above code to be: 为了具有基于对象的事件侦听器,我将上面的代码更改为:

function B(a){
  var that = this;
  this.evt = new events.EventEmitter;
  this.a = a;
  this.cnt = 0;
  this.done = function(){
    this.evt.emit("done");
  };
  this.say = function(){
    console.log(a + " = " + that.cnt);
  };
  this.inc = function(){
    that.cnt++;
  };
}

var a = new B("a"),
    b = new B("b"),
    c = new B("c");

a.evt.on("done",function(){a.inc()});
b.evt.on("done",function(){b.inc()});
c.evt.on("done",function(){c.inc()});

c.done();
c.done();
a.say();
b.say();

This would be per-object event listener, but I don't really think that is a good design/implementation because it breaks the chaining of EventEmitter. 这将是每个对象的事件侦听器,但是我真的认为这不是一个好的设计/实现,因为它破坏了EventEmitter的链接。 Ie, like code bellow: 即,就像下面的代码:

// can chain another method of A after the on() method
a.on("event",functionCallback).anotherMethodOfA();

I'd like to ask, what's a proper implementation of the per-object event listener in NodeJS? 我想问一下,NodeJS中每个对象事件侦听器的正确实现是什么?

You can use addListener or on to attach listeners to your custom events. 您可以使用addListeneron将侦听器附加到您的自定义事件。 You won't need to chain calls on these methods. 您无需在这些方法上链接调用。 Of course you can inherit any object from EventEmitter and add emitting functionality to your object. 当然,您可以从EventEmitter继承任何对象, EventEmitter您的对象添加发射功能。 You can inherit your object from an instance of EventEmitter . 您可以从EventEmitter的实例继承对象。 There's is function called inherit in the util library which does that for you. util库中有一个称为inherit的函数为您完成。

var util = require('util');
var eventEmitter = require('events').EventEmitter;

// Now create your constructor/object.
function MyObj(a, b) {
    this.a = a;
    this.b = b;
    .
    .
    .
}
util.inherits(MyObj,eventEmitter);

// Implement your methods and add the functionality you need.
MyObj.prototype.aMethod = function(arg) {
    .
    .
    .
    // Define how to emit events
    if (arg == 'A')
        this.emit('eventA', this.a);
    else if (arg == 'B')
        this.emit('eventB');

    // Return this for chaining method calls
    return this;
}

MyObj.prototype.anotherMethod = function() {
    // Add more functionality...
    .
    .
    .
    return this;
}

// Now instantiate the constructor and add listenters

var instanceOfMyObj = new MyObj('a parameter', 'another parameter');

instanceOfMyObj.on('eventA', function(a){
    // Handle the event
});

// Now chain calls..
instanceOfMyObj.aMethod('A').anotherMethod(); // This will trigger eventA...

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

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