我想在Bacon.js中缓冲Bacon.js中的buffer(closingSelector)值,就像buffer(closingSelector)在RxJava中的行为一样。当“控制器流”(closingSelector in RxJava方法)发出一个新值时,事件缓冲区就会被刷新。
因此,我希望流输出类似于stream.bufferWithTimeOrCount中的输出,但是我不想用时间间隔或事件计数来控制缓冲,而是想用其他流来控制缓冲。
是否有一种在Bacon.js中实现这一功能的简单方法?
发布于 2014-07-16 07:04:53
Bacon.js没有所需的函数,所以我查看了bacon.js源并编写了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。
发布于 2014-07-14 07:46:33
自0.7.14以来可用的Bacon.holdWhen几乎可以满足您的需要,不过缓冲事件是一个接一个地发出的:
如果阀门中的最后一个事件是真实的,stream.holdWhen(阀门)会暂停并缓冲事件流。当阀门失效时,所有缓冲事件都会被释放。
如果需要将缓冲事件作为单个事件发出,则可以尝试如下所示:
// 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) })发布于 2014-07-15 22:25:55
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流中生成两个具有true和false值的事件,即关闭缓冲(这会触发刷新),然后立即打开它。
https://stackoverflow.com/questions/24691378
复制相似问题