0
votes

Is there a way to use RxJs buffer without clearing the buffer when the closingNotifier emits or achieve the same result with other RxJs operators?

From https://rxjs.dev/api/operators/buffer:

Buffers the incoming Observable values until the given closingNotifier Observable emits a value, at which point it emits the buffer on the output Observable and starts a new buffer internally, awaiting the next time closingNotifier emits.

I want to do the same thing except continue buffering incoming values on the same buffer after the closingNotifier emits.

1

1 Answers

1
votes

If I understand correctly, you want to emit the cumulative results each time. scan will work perfectly for this:

const buffered$ = timer(0, 1000).pipe(
  buffer(interval(3000)),
  scan((acc, cur) => [...acc, ...cur], [])
);

// output:
// - [ 0, 1, 2 ]
// - [ 0, 1, 2, 3, 4, 5 ]
// - [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]

Here's a working StackBlitz demo.