节流无缓冲

Throttle stream without buffering

我有一些消息要限制。我尝试使用缓冲区,但是当我真正想要的是让消息简单地保留在流中直到我请求它们时,我在数组中得到了一堆消息。我最终做的是用间隔流

压缩我的消息流
var messageStream = Rx.Observable.FromEvent(..., 'click');
var intervalStream = Rx.Observable.interval(5000);
messageStream.Zip(intervalStream, (x,_)=>x).subscribe(showToast(x));

有没有更优雅的方法来做到这一点?

看看controlled. It enables you to queue values, waiting for you to .request(x) x values. To use with care, as this means memory will be used to buffer the values and memory is not infinite. This could also be a good reading : backpressure

这是一种使用缓冲区的方法,然后使用 flatMap 展开数组:

var messageStream = Rx.Observable.FromEvent(..., 'click');
var intervalStream = Rx.Observable.interval(5000);
messageStream
    .buffer(intervalStream)
    .flatMap( function (x) {
        return Rx.Observable.from(x)
    })
    .subscribe( function  (x) { 
        showToast(x) 
    })

Example here on jsfiddle.