Search code examples
javascriptfrpbacon.js

Should I unplug single-value stream from Bacon.Bus?


E.g.

var stream = new Bacon.Bus();
for(var i = 0; i < 4; i++) {
  stream.plug(Bacon.later(3000, 'value');
}

Instead of for-loop I may have callback for some async thingy + other values are also being pushed into stream.


Solution

  • There is no need to unplug the stream, as long as it ends (there is a Bacon.end event in the stream). Bacon is smart enough to handle ending streams without memory leaks.

    If you can't get the stream to end naturally, but know that you are only interested in one value, you can create a single value stream with .take(1).

    Using bus.plug to dynamically add streams to a bus is often a sign of a design flaw in your application. It's impossible to give precise advice without seeing your code, but you should think about reorganising your code so that the chain of streams is created at start and only the values are dynamic. It will probably involve the use of flatMap :)