V
V
Vitaly2016-12-02 19:39:03
JavaScript
Vitaly, 2016-12-02 19:39:03

Do I need to take care of freeing memory with EventEmitter?

Example. There are 2 emitters, one contains the other. You need to subscribe the top one to the bottom one so that the events of the bottom one are transferred to the top one.

class Up extends EventEmitter {
  down = new EventEmitter();
  constructor() {
    down.on('event', () => this.emit);
  }
}

In Angular 2, when using subscribe, you need to unsubscribe in the destructor by calling unsubscribe.
Is there a similar need with EventEmitter? What happens if the down property is assigned a new value? What if the Up class object is overwritten?
Immediately I want to clarify how to deal with the limiters on the number of signed events. Is lifting restrictions practiced, or does such a need mean an erroneous architecture?
In general, you need to fix this moment in your head so that later you do not face incomprehensible memory leaks. Thanks for understanding)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Sergeevich, 2016-12-02
@vitali1995

Maybe I'm not fully competent in this matter and I myself am mistaken, but the leak is not visible
. A memory leak can occur if a "long-lived" object refers to a "short-lived" one
when you write:
at down in the events array, a link to the emit method in an instance of the Up class is added, if there is no instance of the Up class, then there will be no links to it from down. The problem can be if an instance of the Up class also subscribes to events in down
If anything, then sorry for the confusion.
If someone reads and sees an error in this answer, I urge you to correct it, it will also be useful to me)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question