title: Does Functional Reactive Programming in JavaScript cause bigger problems with listener references?
body: <p>In JavaScript the observer pattern is used quite often. There is one tricky thing with it and that's the references the subject keeps of the observers. They require cleanup. For regular applications I use the following rules of thumb:</p>
  
  <ul>
  <li>If the subject has a life span shorter than (or equal to) the observer, I can just do <code>subject.on('event', ...)</code></li>
  <li>If the subject has a life span longer than the observer, I need to use <code>observer.listenTo(subject, 'event', ...)</code></li>
  </ul>
  
  <p>In the second case, the <code>listenTo</code> is aware of the life-cycle of the observer and it will automatically remove the listeners when it's time for the observer to die.</p>
  
  <p>In modern day SPA (Single Page Application) style, where only parts of the application are active at any time this is something that becomes very important. If you combine that with web sockets, which are a perfect candidate for an event stream and most likely long lived, this becomes even more important.</p>
  
  <p>With FRP, having something like an event stream representing changing values over time, I am (without knowing it) creating a lot of listeners. Each <code>filter</code>, <code>map</code> and <code>flatMap</code> creates a new stream that is tied (probably using a listener) to the previous one.</p>
  
  <p>In my mind it seems quite tricky to determine how and when I need to remove those listeners. I can not imagine me being the first to think about this problem, yet I could not find much about this on the Internet.</p>
  
  <p>I have seen some frameworks in other languages use weak references. JavaScript does not have the concept of weak references (WeakMap is not usable here). Even if it had though, it seems like a bad idea because it's unclear when garbage collection takes place. </p>
  
  <ul>
  <li>How is this solved in the current frameworks?</li>
  <li>Do the frameworks tie into the life-cycle of objects? If yes: how?</li>
  </ul>