Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140220

rxjs - buffer stream until function returns true

$
0
0

I have a stream of numbers which increase by a constant amount which I want to sub-sample. Given a constant sample interval, I want to buffer the stream until the difference between the first and last buffered value are greater or equal to interval. It would then emit this array, so similar to the buffer operator.

I've searched through the different rxjs operators but can't figure out how to make this work. A bufferUntil operator would be perfect but doesn't seem to exist. How can I implement this?

For example:

const interval = 15;
//example stream would be: 5, 10 , 15, 20, 25, 30..

Observable.pipe(
   bufferUntil(bufferedArray => {
       let last = bufferedArray.length - 1;
       return (bufferedArray[last] - bufferedArray[0] >= interval);
   })
).subscribe(x => console.log(x));

//With an expected output of [5, 10, 15, 20], [ 25, 30, 35, 40],..

Viewing all articles
Browse latest Browse all 140220


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>