无限递归流溢出

时间:2015-01-19 20:02:03

标签: javascript node.js bacon.js

请解释为什么下面的流溢出了?

infiniteStream = function(page) {
  return Bacon.fromBinder(function(sink) {
    sink(page);
    sink(new Bacon.Next(function() { return infiniteStream(page + 1); }));
  }).flatMapConcat(function(v) {
    return v;
  });
};

fiveStream = function(count) {
  return Bacon.fromArray([count, count, count, count, count]);
};

stream = infiniteStream(1).flatMapConcat(function(v) {
  return fiveStream(v);
});

// fine
stream.take(6).log();

// Error: maximum call stack exceeded
stream.take(7).log();

我需要一个无限的流,我可以映射到某些值,我想根据需要使用take(10),或者我想在达到某个特定值时结束此流,例如:

stream = stream.withHandler(function(v) {
  if (v.hasValue() && v.value() == "link50") {
    this.push(new Bacon.Error("bad link"));
    return this.push(new Bacon.End());
  } else {
    return this.push(v);
  }
});

0 个答案:

没有答案
相关问题