Search code examples
scalaakkaakka-stream

How do I delay materialisation of an akka stream that's being appended to another akka stream?


The documentation for FlowOps#concat states that

... the Source is materialized together with this Flow and just kept from producing elements by asserting back-pressure until its time comes.

In other words, if I have

sourceA.concat(sourceB)

then sourceB will be materialised at the same time as sourceA.

How do I delay sourceB being materialised until sourceA has completed?

I'm using Akka 2.5.6.


Solution

  • Did you try using Source.lazily()?

    Here's what its scala doc says:

    Creates a Source that is not materialized until there is downstream demand, when the source gets materialized the materialized future is completed with its value, if downstream cancels or fails without any demand the create factory is never called and the materialized Future is failed.

    See also: https://doc.akka.io/docs/akka/2.5.6/scala/stream/stages-overview.html#lazily

    That is:

    sourceA.concat(Source.lazily(() => sourceB))