简体   繁体   English

Node.js:如何跨模块发送事件

[英]Node.js: how to send events across modules

I created two modules, "person" and "group", with "group" depending on "person": 我创建了两个模块“ person”和“ group”,其中“ group”取决于“ person”:

person.js: person.js:

var events = require('events');

exports.Person = function() {
    var personEvents = new events.EventEmitter();

    function generateEvent(args) {
        console.log("Emitting event with args "+JSON.stringify(args));
        personEvents.emit("myEvent", args);
    }

    return {
        personEvents: personEvents,
        generateEvent: generateEvent
    };
}

group.js: group.js:

var PERSON = require("person.js").Person();
var events = require('events');

exports.Group = function() {
    function handleEvent(args) {
        console.log("Caught event with args "+JSON.stringify(args));
    }

    PERSON.personEvents.on("myEvent", function(args) {
        handleEvent(args);
    });
}

Now I have the main program main.js as a separate module: 现在,我将主程序main.js作为一个单独的模块:

var PERSON = require("person.js").Person();
var GROUP = require("group.js").Group();

PERSON.generateEvent({ type: "bla" });

When I execute "node ./main.js", I will see the message that the event was generated, but the event is not caught. 当我执行“ node ./main.js”时,我会看到一条消息,说明已生成事件,但未捕获到该事件。 It seems that the listener in the group module is not bound to the same "personEvents" variable created in the person module in the main namespace. 似乎组模块中的侦听器未绑定到主命名空间中的人员模块中创建的相同“ personEvents”变量。

I am rather new at javascript and its implications for programming. 我对javascript及其对编程的意义还很陌生。 I used examples I found on the internet to get the code I have so far, but I feel that this may not be the correct way to setup the modules and the cross-module events. 我使用在互联网上找到的示例来获取到目前为止的代码,但是我觉得这可能不是设置模块和跨模块事件的正确方法。

Can someone explain to me how I should setup the modules so I can generate events in one module and catch them in another? 有人可以向我解释如何设置模块,以便可以在一个模块中生成事件并在另一个模块中捕获事件吗? Thanks! 谢谢!

When you call var PERSON = require("person.js").Person(); 当您调用var PERSON = require("person.js").Person(); , that creates a new instance. ,这将创建一个新实例。 So, when you call it in group.js , that creates a separate object/instance than the one you create when you call it in main.js . 所以,当你在调用它group.js ,创建一个单独的对象/实例时相比,你叫什么你创建一个main.js You could get around this by passing the Person object to Group when you create it in main.js like: main.js创建Person对象时,可以通过将Person对象传递给Group来解决此问题:

group.js group.js

var events = require('events');

exports.Group = function(PERSON) {
    function handleEvent(args) {
        console.log("Caught event with args "+JSON.stringify(args));
    }

    PERSON.personEvents.on("myEvent", function(args) {
        handleEvent(args);
    });
}

main.js main.js

var PERSON = require("person.js").Person();
var GROUP = require("group.js").Group(PERSON);

PERSON.generateEvent({ type: "bla" });

That being said, that's not a very loosely coupled system. 话虽这么说,这并不是一个松散耦合的系统。 If you want something more loosely coupled, then Group and Person should not reference each other at all and the communication between the two should happen in main.js . 如果您想要更松散地耦合,那么GroupPerson根本不应该互相引用,并且两者之间的通信应该在main.js

You can accomplish that by changing group.js and main.js to something like this: 您可以通过将group.jsmain.js更改为group.js main.js来实现:

group.js group.js

var events = require('events');

exports.Group = function() {
    function handleEvent(args) {
        console.log("Caught event with args "+JSON.stringify(args));
    }
}

main.js main.js

var PERSON = require("person.js").Person();
var GROUP = require("group.js").Group();

PERSON.on('myEvent', function(args) {
    GROUP.handleEvent(args);
});

PERSON.generateEvent({ type: "bla" });

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

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