In this scenario, I have a stream of numbers being emitted every second. My goal is to group these numbers into arrays for a duration of 4 seconds, except when the number emitted is divisible by 5, in which case I want it to be emitted immediately without being buffered.
// Example: emitting a value every 1 second
// 0,1,2,3,4,5...
const source = interval(1000);
// Buffer values for 4 seconds
const buffered = source.pipe(
buffer(interval(4000)),
filter(x => x.length > 0)
);
const subscribe = buffered.subscribe(val => console.log(val));
The expected outcome should be:
[0,1,2,3] // Emits after 4 seconds
[5] // Emits immediately
[4,6,7,8] // Emits after 4 seconds
[10] // Emits immediately
[9,11,12,13]
// ...and so forth
See it in action on Stackblitz: https://stackblitz.com/edit/typescript-cdvc2d?file=index.ts