Bacon.holdWhen
이후에 대한 0.7.14 버퍼링 이벤트가 하나 하나 방출하지만 당신이 원하는 거의 무엇을 수행합니다
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) })
정말 좋은 접근! 내 솔루션 ('holdWhen'과'bufferWithTime'을 함께 사용)보다 깨끗합니다. 다음 밸브 이벤트가 도착할 때 각 밸브 이벤트가 'sampledBy (valve) .take (1)'로 버퍼 된 값을 반환하는 새로운 "스캐너"를 생성하는 방법을 이해하는 데 시간이 걸렸습니다. – attekei
나는 해결책을 단순화했다. 여기를 보아라. [jsFiddle] (http://jsfiddle.net/7DeQy/) (나중에 나는 holdWhen 만 조금 수정했다. .filter (false) .merge (..)'등) – attekei