Bacon.js控制流与其他流的缓冲

时间:2014-07-11 06:29:19

标签: javascript frp bacon.js

我想在Bacon.js中缓冲一个EventStream的值,就像在{RxJava中} buffer(closingSelector)行为一样。当“控制器流”(RxJava方法中的closingSelector)发出新值时,事件缓冲区将被刷新。

所以我希望流输出与stream.bufferWithTimeOrCount类似,但不是用时间间隔或事件计数来控制缓冲,而是想控制其他流的缓冲。

在Bacon.js中有一种简单的方法可以实现吗?

3 个答案:

答案 0 :(得分:1)

Bacon.holdWhen可用,因为大约0.7.14几乎可以满足您的需求,尽管缓冲事件是逐个发出的:

  

stream.hold当(阀门)暂停并缓冲事件流,如果阀门中的最后一个事件是真的。当阀门变得麻痹时,所有缓冲的事件都会被释放。

如果您需要将缓冲事件作为单个事件发出,可以尝试以下内容:

// source streams
var sourceObservable = Bacon.interval(1000);
var closingSelector = new Bacon.Bus();

// Constructing a new Observable where we're going to keep our state.
// 
// We need to keep track of two things: 
//   - the buffer that is currently being filled, and
//   -  a previous buffer that is being flushed.
// The state will then look like this:
//   [ buffer, flushed]
// where both buffer and flushed is an array of events from the source observable.

// empty initial state
var initialState = {buffer: [], flushed: []}

// There are two operations on the state: appending a new element to the buffer 
// and flushing the current buffer:

// append each event from the source observable to the buffer,
// keeping flushed unchanged
var appends = sourceObservable.map(function(e) {
   return function(state) {
       state.buffer.push(e); return state; 
   } 
});

// each event from the closingSelector replaces the `flushed` with 
// the `buffer`'s contents, inserting an empty buffer.
var flushes = closingSelector.map(function(_) {
   return function(state) { return {buffer: [], flushed: state.buffer} }
})

// merge appends and flushes into a single stream and apply them to the initial state
var ops = appends.merge(flushes)
var state = ops.scan(initialState, function(acc, f) { return f(acc) });

// resulting stream of flushed events
var flushed = state.sampledBy(closingSelector).map(function(state) { return state.flushed })

// triggered with `closingSelector.push({})`
flushed.onValue(function(x) { console.log("flushed", x) })

答案 1 :(得分:1)

Bacon.js没有你需要的功能,所以我看了bacon.js source并写了holdWhen的修改版本。

Bacon.EventStream.prototype.bufferUntilValue = function(valve) {
var valve_ = valve.startWith(false);

  return this.filter(false).merge(valve_.flatMapConcat((function(_this) {
    return function() {
        return _this.scan([], (function(xs, x) {
            return xs.concat(x);
        }), {
            eager: true
        }).sampledBy(valve).take(1);
    };
  })(this)));
};

要查看此操作,请查看此jsFiddle

答案 2 :(得分:0)

stream.holdWhen(valve)看起来几乎就是你想要的。它的工作方式与buffer(closingSelector)略有不同:不是一直缓冲来自closingSelector的事件的缓冲,而是根据value流中的最后一个值来切换缓冲。

也许您可以按原样使用holdWhen,但如果您想要buffer(closingSelector)中的行为,则可能会执行以下操作:

var result = sourceStream.holdWhen(closingSelector.flatMap(function(){
  return Bacon.fromArray([false, true]);
}).toProperty(true));

在来自closingSelector的每个事件中,我们在value流中生成两个事件,其值为truefalse,即关闭缓冲(触发刷新)然后立即转动它重新开始。