I've a use case where the stream should only emit when the cumulative "sum" equals or exceeds a given value, n. Let's take the example of six integers with n = 5.
+---+------+---------+
| i | Emit | Sum |
+---+------+---------+
| 1 | - | 1 |
| 2 | - | 3 |
| 3 | 5 | 1 |
| 4 | 5 | 0 |
| 5 | 5 | 0 |
| 2 | 2 | 0 (end) |
+---+------+---------+
As you can see, nothing is emitted unless the sum equals or exceeds 5, except for the last element, which is emitted anyway.
Once an item is emitted, the sum gets reduced by that value (n). In reality, I'm reading data from a network call, and subsequently sending them to a downstream consumer who only accepts fixed size chunks, except for the last one, of course (upstream completed).
I'm using project Reactor Flux as the Publisher
; I couldn't find any method on it that allows me do what is shown above. scan
comes closest, but it also emits intermediate elements that need to be filtered out.