Search code examples
javascriptbacon.js

Bacon.js Maximum Call Stack Exceeded


I'm trying to produce a stream similar to Bacon.fromPoll for requestAnimationFrame

Why does the following code produce a "Maximum call stack exceeded" error?

function rafSequence() {
    var raf = Bacon.fromCallback(function(callback) {
        requestAnimationFrame(function() {
            callback(Date.now());
        });
    });
    return raf.merge(raf.flatMap(rafSequence));
}

rafSequence().log();

I thought merge() would garbage collect when one of the 2 streams threw a Bacon.End (the raf in raf.merge(...). So why does it error?

UPDATE: I have been able to implement a working version as follows:

Bacon.repeat(() => Bacon.fromCallback(requestAnimationFrame));

I am still interested why merge() isn't cleaning up.


Solution

  • In the current Bacon.js implementation (0.6.x) all "infinite" sequences based on recursion are bound to fail, because at each step, the "stream stack" gets deeper. I'm sure there is a way to optimize the implementation to cope better with this kind of constructs, but it's far from trivial.

    For your particular case, a fromGenerator method would make your implementation simpler. There's already related code in this commit, if you're interested. I suggest you use Github Issues in case you're interested in getting this fixed.