How splitWhen works?

Hello everybody!
I’m playing with akka streams and got something I don’t understands. I have simple code:
Source(1 to 100)
.throttle(1, 100.millis)
.map(x => {println("raw "+x);x})
.groupBy(10, _ % 2, false)
.concatSubstreams
.map(x => {println("concat "+x);x})
.to(Sink.ignore).run()

When I run it I get:
raw 1
concat 1
raw 2

And after this, the app is stuck, which makes perfect sense. ‘concatSubstreams’ tries to read everything from first substream, this substream is never ending, so second substream is backpressed.
But if I add ‘splitWhen’ right after groupBy it starts working!
Source(1 to 100)
.throttle(1, 100.millis)
.map(x => {println("raw "+x);x})
.groupBy(10, _ % 2, false)
.splitWhen(_ => false)
.concatSubstreams
.map(x => {println("concat "+x);x})
.to(Sink.ignore).run()

raw 1
concat 1
raw 2
concat 2
raw 3
concat 3
raw 4
concat 4
raw 5

‘splitWhen’ actually doesn’t split anything, the function I pass always return false. So, there should be the same never ending stream. Why it works now? What do i miss?

Thank you,
Andrey

Hi Andrey,

What trips you here is that both grouped and splitWhen create sub-flows.

When you introduce splitWhen the concatSubstreams applies to the splitWhen substream and the elements in that stream are passed to Sink.ignore which makes the backpressure go away.

As there is never more than one substream to this splitWhen the concatSubstreams does not have to wait for a stream to complete.

Does this explain the inspected behaviour?

Cheers,
Enno/

Ahh. Substreams. I see now.
Thank you very much!

Andrey