最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In Node.js, does listening to an EventEmitter, create a reference to it? - Stack Overflow

programmeradmin0浏览0评论

If I have some code like this:

const EventEmitter = require('events');

class Bot extends EventEmitter {
  sendMessage() {
    // do something
    this.emit('messageSent', 'user123');
  }
}

class Controller {
  loadBot() {
    const bot = new Bot();
    bot.on('messageSent', userId => {
      // do something
    });
  }
}

Would the bot object created inside loadBot be destroyed immediately? or perhaps later via garbage collection?

Or would the instance of Controller hold a reference to it so that bot would never be destroyed until the Controller instance was destroyed?

If I have some code like this:

const EventEmitter = require('events');

class Bot extends EventEmitter {
  sendMessage() {
    // do something
    this.emit('messageSent', 'user123');
  }
}

class Controller {
  loadBot() {
    const bot = new Bot();
    bot.on('messageSent', userId => {
      // do something
    });
  }
}

Would the bot object created inside loadBot be destroyed immediately? or perhaps later via garbage collection?

Or would the instance of Controller hold a reference to it so that bot would never be destroyed until the Controller instance was destroyed?

Share Improve this question asked Jul 19, 2016 at 3:45 abc123abc123 8,3137 gold badges52 silver badges82 bronze badges 1
  • Where would a Controller instance hold a reference to bot? It's a local variable that goes out of scope when the method ends, not a property. – Bergi Commented Jul 19, 2016 at 4:26
Add a ment  | 

2 Answers 2

Reset to default 7

Registering an event listener all by itself does not keep it from being garbage collected. Something has to actually have a reference to the Bot object itself (so that events could actually be emitted from it) for it to not be garbage collected.

In your Controller class, if nothing else has a reference to the Bot instance you create, then it will be eligible for garbage collection. This makes sense because if nothing has a reference to it, then nothing else can use it and nothing can ever call its custom method sendMessage() either.

As you have your code now, your bot variable is just a local variable inside the loadBot() method and nothing else has a reference to it. So, as soon as the loadBot() method is done executing, the bot variable will then be eligible for garbage collection because there is no code anywhere that could ever use or reach that object again. That makes it eligible for garbage collection.

From your code, it looks like maybe you meant for the bot variable to be an instance variable of your Controller object. If that was the case, then as long as some had a reference to your Controller object, then the bot object would stay alive too.

So, it looks like maybe you meant to do this:

const EventEmitter = require('events');

class Bot extends EventEmitter {
  sendMessage() {
    // do something
    this.emit('messageSent', 'user123');
  }
}

class Controller {
  loadBot() {
    this.bot = new Bot();
    this.bot.on('messageSent', userId => {
      // do something
    });
  }

  send() {
      this.bot.sendMessage();
  }
}

var c = new Controller();
c.loadBot();

Here you retain a reference to the bot variable in the instance data of the Controller object and thus it can be reached by other code (like the send() method) or any other code that accesses the .bot property on your Controller object.

I DONOT think the object bot will be GCed, Because there is a listener hold a reference to it.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论