我在下面创建了Observable构造函数,其工作原理如上所述。有没有人知道使用RxJs附带的运算符是否有更简洁的方法来实现相同的行为?我正在查看接近所需行为的bufferToggle,但我需要在缓冲区关闭时传递所发出的值。
功能描述:如果source
发出condition
,则会触发发出的true
值,如果source
值,则会通过发出的condition
值{1}}发出false
。如果条件在false
后发出true
,则缓冲区按接收顺序释放每个值。初始化缓冲区以通过发出的source
值,直到condition
发出true
。
function bufferIf<T>(condition: Observable<boolean>, source: Observable<T>): Observable<T> {
return new Observable<T>(subscriber => {
const subscriptions: Subscription[] = [];
const buffer = [];
let isBufferOpen = false;
subscriptions.push(
// handle source events
source.subscribe(value => {
// if buffer is open, or closed but buffer is still being
// emptied from previously being closed.
if (isBufferOpen || (!isBufferOpen && buffer.length > 0)) {
buffer.push(value);
} else {
subscriber.next(value);
}
}),
// handle condition events
condition.do(value => isBufferOpen = value)
.filter(value => !value)
.subscribe(value => {
while (buffer.length > 0 && !isBufferOpen) {
subscriber.next(buffer.shift());
}
})
);
// on unsubscribe
return () => {
subscriptions.forEach(sub => sub.unsubscribe());
};
});
}
答案 0 :(得分:2)
我找到了一个基于运营商而不是订阅的解决方案,但不愿意将其称为更简洁。
注意,如果可以保证缓冲区开/关始终以关闭(即奇数个发射)结束,则可以删除endToken。
console.clear()
const Observable = Rx.Observable
// Source and buffering observables
const source$ = Observable.timer(0, 200).take(15)
const bufferIt$ = Observable.timer(0, 500).map(x => x % 2 !== 0).take(6)
// Function to switch buffering
const endToken = 'end'
const bufferScanner = { buffering: false, value: null, buffer: [] }
const bufferSwitch = (scanner, [src, buffering]) => {
const onBufferClose = (scanner.buffering && !buffering) || (src === endToken)
const buffer = (buffering || onBufferClose) ? scanner.buffer.concat(src) : []
const value = onBufferClose ? buffer : buffering ? null : [src]
return { buffering, value, buffer }
}
// Operator chain
const output =
source$
.concat(Observable.of(endToken)) // signal last buffer to emit
.withLatestFrom(bufferIt$) // add buffering flag to stream
.scan(bufferSwitch, bufferScanner) // turn buffering on and off
.map(x => x.value) // deconsruct bufferScanner
.filter(x => x) // ignore null values
.mergeAll() // deconstruct buffer array
.filter(x => x !== endToken) // ignore endToken
// Proof
const start = new Date()
const outputDisplay = output.timestamp()
.map(x => 'value: ' + x.value + ', elapsed: ' + (x.timestamp - start) )
const bufferDisplay = bufferIt$.timestamp()
.map(x => (x.value ? 'buffer on' : 'buffer off') + ', elapsed: ' + (x.timestamp - start) )
bufferDisplay.merge(outputDisplay)
.subscribe(console.log)
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.js"></script>
脚注
我还找到了一个基于buffer()
的解决方案,但我不相信它在高频源下是稳定的。似乎有一些具有某些缓冲器配置的东西(即声明看起来很合理但是测试显示偶尔的延迟会干扰缓冲区操作)。
无论如何,仅供参考,
/*
Alternate with buffered and unbuffered streams
*/
const buffered =
source$.withLatestFrom(bufferIt$)
.filter(([x, bufferIsOn]) => bufferIsOn)
.map(x => x[0])
.buffer(bufferIt$.filter(x => !x))
.filter(x => x.length) // filter out empty buffers
.mergeAll() // unwind the buffer
const unbuffered =
source$.withLatestFrom(bufferIt$)
.filter(([x, bufferIsOn]) => !bufferIsOn)
.map(x => x[0])
const output = buffered.merge(unbuffered)
答案 1 :(得分:1)
这是另一种方式,稍微简短(添加一个新答案,因为前一个很忙)
// Source and buffering observables
const source$ = Rx.Observable.timer(0, 200).take(15)
const bufferIt$ = Rx.Observable.timer(0, 500).map(x => x % 2 !== 0).take(6)
const makeHot$ = (src) => {
const hot$ = new Rx.Subject();
src.subscribe(x => hot$.next(x));
return hot$;
}
// Buffered output
const buffered$ = (source, bufferIt) => {
const hot$ = makeHot$(source)
const close = new Rx.Subject()
return bufferIt
.concat(Rx.Observable.of(false)) // ensure last buffer emits
.do(x => {if(!x) close.next(true)} ) // close previous buffer
.switchMap(x => x ? hot$.buffer(close) : hot$.map(x=>[x]))
.mergeAll()
}
// Proof
const start = new Date()
const outputDisplay = buffered$(source$, bufferIt$).timestamp()
.map(x => 'value: ' + x.value + ', elapsed: ' + (x.timestamp - start) )
const bufferDisplay = bufferIt$.timestamp()
.map(x => (x.value ? 'buffer on' : 'buffer off') + ', elapsed: ' + (x.timestamp - start) )
bufferDisplay.merge(outputDisplay)
.subscribe(console.log)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.2/Rx.js"></script>
&#13;
答案 2 :(得分:0)
我最近正在寻找类似的解决方案,并最终提出了这个建议。
假设可以接受一些反跳时间(也许是可取的),可能会帮助其他人
source$.pipe(buffer(source$.pipe(
debounceTime(500),
filter(condition)
)))