stimms stimms - 3 months ago 25
Javascript Question

Throttle stream without buffering

I have some toast messages that I'd like to throttle. I tried using buffer but then I get a bunch of messages in an array when what I'd really like is for the messages to simply stay in the stream until I ask for them. What I ended up doing was zipping my message stream with an interval stream

var messageStream = Rx.Observable.FromEvent(..., 'click');
var intervalStream = Rx.Observable.interval(5000);
messageStream.Zip(intervalStream, (x,_)=>x).subscribe(showToast(x));


Is there a more elegant way to do this?

Answer

Have a look at 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